好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php token验证失败怎么办?

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

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

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

后来看了下文档,如下

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

所以准备的一下代码

respond.php:
<?php
/**
  * wechat php test
  */
 
//define your token
define("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编程从入门到精通

以上就是php token验证失败怎么办?的详细内容!

查看更多关于php token验证失败怎么办?的详细内容...

  阅读:43次