当前位置:Gxlcms > php框架 > 微信公众号开发之语音消息识别php代码

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

时间:2021-07-01 10:21:17 帮助过:246人阅读

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

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

2.语音识别

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

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

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

人气教程排行