激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - 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;
 }
 }
}
 
 
?>

 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美自拍三区 | 国产日韩线路一线路二 | 不卡国产一区二区三区四区 | 永久免费不卡在线观看黄网站 | 91精品福利视频 | 久久网站热最新地址4 | 欧美顶级毛片在线播放小说 | 国产精品探花在线观看 | 久久久久久久久日本理论电影 | 国产高潮失禁喷水爽到抽搐视频 | 久草在线视频在线 | 国产毛片在线高清视频 | 久久丝袜脚交足黄网站免费 | 在线成人www免费观看视频 | 日本在线视频免费 | 中国漂亮护士一级a毛片 | 黄色片免费在线播放 | 成人毛片100部免费观看 | 草莓视频久久 | 久久久久久久亚洲精品 | 欧美中文字幕一区二区三区亚洲 | 国产又粗又爽又深的免费视频 | 久久久久久久久久久一区 | 中文字幕在线观看视频一区 | 久久精品在线免费观看 | av电影观看 | 国产亚洲精彩视频 | 成年性羞羞视频免费观看 | 国产精品一区二区手机在线观看 | 国产午夜精品久久久久久久蜜臀 | 免费看综艺策驰影院 | 中国免费黄色 | 午夜精品成人一区二区 | 午夜人体| 斗罗破苍穹在线观看免费完整观看 | 依人九九宗合九九九 | 日本成人一区二区 | 欧美视频99 | 成人一级黄色片 | 92看片淫黄大片欧美看国产片 | 久久久久国产一区二区三区不卡 |