国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

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

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

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

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

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

1.php示例代碼下載

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

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

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

關(guān)注事件與一般的文本消息有兩處不同,一是msgtype值是event,二是增加了event值是subscribe。由于官方文檔(最初的wx_sample.php)沒有提取這個(gè)參數(shù),需要我們自己提取。在程序中增加兩個(gè)變量$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();//接口驗(yàn)證
$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;//時(shí)間類型,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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 偷拍自拍第一页 | 欧美一区在线视频 | 亚洲成人激情在线观看 | 亚洲精品国产综合 | 精品在线 | 久久久久国产精品免费免费搜索 | 国产精品久久久久久久久免费高清 | 黄色一级片在线观看 | 久草视频免费看 | 91中文字幕在线观看 | 求av网址| 亚洲欧美日韩国产综合 | 黄色av影院| 亚洲免费视频观看 | 国产中文字幕在线播放 | 九九精品视频在线观看 | 亚洲午夜激情 | 日韩精品专区在线影院重磅 | 久久久精 | 99成人| 久久人成 | 91亚洲精品 | 久久精品在线 | 欧美中文字幕一区二区三区 | 婷婷精品久久久久久久久久不卡 | 欧美日韩亚洲一区二区 | 一级黄色在线观看 | 亚洲精品久久久久久久久久吃药 | 四季久久免费一区二区三区四区 | 久久青草国产 | 免费黄色大片 | 亚洲成人日韩在线 | 久久中文字幕一区二区三区 | 中文字幕久久精品 | 国产另类ts人妖一区二区 | 亚洲 中文 欧美 日韩 在线观看 | 国产在线视频网站 | 亚洲青青草| 日韩久久精品一区二区 | 亚洲国产精品美女 | 日韩成人中文字幕 |