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