Home > Backend Development > PHP Tutorial > 微信接口配置token验证失败。

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

WBOY
Release: 2016-06-06 20:25:22
Original
1952 people have browsed it

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

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



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




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

/**

  • wechat php test
    */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

class wechatCallbackapiTest
{

<code>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></tousername>
                        <fromusername></fromusername>
                        <createtime>%s</createtime>
                        <msgtype></msgtype>
                        <content></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);
    print_r($tmpArr);
    echo "<hr>";
    sort($tmpArr,SORT_STRING);
    print_r($tmpArr);
    echo "<hr>";
    $tmpStr = implode('', $tmpArr );
    echo $tmpStr;
    echo "<hr>";
    $tmpStr = sha1( $tmpStr );
    echo "sha1加密后:".$tmpStr;
    echo "<hr>";
    echo  'signature:'.$signature;
    
    if( $tmpStr == $signature ){
        return true;
    }else{
        return false;
    }
}</code>
Copy after login
Copy after login

}
`

回复内容:

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

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



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




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

/**

  • wechat php test
    */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

class wechatCallbackapiTest
{

<code>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></tousername>
                        <fromusername></fromusername>
                        <createtime>%s</createtime>
                        <msgtype></msgtype>
                        <content></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);
    print_r($tmpArr);
    echo "<hr>";
    sort($tmpArr,SORT_STRING);
    print_r($tmpArr);
    echo "<hr>";
    $tmpStr = implode('', $tmpArr );
    echo $tmpStr;
    echo "<hr>";
    $tmpStr = sha1( $tmpStr );
    echo "sha1加密后:".$tmpStr;
    echo "<hr>";
    echo  'signature:'.$signature;
    
    if( $tmpStr == $signature ){
        return true;
    }else{
        return false;
    }
}</code>
Copy after login
Copy after login

}
`

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

你直接

<code><?php echo $_GET['echostr'];
?></code>
Copy after login

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

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template