Heim > Backend-Entwicklung > PHP-Tutorial > 问题:微信公众平台的开发中一直显示的是 token验证失败

问题:微信公众平台的开发中一直显示的是 token验证失败

WBOY
Freigeben: 2016-06-06 20:30:37
Original
1198 Leute haben es durchsucht

URL能够正常的访问
使用的是微信公众平台给出的文档。

<code><?php /**
  * wechat php test
  */

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

if($_GET[echostr]){

$wechatObj->valid();
    
}else {
    
}

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)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
                $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()
    {
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }
        
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
                
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

?>
</code>
Nach dem Login kopieren
Nach dem Login kopieren

改动位置就在最前面加入了一个if 判断,后面没有做任何的修改
问题:微信公众平台的开发中一直显示的是 token验证失败

google了方法也试了没有解决,所有在这里发帖了,谢谢各位。

回复内容:

URL能够正常的访问
使用的是微信公众平台给出的文档。

<code><?php /**
  * wechat php test
  */

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

if($_GET[echostr]){

$wechatObj->valid();
    
}else {
    
}

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)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
                $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()
    {
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }
        
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
                
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

?>
</code>
Nach dem Login kopieren
Nach dem Login kopieren

改动位置就在最前面加入了一个if 判断,后面没有做任何的修改
问题:微信公众平台的开发中一直显示的是 token验证失败

google了方法也试了没有解决,所有在这里发帖了,谢谢各位。

我们在学习的时候也是遇到这个问题,请你用的是租用的虚拟空间,还是利用百度app或者是新浪app的云服务器,如果是前者提到的,那么需要先完成实名验证的。这样才可以去掉那个烦人的提醒,能可以连上微信后台服务器。
如果还是有其他问题,可以继续提问,谢谢

<code>php</code><code>if($_GET[echostr]){

$wechatObj->valid();

}else {
    $wechatObj->responseMsg();//?
}
</code>
Nach dem Login kopieren

你要确保你 echo $echoStr; 之前没有任何输出才行。。。

Verwandte Etiketten:
php
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage