本文實(shí)例為大家分享了php微信文本消息自動(dòng)回復(fù) 別代碼,供大家參考,具體內(nèi)容如下
1.php示例代碼下載
下載地址:https://mp.weixin.qq.com/wiki/home/index.html(開始開發(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)注事件)
關(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ù)器之家。