Home  >  Article  >  Backend Development  >  Solution to WeChat development Token verification failure

Solution to WeChat development Token verification failure

韦小宝
韦小宝Original
2018-03-14 13:21:095634browse

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):

Code sample two:

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);  
        sort($tmpArr);  
        $tmpStr = implode( $tmpArr );  
        $tmpStr = sha1( $tmpStr );  
          
        if( $tmpStr == $signature ){  
            return true;  
        }else{  
            return false;  
        }  
    }  
}  
  
?>

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!

Statement:
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