微信公众号token验证失败怎么办

微信公众号token验证失败怎么办

 

在微信公众号的开发过程中,我们使用自己的服务器,所以需要对现有的公号进行配置,以便将公号中的消息转发到自己的服务器。但在很多情况下,我们在公众号平台的服务器配置中已经填写了正确的信息,在提交确认信息时,总是会提示:token验证失败,您可以通过下面的php代码快速验证您的服务器令牌(Token)的有效性。

微信公众号token验证失败怎么办

微信公众号token验证php代码如下:

  • <?php
  • /**
  •  * wechat php test
  •  * update time: 20141008
  •  */
  • //define your token
  • define(“TOKEN”, “weixin”);
  • $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 (!emptyempty($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(!emptyempty( $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, SORT_STRING);

$tmpStr = implode( $tmpArr );

$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){

return true;

}else{

return false;

}

 

 

 

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/115407.html<

(0)
运维的头像运维
上一篇2025-02-18 02:21
下一篇 2025-02-18 02:22

相关推荐

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注