首先,先去微信公眾平臺注冊一個賬號(注冊要填的東西挺多的),注冊好之后,登錄進去。可以看到左側的“開發者中心”,開啟開發者中心前好像還要你完善一些資料,按照步驟完善即可。進入開發者中心之后,先去編輯
修改配置,修改配置的時候,注意:
url是你自己的域名下的php腳本(往下讀有該腳本的demo),該腳本用于和微信接口對接。比如 http://www.example.com/weixin.php
token是上述腳本里的定義的一個常量,比如你的php腳本里定義了:
define("token", "my_weixin");
那么,在填寫token時,你就填abcdefgh
encodingaeskey是消息加密用。你可以自己寫一個43為的數字和字母的組合,也可以選擇“隨機生成”,一般選擇隨機生成即可。
填好之后,保存(如果保存時,提示token驗證失敗,那么請確認token一致并多點幾次保存試試)。
保存好之后,點擊修改配置旁的:“開啟”。
然后,就可以編輯你的php腳本了。(如果你沒有自己的域名,可以使用新浪云的免費的sae,并最好完成實名認證)
demo的腳本如下:關注該公眾平臺(比如訂閱號之后),功能是:輸入hehe則返回hello world!!!如果輸入其他字符,則返回輸入heeh試試。
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
|
header( 'content-type:text/html;charset=utf-8' ); define( "token" , "my_weixin" ); //define your token $wx = new wechatcallbackapitest(); if ( $_get [ 'echostr' ]){ $wx ->valid(); //如果發來了echostr則進行驗證 } else { $wx ->responsemsg(); //如果沒有echostr,則返回消息 } class wechatcallbackapitest{ public function valid(){ //valid signature , option $echostr = $_get [ "echostr" ]; 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" ]; //接收微信發來的xml數據 //extract post data if (! empty ( $poststr )){ //解析post來的xml為一個對象$postobj $postobj = simplexml_load_string( $poststr , 'simplexmlelement' , libxml_nocdata); $fromusername = $postobj ->fromusername; //請求消息的用戶 $tousername = $postobj ->tousername; //"我"的公眾號id $keyword = trim( $postobj ->content); //消息內容 $time = time(); //時間戳 $msgtype = 'text' ; //消息類型:文本 $texttpl = "<xml> <tousername><![cdata[%s]]></tousername> <fromusername><![cdata[%s]]></fromusername> <createtime>%s</createtime> <msgtype><![cdata[%s]]></msgtype> <content><![cdata[%s]]></content> </xml>"; if ( $keyword == 'hehe' ){ $contentstr = 'hello world!!!' ; $resultstr = sprintf( $texttpl , $fromusername , $tousername , $time , $msgtype , $contentstr ); echo $resultstr ; exit (); } else { $contentstr = '輸入hehe試試' ; $resultstr = sprintf( $texttpl , $fromusername , $tousername , $time , $msgtype , $contentstr ); echo $resultstr ; exit (); } } else { echo "" ; exit ; } } //驗證字段 private function checksignature(){ $signature = $_get [ "signature" ]; $timestamp = $_get [ "timestamp" ]; $nonce = $_get [ "nonce" ]; $token = token; $tmparr = array ( $token , $timestamp , $nonce ); sort( $tmparr ); $tmpstr = implode( $tmparr ); $tmpstr = sha1( $tmpstr ); if ( $tmpstr == $signature ){ return true; } else { return false; } } } |
如果發送消息,系統提示:該公眾平臺暫時不能提共服務,請稍后再試。那么多半是代碼語法有問題,檢查好語法錯誤再試試。
附:
在新用戶關注你的公眾號時,自動返回信息:(把這段代碼加在判斷$keyword之前)。
1
2
3
4
5
6
7
8
|
if ( $postobj ->msgtype == 'event' ){ //如果xml信息里消息類型為event if ( $postobj ->event == 'subscribe' ){ //如果是訂閱事件 $contentstr = "歡迎訂閱misaka去年夏天!\n更多精彩內容:http://blog.csdn.net/misakaqunianxiatian" ; $resultstr = sprintf( $texttpl , $fromusername , $tousername , $time , $msgtype , $contentstr ); echo $resultstr ; exit (); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。