当前位置: 移动技术网 > IT编程>开发语言>PHP > 微信公众号开发之语音消息识别php代码

微信公众号开发之语音消息识别php代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了php微信语音消息识别代码,供大家参考,具体内容如下

1.开通语音识别(默认关闭)

2.语音识别

请注意,开通语音识别后,用户每次发送语音给公众号时,微信会在推送的语音消息xml数据包中,增加一个recognition字段(注:由于客户端缓存,开发者开启或者关闭语音识别功能,对新关注者立刻生效,对已关注用户需要24小时生效。开发者可以重新关注此帐号进行测试)。开启语音识别后的语音xml数据包如下:

<?php
/**
 * wechat php test
 */

//define your token
define("token", "weixin");
$wechatobj = new wechatcallbackapitest();
//$wechatobj->valid();//接口验证
$wechatobj->responsemsg();//调用回复消息方法
class wechatcallbackapitest
{
 public function valid()
 {
  $echostr = $_get["echostr"];

  //valid signature , option
  if($this->checksignature()){
   echo $echostr;
   exit;
  }
 }

 public function responsemsg()
 {
  //get post data, may be due to the different environments
  $poststr = $globals["http_raw_post_data"];

   //extract post data
  if (!empty($poststr)){
    /* libxml_disable_entity_loader is to prevent xml external entity injection,
     the best way is to check the validity of xml by yourself */
    libxml_disable_entity_loader(true);
     $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
    $fromusername = $postobj->fromusername;
    $tousername = $postobj->tousername;
    $keyword = trim($postobj->content);
    $time = time();
    $msgtype = $postobj->msgtype;//消息类型
    $event = $postobj->event;//时间类型,subscribe(订阅)、unsubscribe(取消订阅)
    
    $texttpl = "<xml>
       <tousername><![cdata[%s]]></tousername>
       <fromusername><![cdata[%s]]></fromusername>
       <createtime>%s</createtime>
       <msgtype><![cdata[%s]]></msgtype>
       <content><![cdata[%s]]></content>
       <funcflag>0</funcflag>
       </xml>"; 
       
    switch($msgtype){
     case "event":
     if($event=="subscribe"){
      $contentstr = "hi,欢迎关注海仙日用百货!"."\n"."回复数字'1',了解店铺地址."."\n"."回复数字'2',了解商品种类.";
     } 
     break;
     case "text"://文本消息
      switch($keyword){
       case "1":
       $contentstr = "店铺地址:"."\n"."杭州市江干区."; 
       break;
       case "2":
       $contentstr = "商品种类:"."\n"."杯子、碗、棉签、水桶、垃圾桶、洗碗巾(刷)、拖把、扫把、"
           ."衣架、粘钩、牙签、垃圾袋、保鲜袋(膜)、剪刀、水果刀、饭盒等.";
       break;
       default:
       $contentstr = "对不起,你的内容我会稍后回复";
      }
     break;
     case "voice"://语音消息
     //语音识别
     $recognition = $postobj->recognition;
     $format = $postobj->format;
     $contentstr = "你发送的是语音消息"."\n"."语音格式为:"."\n".$format."\n"."语音内容为:"."\n".$recognition;
     break;
    }
    $msgtype = "text";
    $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
    echo $resultstr;
  }else {
   echo "";
   exit;
  }
 }
  
 private function checksignature()
 {
  // you must define token by yourself
  if (!defined("token")) {
   throw new exception('token is not defined!');
  }
  
  $signature = $_get["signature"];
  $timestamp = $_get["timestamp"];
  $nonce = $_get["nonce"];
    
  $token = token;
  $tmparr = array($token, $timestamp, $nonce);
  // use sort_string rule
  sort($tmparr, sort_string);
  $tmpstr = implode( $tmparr );
  $tmpstr = sha1( $tmpstr );
  
  if( $tmpstr == $signature ){
   return true;
  }else{
   return false;
  }
 }
}


?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网