您好,登錄后才能下訂單哦!
這篇文章主要介紹php如何實(shí)現(xiàn)微信公眾號開發(fā)之語音消息識別,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
具體內(nèi)容如下
1.開通語音識別(默認(rèn)關(guān)閉)
2.語音識別
請注意,開通語音識別后,用戶每次發(fā)送語音給公眾號時,微信會在推送的語音消息XML數(shù)據(jù)包中,增加一個Recognition字段(注:由于客戶端緩存,開發(fā)者開啟或者關(guān)閉語音識別功能,對新關(guān)注者立刻生效,對已關(guān)注用戶需要24小時生效。開發(fā)者可以重新關(guān)注此帳號進(jìn)行測試)。開啟語音識別后的語音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;//時間類型,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 = "對不起,你的內(nèi)容我會稍后回復(fù)"; } break; case "voice"://語音消息 //語音識別 $recognition = $postObj->Recognition; $format = $postObj->Format; $contentStr = "你發(fā)送的是語音消息"."\n"."語音格式為:"."\n".$format."\n"."語音內(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; } } } ?>
以上是“php如何實(shí)現(xiàn)微信公眾號開發(fā)之語音消息識別”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。