这篇文章将为大家详细讲解有关如何解决php token验证失败的问题,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

php token验证失败的解决办法:1、保障添加的服务器是联通,并且url是能够访问;2、token不能重复;3、服务器上的token要改时,要和配置表单上的一致。

php token验证失败的解决办法:

这里附上配置表单,token验证失败的信息。

后来看了下文档,如下

要返回参数给微信,返回成功则成为开发者;

所以准备的一下代码

respond.php:<?php/** * wechat php test */ //define your tokendefine("TOKEN", "hwqhwq");$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; } }} ?>

只要两个条件就可以验证成功

一、你的服务器一定是通的,保证你url是能访问的。

二、token最好是不要和别人重复的;

三、服务器上的token要改,要有配置表单上的一样,他们对应就可以了


关于如何解决php token验证失败的问题就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。