欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信公眾號(hào)開發(fā)之語(yǔ)音消息識(shí)別php代碼

 更新時(shí)間:2016年08月08日 08:33:33   作者:屠龍灬世家  
這篇文章主要為大家詳細(xì)介紹了微信公眾號(hào)開發(fā)之語(yǔ)音消息識(shí)別php代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了php微信語(yǔ)音消息識(shí)別代碼,供大家參考,具體內(nèi)容如下

1.開通語(yǔ)音識(shí)別(默認(rèn)關(guān)閉)

2.語(yǔ)音識(shí)別

請(qǐng)注意,開通語(yǔ)音識(shí)別后,用戶每次發(fā)送語(yǔ)音給公眾號(hào)時(shí),微信會(huì)在推送的語(yǔ)音消息XML數(shù)據(jù)包中,增加一個(gè)Recognition字段(注:由于客戶端緩存,開發(fā)者開啟或者關(guān)閉語(yǔ)音識(shí)別功能,對(duì)新關(guān)注者立刻生效,對(duì)已關(guān)注用戶需要24小時(shí)生效。開發(fā)者可以重新關(guān)注此帳號(hào)進(jìn)行測(cè)試)。開啟語(yǔ)音識(shí)別后的語(yǔ)音XML數(shù)據(jù)包如下:

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

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();//接口驗(yàn)證
$wechatObj->responseMsg();//調(diào)用回復(fù)消息方法
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;//時(shí)間類型,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,歡迎關(guān)注海仙日用百貨!"."\n"."回復(fù)數(shù)字'1',了解店鋪地址."."\n"."回復(fù)數(shù)字'2',了解商品種類.";
     } 
     break;
     case "text"://文本消息
      switch($keyword){
       case "1":
       $contentStr = "店鋪地址:"."\n"."杭州市江干區(qū)."; 
       break;
       case "2":
       $contentStr = "商品種類:"."\n"."杯子、碗、棉簽、水桶、垃圾桶、洗碗巾(刷)、拖把、掃把、"
           ."衣架、粘鉤、牙簽、垃圾袋、保鮮袋(膜)、剪刀、水果刀、飯盒等.";
       break;
       default:
       $contentStr = "對(duì)不起,你的內(nèi)容我會(huì)稍后回復(fù)";
      }
     break;
     case "voice"://語(yǔ)音消息
     //語(yǔ)音識(shí)別
     $recognition = $postObj->Recognition;
     $format = $postObj->Format;
     $contentStr = "你發(fā)送的是語(yǔ)音消息"."\n"."語(yǔ)音格式為:"."\n".$format."\n"."語(yǔ)音內(nèi)容為:"."\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;
  }
 }
}


?>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論