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

2025-05-29 0 25

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

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

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

2.语音识别

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

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113
<?php

/**

* wechat php test

*/

//define your token

define("token", "weixin");

$wechatobj = new wechatcallbackapitest();

//$wechatobj->valid();//接口验证

$wechatobj->responsemsg();//调用回复消息方法

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,欢迎关注海仙日用百货!"."\\n"."回复数字'1',了解店铺地址."."\\n"."回复数字'2',了解商品种类.";

}

break;

case "text"://文本消息

switch($keyword){

case "1":

$contentstr = "店铺地址:"."\\n"."杭州市江干区.";

break;

case "2":

$contentstr = "商品种类:"."\\n"."杯子、碗、棉签、水桶、垃圾桶、洗碗巾(刷)、拖把、扫把、"

."衣架、粘钩、牙签、垃圾袋、保鲜袋(膜)、剪刀、水果刀、饭盒等.";

break;

default:

$contentstr = "对不起,你的内容我会稍后回复";

}

break;

case "voice"://语音消息

//语音识别

$recognition = $postobj->recognition;

$format = $postobj->format;

$contentstr = "你发送的是语音消息"."\\n"."语音格式为:"."\\n".$format."\\n"."语音内容为:"."\\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;

}

}

}

?>

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 微信公众号开发之语音消息识别php代码 https://www.kuaiidc.com/96360.html

相关文章

发表评论
暂无评论