本文實(shí)例講述了php版微信公眾平臺接口開發(fā)之智能回復(fù)功能實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
智能回復(fù)是根據(jù)用戶輸入的條件來反饋結(jié)果用用戶了,這個(gè)小編以前有做過信整理了一些例子供各位參考,比較完整主要是介紹在開發(fā)端了。
微信自推出后,著實(shí)火了一把,而支付功能的推出,又把微信推到了一個(gè)無可比擬的高度,然后申請微信訂閱號或者服務(wù)號的人也開始比肩接踵。下面我將給大家簡單講解下微信公眾平臺開發(fā)接口。
先去 微信公眾平臺 申請賬號,然后按照提示一步步。在選擇訂閱號和服務(wù)號上,個(gè)人只能申請訂閱號,而且局限于基礎(chǔ)功能;而企業(yè)兩者都可以申請。訂閱號和服務(wù)號的區(qū)別在于:訂閱號可以每天群發(fā)一條消息,而服務(wù)號一個(gè)月才能群發(fā)一條;訂閱號需要微信認(rèn)證才能自定義菜單(企業(yè)才能認(rèn)證,認(rèn)證300元一次),而服務(wù)號則一開始就有自定義菜單,但是也可以認(rèn)證,認(rèn)證后服務(wù)號直接升級高級功能。更多差異請百度...
我申請的是訂閱號,因?yàn)槭莻€(gè)人。只要傳一張手捧身份證的人頭照就可以了,雖然有點(diǎn)傻。然后等待信息登記審核(一天左右時(shí)間)。通過后直接進(jìn)入 微信公眾平臺 ,點(diǎn)擊功能進(jìn)入高級功能,關(guān)閉編輯模式,開啟開發(fā)模式,然后下載微信提供的demo,解壓,就一個(gè)文件: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
|
<?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 (!emptyempty( $postStr )){ $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 (!emptyempty( $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() { $signature = $_GET [ "signature" ]; $timestamp = $_GET [ "timestamp" ]; $nonce = $_GET [ "nonce" ]; $token = TOKEN; $tmpArr = array ( $token , $timestamp , $nonce ); sort( $tmpArr , SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if ( $tmpStr == $signature ){ return true; } else { return false; } } } ?> |
其實(shí)就是認(rèn)證,然后發(fā)送消息。將文件傳到你的服務(wù)器上,我放在根目錄下,然后修改開發(fā)模式下的url和token值。假設(shè)這里使用的url是http://m.ythuaji.com.cn/wx_sample.php,token就是上面define的token,這個(gè)可以改的,只要兩邊保持一致,默認(rèn)是weixin。然后點(diǎn)提交,就會提示你成功了。然后掃下你申請的號碼,發(fā)個(gè)消息,你會發(fā)現(xiàn)沒反應(yīng),這個(gè)時(shí)候我們需要小調(diào)整一下,關(guān)閉接口文檔中調(diào)用認(rèn)證的方法,開啟調(diào)用處理回復(fù)信息的方法:
1
2
|
//$wechatObj->valid(); $wechatObj ->responseMsg(); |
這個(gè)時(shí)候你再發(fā)個(gè)消息,你就會收到:Welcome to wechat world!
是不是在關(guān)注了有些訂閱號或者服務(wù)號之后,馬上會收到一條消息。什么回復(fù)1,怎樣怎樣;回復(fù)2,怎樣怎樣之類的。
拿我自己的博客舉例,我的關(guān)注語是:
感謝您關(guān)注AndyYang個(gè)人博客微信小助手。
回復(fù)【1】返回兩篇最新文章
回復(fù)【2】返回兩篇人氣文章
回復(fù)【3】返回兩篇熱評文章
回復(fù)【4】返回兩篇最新技術(shù)文章
回復(fù)【5】返回兩篇最新寫作文章
回復(fù)其他返回搜索關(guān)鍵字的兩篇文章
更多精彩內(nèi)容,盡在:m.ythuaji.com.cn。親們,請多多支持哦,謝謝~
那這個(gè)怎么實(shí)現(xiàn)呢?直接上代碼:
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
|
<?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 )){ $postObj = simplexml_load_string( $postStr , 'SimpleXMLElement' , LIBXML_NOCDATA); $fromUsername = $postObj ->FromUserName; $toUsername = $postObj ->ToUserName; $keyword = trim( $postObj ->Content); $time = time(); $MsgType = $postObj ->MsgType; //add $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 ( $MsgType != 'event' ) { if (! empty ( $keyword )) { $msgType = "text" ; $contentStr = "Welcome to wechat world!" ; } else { echo "Input something..." ; } } else { $msgType = "text" ; $contentStr = "感謝您關(guān)注AndyYang個(gè)人博客微信小助手。\r\n" . "回復(fù)【1】返回兩篇最新文章\r\n" . "回復(fù)【2】返回兩篇人氣文章\r\n" . "回復(fù)【3】返回兩篇熱評文章\r\n" . "回復(fù)【4】返回兩篇最新技術(shù)文章\r\n" . "回復(fù)【5】返回兩篇最新寫作文章\r\n" . "回復(fù)其他返回搜索關(guān)鍵字的兩篇文章\r\n" . ; } $resultStr = sprintf( $textTpl , $fromUsername , $toUsername , $time , $msgType , $contentStr ); echo $resultStr ; } else { echo "" ; exit ; } } private function checkSignature() { $signature = $_GET [ "signature" ]; $timestamp = $_GET [ "timestamp" ]; $nonce = $_GET [ "nonce" ]; $token = TOKEN; $tmpArr = array ( $token , $timestamp , $nonce ); sort( $tmpArr , SORT_STRING); //這個(gè)在新的sdk中添加了第二個(gè)參數(shù)(compare items as strings) $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if ( $tmpStr == $signature ){ return true; } else { return false; } } } |
當(dāng)然這里只是簡單的實(shí)現(xiàn)下,在微信公眾平臺提供的sdk上做簡單的修改,實(shí)際上msgtype類型很多,就算消息類型為event的,它里面也有subscribe、LOCATION等,而如果細(xì)化的話,就用Event為subscribe來處理初次關(guān)注的事件,代碼如下:
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
|
<?php define( "TOKEN" , "weixin" ); $wechatObj = new wechatCallbackapiTest(); $wechatObj ->weixin_run(); class wechatCallbackapiTest { private $fromUsername ; private $toUsername ; private $times ; private $keyword ; private $MsgType ; public function responseMsg() { $postStr = $GLOBALS [ "HTTP_RAW_POST_DATA" ]; if (!emptyempty( $postStr )) { $postObj = simplexml_load_string( $postStr , 'SimpleXMLElement' , LIBXML_NOCDATA); $this ->fromUsername = $postObj ->FromUserName; $this ->toUsername = $postObj ->ToUserName; $this ->keyword = trim( $postObj ->Content); $this ->time = time(); $this ->MsgType = $postObj ->MsgType; } else { echo "Pay attention to <a href='http://{$_SERVER['HTTP_HOST']}'>http://{$_SERVER['HTTP_HOST']}</a>,thanks!" ; exit ; } } public function weixin_run() { $this ->responseMsg(); if ( $this ->MsgType != 'event' ) { //attention $data = $this ->getData(); $this ->fun_xml( "news" , $data , count ( $data )); } else { $data = $this ->getWelData(); $this ->fun_xml( "text" , $data , 1); } } //type: text 文本類型, news 圖文類型 //text,array(內(nèi)容),array(ID) //news,array(array(標(biāo)題,介紹,圖片,超鏈接),...小于10條),條數(shù) private function fun_xml( $type , $value_arr , $count ) { $con ="<xml> <ToUserName><![CDATA[{ $this ->fromUsername}]]></ToUserName> <FromUserName><![CDATA[{ $this ->toUsername}]]></FromUserName> <CreateTime>{ $this ->times}</CreateTime> <MsgType><![CDATA[{ $type }]]></MsgType>"; switch ( $type ) { case "text" : $con .= "<Content><![CDATA[$value_arr]]></Content>" ; break ; case "news" : $con .="<ArticleCount>{ $count }</ArticleCount> <Articles>"; foreach ( $value_arr as $key => $v ) { $con .="<item> <Title><![CDATA[{ $v [0]}]]></Title> <Description><![CDATA[{ $v [1]}]]></Description> <PicUrl><![CDATA[{ $v [2]}]]></PicUrl> <Url><![CDATA[{ $v [3]}]]></Url> </item>"; } $con .= "</Articles>" ; break ; } echo $con . "</xml>" ; } private function getData() { //數(shù)據(jù)庫通過關(guān)鍵字查詢文章 //。。。。。。。。。。。。 //。。。。。。。。。。。。 //返回文章結(jié)果的數(shù)組 return $data ; } private function getWelData() { $data = "感謝您關(guān)注AndyYang個(gè)人博客微信小助手。\r\n" . "回復(fù)【1】返回兩篇最新文章\r\n" . "回復(fù)【2】返回兩篇人氣文章\r\n" . "回復(fù)【3】返回兩篇熱評文章\r\n" . "回復(fù)【4】返回兩篇最新技術(shù)文章\r\n" . "回復(fù)【5】返回兩篇最新寫作文章\r\n" . "回復(fù)其他返回搜索關(guān)鍵字的兩篇文章\r\n" . "更多精彩內(nèi)容,盡在:<a href='http://m.ythuaji.com.cn/'>m.ythuaji.com.cn</a>。親們,請多多支持哦,謝謝~" ; ; return $data ; } } |
老實(shí)說很想弄個(gè)服務(wù)號玩玩,自定義菜單是沒什么技術(shù)含量的,但是后面的微信支付之類,服務(wù)號僅有的服務(wù),還是挺值得去嘗試下的.
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。