服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - 微信公众号开发之文本消息自动回复php代码

微信公众号开发之文本消息自动回复php代码

2021-02-22 14:48屠龙灬世家 PHP教程

这篇文章主要为大家详细介绍了微信公众号开发之文本消息自动回复php代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

1.php示例代码下载

 下载地址:https://mp.weixin.qq.com/wiki/home/index.html(开始开发-》接入指南-》php示例代码下载) 

微信公众号开发之文本消息自动回复php代码

2.wx_sample.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
<?php
/**
 * wechat php test
 */
 
//define your token
define("token", "weixin");
$wechatobj = new wechatcallbackapitest();
$wechatobj->valid();
 
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();
 $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>";
 if(!empty( $keyword ))
 {
  $msgtype = "text";
  $contentstr = "welcome to wechat world!";
  $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
  echo $resultstr;
 }else{
  echo "input something...";
 }
 
 }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;
 }
 }
}
 
?>

3.调用回复信息方法
 在wx_sample.php文件中注释掉$wechatobj->valid();,在其下增加一句“$wechatobj->responsemsg();”。

?
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
<?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();
 $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>";
 if(!empty( $keyword ))
 {
  $msgtype = "text";
  $contentstr = "welcome to wechat world!";
  $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
  echo $resultstr;
 }else{
  echo "input something...";
 }
 
 }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;
 }
 }
}
 
?>

4.关键词自动回复和关注回复
 $keyword保存着用户微信端发来的文本信息。
 官方开发者文档:https://mp.weixin.qq.com/wiki/home/index.html(消息管理-》接收消息-接收事件推送-》1.关注/取消关注事件)

微信公众号开发之文本消息自动回复php代码

关注事件与一般的文本消息有两处不同,一是msgtype值是event,二是增加了event值是subscribe。由于官方文档(最初的wx_sample.php)没有提取这个参数,需要我们自己提取。在程序中增加两个变量$msgtype和$event。

?
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
<?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"."杭州市江干艮山西路233号新东升市场地下室第一排.";
  break;
  case "2":
  $contentstr = "商品种类:"."\n"."杯子、碗、棉签、水桶、垃圾桶、洗碗巾(刷)、拖把、扫把、"
   ."衣架、粘钩、牙签、垃圾袋、保鲜袋(膜)、剪刀、水果刀、饭盒等.";
  break;
  default:
  $contentstr = "对不起,你的内容我会稍后回复";
  }
  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;
 }
 }
}
 
 
?>

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

延伸 · 阅读

精彩推荐