Solution to WeChat development Token verification failure

韦小宝
Release: 2023-03-21 11:58:01
Original
5705 people have browsed it

This article describes the solution to Token verification failure during WeChat development. If you are interested in WeChat development or have encountered token verification failure and cannot solve it, you can read this article! Without further ado, let’s get to the point!

WeChatMini program configurationToken verification failure usually occurs when pushing messages. This error is because there is no feedback from your interface page. Correct information is given to the WeChat interface. Netizens also gave some solutions, but some can be configured successfully, and some are not. Below are two types of phpinterface verification codes provided by netizens that are easier to configure successfully.

Code sample one (my verification can be successful):

<?php  
//1. 将timestamp , nonce , token 按照字典排序  
$timestamp = $_GET[&#39;timestamp&#39;];  
$nonce = $_GET[&#39;nonce&#39;];  
$token = "你自定义的Token值";  
$signature = $_GET[&#39;signature&#39;];  
$array = array($timestamp,$nonce,$token);  
sort($array);  
  
//2.将排序后的三个参数拼接后用sha1加密  
$tmpstr = implode(&#39;&#39;,$array);  
$tmpstr = sha1($tmpstr);  
  
//3. 将加密后的字符串与 signature 进行对比, 判断该请求是否来自微信  
if($tmpstr == $signature)  
{  
    echo $_GET[&#39;echostr&#39;];  
    exit;  
}
Copy after login

Code sample two:

<?php  
/** 
  * wechat php test 
  */  
  
//define your token  
define("TOKEN", "自定义token");  
$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, &#39;SimpleXMLElement&#39;, 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(!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);  
        sort($tmpArr);  
        $tmpStr = implode( $tmpArr );  
        $tmpStr = sha1( $tmpStr );  
          
        if( $tmpStr == $signature ){  
            return true;  
        }else{  
            return false;  
        }  
    }  
}  
  
?>
Copy after login

Choose one of the above two example codes to upload directly to your server , in the url in the message configuration (server address, write the server address of the php file), fill in the corresponding custom Token (token), the message encryption key can be randomly generated, I filled in the compatibility mode for the encryption method, and the data The format is up to personal preference ( I filled in JSON). Then just click submit. If the following picture appears, the verification has passed:



##Everyone has encountered Token verification errors. You can take a look! Help you solve token problems faster!

Related recommendations:

Detailed explanation of the token of the app interface

How to set the WeChat applet url and token

The above is the detailed content of Solution to WeChat development Token verification failure. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!