轉 微信Token驗證程式碼的實現 php
微信開放第三方API介面,
申請地址:
官方提供PHP開發程式碼下載: 點此下載
如果你的微信介面頁面沒有做好,提交儲存的時候,會提示:驗證Token失敗。這個錯誤是因為,你的介面頁面還沒有反饋正確的資訊給微信介面。微信在教程方面還做得不夠成熟,就一個PHP示例,還是有問題的,在下篇文章我會講到這個錯誤在哪。微信官方也沒有跟大家說清楚怎麼才能是token驗證成功。下面我將給出示例告訴大家如何通過token驗證。
譬如:微信介面頁面是http://bbhet.com 預設頁面是weixin.php,我們只需要把weixin.php的程式碼改為, 然後放到你得網站根目錄:
官方下載地址 http://mp.weixin.qq.com/wiki/8/f9a0b8382e0b77d87b3bcc1ce6fbc104.html
方法一: 簡單的PHP實現Token驗證
- <?php
- //1. 將timestamp , nonce , token 按照字典排序
- $timestamp = $_GET['timestamp'];
- $nonce = $_GET['nonce'];
- $token = "你自定義的Token值 用於驗證";
- $signature = $_GET['signature'
- $array = array($timestamp,$nonce,$token);
- sort($array);
- //2.將排序後的三個引數拼接後用sha1加密
- $tmpstr = implode('',$array);
- $tmpstr = sha1($tmpstr);
- //3. 將加密後的字串與 signature 進行對比, 判斷該請求是否來自微信
- if($tmpstr == $signature)
- {
- echo $_GET['echostr'];
- exit;
- }
方法二: 封裝類的程式碼實現Token驗證
[php] view plain copy
- <?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)){
- $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()
- {
- $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;
- }
- }
- }
- ?>
微信開放第三方API介面,
申請地址:
官方提供PHP開發程式碼下載: 點此下載
如果你的微信介面頁面沒有做好,提交儲存的時候,會提示:驗證Token失敗。這個錯誤是因為,你的介面頁面還沒有反饋正確的資訊給微信介面。微信在教程方面還做得不夠成熟,就一個PHP示例,還是有問題的,在下篇文章我會講到這個錯誤在哪。微信官方也沒有跟大家說清楚怎麼才能是token驗證成功。下面我將給出示例告訴大家如何通過token驗證。
譬如:微信介面頁面是http://bbhet.com 預設頁面是weixin.php,我們只需要把weixin.php的程式碼改為, 然後放到你得網站根目錄:
官方下載地址 http://mp.weixin.qq.com/wiki/8/f9a0b8382e0b77d87b3bcc1ce6fbc104.html
方法一: 簡單的PHP實現Token驗證
- <?php
- //1. 將timestamp , nonce , token 按照字典排序
- $timestamp = $_GET['timestamp'];
- $nonce = $_GET['nonce'];
- $token = "你自定義的Token值 用於驗證";
- $signature = $_GET['signature'];
- $array = array($timestamp,$nonce,$token);
- sort($array);
- //2.將排序後的三個引數拼接後用sha1加密
- $tmpstr = implode('',$array);
- $tmpstr = sha1($tmpstr);
- //3. 將加密後的字串與 signature 進行對比, 判斷該請求是否來自微信
- if($tmpstr == $signature)
- {
- echo $_GET['echostr'];
- exit;
- }
方法二: 封裝類的程式碼實現Token驗證
[php] view plain copy
- <?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)){
- $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()
- {
- $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;
- }
- }
- }
- ?>