一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

微信开发Token验证失败或请求URL超时问题解决办法

时间:2015-12-15 编辑:简简单单 来源:一聚教程网

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 (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content);
            $time = time();
            $textTpl = "
                       
                       
                        %s
                       
                       
                        0
                       
";
            if($keyword != " " || !empty( $keyword ) )
            {
    msgType = "text";
    //$contentStr .= date("Y-m-d H:i:s",time());
    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
    echo $resultStr;
            }
        }else{
            echo "";
            exit;
        }
    }
}

热门栏目