一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - 微信公眾號開發(fā)之文本消息自動回復(fù)php代碼

微信公眾號開發(fā)之文本消息自動回復(fù)php代碼

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

這篇文章主要為大家詳細介紹了微信公眾號開發(fā)之文本消息自動回復(fù)php代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了php微信文本消息自動回復(fù) 別代碼,供大家參考,具體內(nèi)容如下

1.php示例代碼下載

 下載地址:https://mp.weixin.qq.com/wiki/home/index.html(開始開發(fā)-》接入指南-》php示例代碼下載) 

微信公眾號開發(fā)之文本消息自動回復(fù)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.調(diào)用回復(fù)信息方法
 在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();//調(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();
 $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.關(guān)鍵詞自動回復(fù)和關(guān)注回復(fù)
 $keyword保存著用戶微信端發(fā)來的文本信息。
 官方開發(fā)者文檔:https://mp.weixin.qq.com/wiki/home/index.html(消息管理-》接收消息-接收事件推送-》1.關(guān)注/取消關(guān)注事件)

微信公眾號開發(fā)之文本消息自動回復(fù)php代碼

關(guān)注事件與一般的文本消息有兩處不同,一是msgtype值是event,二是增加了event值是subscribe。由于官方文檔(最初的wx_sample.php)沒有提取這個參數(shù),需要我們自己提取。在程序中增加兩個變量$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();//調(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"."杭州市江干艮山西路233號新東升市場地下室第一排.";
  break;
  case "2":
  $contentstr = "商品種類:"."\n"."杯子、碗、棉簽、水桶、垃圾桶、洗碗巾(刷)、拖把、掃把、"
   ."衣架、粘鉤、牙簽、垃圾袋、保鮮袋(膜)、剪刀、水果刀、飯盒等.";
  break;
  default:
  $contentstr = "對不起,你的內(nèi)容我會稍后回復(fù)";
  }
  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)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 爽好舒服把腿张小说 | 国产成人一区二区三区小说 | 性xxx免费视频 | 男人看的网址 | 四虎影院在线免费播放 | 亚洲伦理影院 | 欧美zoosex| 办公室恋情在线观看 | 乌克兰黄色录像 | 17个农民工婉莹第一部 | 99视频在线免费 | 午夜亚洲国产 | 欧美╳bbbb | 99久久综合精品免费 | 精品亚洲综合久久中文字幕 | 四虎影院免费在线播放 | 久久毛片视频 | 天天操免费视频 | 日本天堂视频在线观看 | 狠狠躁夜夜躁人人爽天天miya | 欧美va在线播放免费观看 | 国产福利一区二区三区 | 国产成人免费片在线视频观看 | 四虎1515h永久| 污污免费 | 桥本有菜在线四虎福利网 | 成人a级特黄毛片 | 国人精品视频在线观看 | 跪在老师脚下吃丝袜脚 | 第一次出血videos | 办公室强行丝袜秘书啪啪 | 喷潮女王cytherea全部视频 | 国产精品伊人 | 女人和拘做受全过程免费 | 亚洲 欧美 清纯 校园 另类 | 亲爱的客栈第二季免费观看完整版 | 五月最新女厕所高跟嘘嘘 | a级精品九九九大片免费看 a级动漫 | 魔法满屋免费观看完整版中文 | 狠狠的撞进去嗯啊h女强男视频 | 色婷婷综合缴情综六月 |