当前位置:Gxlcms > PHP教程 > 微信接口配置token验证失败。

微信接口配置token验证失败。

时间:2021-07-01 10:21:17 帮助过:19人阅读

我在新浪sae申请了账号之后创建了一个应用作为测试微信接口使用,代码用的都是官方教程的,但是在提交服务器配置的时候不是连接失败就是token验证失败,地址和token我确认没有填写错误,实在找不出原因。

于是我在本地进行了一下测试,把新浪sae日志中心微信发送的get请求复制过来,加在本地的地址后面模仿微信的请求,!
!
![图片描述][2]



结果发现,程序里接收的timestamp和nonce加上token的字符串按要求排序后和链接里的signature根本不相同。!
!![图片描述][3]




有大神遇到过这个问题吗?还是我哪里操作不对?程序是照搬官方文档的啊。求指点啊!
`

/**

  • 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 = "
                        
                        
                        %s
                        
                        
                        0
                        ";             
            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);
    print_r($tmpArr);
    echo "
"; sort($tmpArr,SORT_STRING); print_r($tmpArr); echo "
"; $tmpStr = implode('', $tmpArr ); echo $tmpStr; echo "
"; $tmpStr = sha1( $tmpStr ); echo "sha1加密后:".$tmpStr; echo "
"; echo 'signature:'.$signature; if( $tmpStr == $signature ){ return true; }else{ return false; } }

}
`

回复内容:

我在新浪sae申请了账号之后创建了一个应用作为测试微信接口使用,代码用的都是官方教程的,但是在提交服务器配置的时候不是连接失败就是token验证失败,地址和token我确认没有填写错误,实在找不出原因。

于是我在本地进行了一下测试,把新浪sae日志中心微信发送的get请求复制过来,加在本地的地址后面模仿微信的请求,!
!
![图片描述][2]



结果发现,程序里接收的timestamp和nonce加上token的字符串按要求排序后和链接里的signature根本不相同。!
!![图片描述][3]




有大神遇到过这个问题吗?还是我哪里操作不对?程序是照搬官方文档的啊。求指点啊!
`

/**

  • 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 = "
                        
                        
                        %s
                        
                        
                        0
                        ";             
            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);
    print_r($tmpArr);
    echo "
"; sort($tmpArr,SORT_STRING); print_r($tmpArr); echo "
"; $tmpStr = implode('', $tmpArr ); echo $tmpStr; echo "
"; $tmpStr = sha1( $tmpStr ); echo "sha1加密后:".$tmpStr; echo "
"; echo 'signature:'.$signature; if( $tmpStr == $signature ){ return true; }else{ return false; } }

}
`

做微信这块最好不要用sae,sae好像要实名认证才能正常访问一些消息。

你直接

sae需要实名认证,如果不进行实名认证,SAE会在回复的内容中会带上干扰的html内容信息,从而导致Token验证失败或者该公众号暂时无法提供服务,请稍后再试。

ase需要实名认证后,才能验证成功的

人气教程排行