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['timestamp']; $nonce = $_GET['nonce']; $token = "你自定义的Token值"; $signature = $_GET['signature']; $array = array($timestamp,$nonce,$token); sort($array); //2.将排序后的三个参数拼接后用sha1加密 $tmpstr = implode('',$array); $tmpstr = sha1($tmpstr); //3. 将加密后的字符串与 signature 进行对比, 判断该请求是否来自微信 if($tmpstr == $signature) { echo $_GET['echostr']; exit; }
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, 'SimpleXMLElement', 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; } } } ?>
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:
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!