微信公眾號開發(fā)之語音消息識別php代碼
本文實例為大家分享了php微信語音消息識別代碼,供大家參考,具體內(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();//接口驗證
$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;
}
}
}
?>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用PHP+Redis實現(xiàn)延遲任務(wù),實現(xiàn)自動取消訂單功能
這篇文章主要介紹了用PHP+Redis實現(xiàn)延遲任務(wù),實現(xiàn)自動取消訂單功能,通過業(yè)務(wù)場景給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
使用php+swoole對client數(shù)據(jù)實時更新(一)
這篇文章主要介紹了使用php+swoole對client數(shù)據(jù)實時更新(一) 的相關(guān)資料,需要的朋友可以參考下2016-01-01
ThinkPHP3.2.3框架實現(xiàn)執(zhí)行原生SQL語句的方法示例
這篇文章主要介紹了ThinkPHP3.2.3框架實現(xiàn)執(zhí)行原生SQL語句的方法,結(jié)合實例形式分析了thinkPHP3.2.3框架針對查詢、添加、修改、刪除等原生SQL操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-04-04
PHP安裝threads多線程擴(kuò)展基礎(chǔ)教程
php5.3或以上,且為線程安全版本。apache和php使用的編譯器必須一致,通過phpinfo()查看Thread Safety為enabled則為線程安全版,通過phpinfo()查看Compiler項可以知道使用的編譯器,本文給大家介紹PHP安裝threads多線程擴(kuò)展基礎(chǔ)教程,需要的朋友參考下2015-11-11

