微信小程式訊息推送php伺服器驗證

PHPz
發布: 2017-04-04 11:42:56
原創
2645 人瀏覽過

設定頁面(「設定」>>「開發設定」):


  1. 設定伺服器網域名稱
    例如:https://hosts.com
    注意http和https協定的差異。

  2. 設定訊息推送
    2.1 在你的伺服器裡新增伺服器介面test.php,test.php介面內容主要是透過token驗證訊息是否為微信寄來的,程式碼參考官方的例子:
    define("TOKEN","xxxxx");/ 後台填寫的token<br>$wechatObj = <a href="http://www.php.cn/wiki/165.html" target="_blank">new</a> wechatAPI(); <br>$wechatObj->isValid();<br><a href="http://www.php.cn/wiki/164.html" target="_blank">class</a> wechatAPI<br>{<br>public function isValid()//驗證微信接口,如果確認是微信就返回它傳來的<a href="http://www.php.cn/wiki/1343.html" target="_blank">echo</a>str參數<br>{<br> $echoStr = $_GET["echostr"];<br> <a href="http://www.php.cn/wiki/109.html" target="_blank">if</a> ($this->checkSignature()) {<br>   echo $echoStr;<br>   exit;<br> }<br>}<br>private function checkSignature()   //官方的驗證<a href="http://www.php.cn/wiki/145.html" target="_blank">函數</a>##{<br># $signature = $ _GET["signature"];<br> $<br>time<a href="http://www.php.cn/wiki/1268.html" target="_blank">stamp = $_GET["timestamp"];</a> $nonce = $_GET["nonce"];<br> $token = TOKEN; <br> $tmpArr = <br>array<a href="http://www.php.cn/wiki/58.html" target="_blank">($token, $timestamp, $nonce);</a> <br>sort<a href="http://www.php.cn/wiki/1087.html" target="_blank">($tmpArr, SORT_</a>STRING<a href="http://www.php.cn/wiki/57.html" target="_blank">);</a> $tmpStr = <br>implode<a href="http://www.php.cn/wiki/1336.html" target="_blank">( $tmpArr );</a> $tmpStr = <br>sha1<a href="http://www.php.cn/wiki/1366.html" target="_blank">( $tmpStr );</a> if( $tmpStr == $signature ){<br> <br>return<a href="http://www.php.cn/wiki/135.html" target="_blank"> true;</a> }<br>else<a href="http://www.php.cn/wiki/111.html" target="_blank">{</a>   return false;<br> }<br>}<br>};<br>2.2 設定小程式後台訊息推送相關資訊
    URL(伺服器位址):https://hosts.com/xx/test.php
    Token:  任意符合規範的
    字串#,如上面定義的" xxxxx"#​​##EncodingAESKey
    (訊息加密金鑰):(隨機生成,自己保存就好了,這個加解密才用)訊息加密方式:暫時選擇明文,不用去考慮加密解密的問題。 資料格式:依需求選擇。
    提交,如果沒有問題就會成功了。 (如果有問題你可以用假的資料通過瀏覽器測試)

    #

以上是微信小程式訊息推送php伺服器驗證的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!