程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 微信公眾號開發之語音消息識別,公眾語音

微信公眾號開發之語音消息識別,公眾語音

編輯:關於PHP編程

微信公眾號開發之語音消息識別,公眾語音


1.開通語音識別(默認關閉)

2.語音識別

請注意,開通語音識別後,用戶每次發送語音給公眾號時,微信會在推送的語音消息XML數據包中,增加一個Recognition字段(注:由於客戶端緩存,開發者開啟或者關閉語音識別功能,對新關注者立刻生效,對已關注用戶需要24小時生效。開發者可以重新關注此帳號進行測試)。開啟語音識別後的語音XML數據包如下:

1 <?php 2 /** 3 * wechat php test 4 */ 5 6 //define your token 7 define("TOKEN", "weixin"); 8 $wechatObj = new wechatCallbackapiTest(); 9 //$wechatObj->valid();//接口驗證 10 $wechatObj->responseMsg();//調用回復消息方法 11 class wechatCallbackapiTest 12 { 13 public function valid() 14 { 15 $echoStr = $_GET["echostr"]; 16 17 //valid signature , option 18 if($this->checkSignature()){ 19 echo $echoStr; 20 exit; 21 } 22 } 23 24 public function responseMsg() 25 { 26 //get post data, May be due to the different environments 27 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 28 29 //extract post data 30 if (!empty($postStr)){ 31 /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, 32 the best way is to check the validity of xml by yourself */ 33 libxml_disable_entity_loader(true); 34 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 35 $fromUsername = $postObj->FromUserName; 36 $toUsername = $postObj->ToUserName; 37 $keyword = trim($postObj->Content); 38 $time = time(); 39 $msgType = $postObj->MsgType;//消息類型 40 $event = $postObj->Event;//時間類型,subscribe(訂閱)、unsubscribe(取消訂閱) 41 42 $textTpl = "<xml> 43 <ToUserName><![CDATA[%s]]></ToUserName> 44 <FromUserName><![CDATA[%s]]></FromUserName> 45 <CreateTime>%s</CreateTime> 46 <MsgType><![CDATA[%s]]></MsgType> 47 <Content><![CDATA[%s]]></Content> 48 <FuncFlag>0</FuncFlag> 49 </xml>"; 50 51 switch($msgType){ 52 case "event": 53 if($event=="subscribe"){ 54 $contentStr = "Hi,歡迎關注海仙日用百貨!"."\n"."回復數字'1',了解店鋪地址."."\n"."回復數字'2',了解商品種類."; 55 } 56 break; 57 case "text"://文本消息 58 switch($keyword){ 59 case "1": 60 $contentStr = "店鋪地址:"."\n"."杭州市江干區."; 61 break; 62 case "2": 63 $contentStr = "商品種類:"."\n"."杯子、碗、棉簽、水桶、垃圾桶、洗碗巾(刷)、拖把、掃把、" 64 ."衣架、粘鉤、牙簽、垃圾袋、保鮮袋(膜)、剪刀、水果刀、飯盒等."; 65 break; 66 default: 67 $contentStr = "對不起,你的內容我會稍後回復"; 68 } 69 break; 70 case "voice"://語音消息 71 //語音識別 72 $recognition = $postObj->Recognition; 73 $format = $postObj->Format; 74 $contentStr = "你發送的是語音消息"."\n"."語音格式為:"."\n".$format."\n"."語音內容為:"."\n".$recognition; 75 break; 76 } 77 $msgType = "text"; 78 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 79 echo $resultStr; 80 }else { 81 echo ""; 82 exit; 83 } 84 } 85 86 private function checkSignature() 87 { 88 // you must define TOKEN by yourself 89 if (!defined("TOKEN")) { 90 throw new Exception('TOKEN is not defined!'); 91 } 92 93 $signature = $_GET["signature"]; 94 $timestamp = $_GET["timestamp"]; 95 $nonce = $_GET["nonce"]; 96 97 $token = TOKEN; 98 $tmpArr = array($token, $timestamp, $nonce); 99 // use SORT_STRING rule 100 sort($tmpArr, SORT_STRING); 101 $tmpStr = implode( $tmpArr ); 102 $tmpStr = sha1( $tmpStr ); 103 104 if( $tmpStr == $signature ){ 105 return true; 106 }else{ 107 return false; 108 } 109 } 110 } 111 112 113 ?> 語音識別

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved