微信小程序 消息推送php服務(wù)器驗證實例詳解
微信文檔(靠下有個“接入指引”):https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/callback_help.html
設(shè)置頁面(“設(shè)置”>>“開發(fā)設(shè)置”):
https://mp.weixin.qq.com/wxopen/initprofile?action=home&lang=zh_CN
1.設(shè)置服務(wù)器域名
比如:https://hosts.com
注意http和https協(xié)議的不同。
2.設(shè)置消息推送
2.1 在你的服務(wù)器里添加服務(wù)器接口test.php,test.php接口內(nèi)容主要是通過token驗證消息是否為微信發(fā)來的,代碼參照官方的例子:
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
|
define( "TOKEN" , "xxxxx" );/ 后臺填寫的token $wechatObj = new wechatAPI(); $wechatObj->isValid(); class wechatAPI { public function isValid() //驗證微信接口,如果確認是微信就返回它傳來的echostr參數(shù) { $echoStr = $_GET[ "echostr" ]; if ($ this ->checkSignature()) { echo $echoStr; exit; } } private function checkSignature() //官方的驗證函數(shù) { $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 ; } } }; |
2.2 設(shè)置小程序后臺消息推送相關(guān)信息
URL(服務(wù)器地址):https://hosts.com/xx/test.php
Token: 任意符合規(guī)范的字符串,如上面定義的"xxxxx"
EncodingAESKey(消息加密密鑰):(隨機生成,自己保存就好了,這個加解密才用)
消息加密方式:暫時選擇明文,不用去考慮加密解密的問題。
數(shù)據(jù)格式:根據(jù)需求選擇。
提交,如果沒有問題就會成功了。(如果有問題你可以用假的數(shù)據(jù)通過瀏覽器測試)
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://www.jianshu.com/p/2b9773592eb6