微信開發最近要用到的一個功能,其實就是一個非常的簡單的用戶輸入然后自動搜索數據庫并進行一個數據回復了,這個與官方沒多大的問題,但小編就微信Token驗證失敗折騰了許多,下面解決了給各位分析一下.
1.Token驗證失敗
這個就是要檢查配置文件了,最基本的就是
define("TOKEN", "weixin"); weixin 是你的微信開發后臺的ID
微信開發Token驗證失敗或請求URL超時問題解決辦法
2.請求URL超時
這個沒什么辦法多提交幾次了,這個還有就是服務器安裝了安全狗之類的軟件把微信IP給攔截了,可以檢查一下。
3.官方下載一個wechatCallbackapiTest類然后進行一下操作即可,代碼如下:
- define("TOKEN", "weixin");
- $wechatObj = new wechatCallbackapiTest();
- if (isset($_GET['echostr'])) {
- $wechatObj->valid();
- }else{
- $wechatObj->responseMsg();
- }
wechatCallbackapiTest類就代碼如下:
- class wechatCallbackapiTest
- {
- public function valid()
- {
- $echoStr = $_GET["echostr"];
- if($this->checkSignature()){
- echo $echoStr;
- 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;
- }
- }
- public function responseMsg()
- {
- $postStr = $GLOBALS["HTTP_RAW_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($keyword != " " || !emptyempty( $keyword ) )
- {
- msgType = "text";
- //$contentStr .= date("Y-m-d H:i:s",time());
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //Vevb.com
- echo $resultStr;
- }
- }else{
- echo "";
- exit;
- }
- }
- }
新聞熱點
疑難解答