PHP开发微信公众号,你的服务器没有正确响应Token验证,请阅读消息接口使用指南
程序员文章站
2022-06-14 22:40:58
...
在使用URL和Token启用微信公众平台开发模式消息接口的时候,显示]“ 你的服务器没有正确响应Token验证,请阅读消息接口使用指南”,不知道是什么情况,我的token验证代码用的是开发文档里的示例代码,是在SAE申请的服务器。代码如下(我看网上的解决方法,说写traceHttp()这个方法看log.html文件,但是我在服务器代码目录里边又没有找到log.html文件),求大神指导
define ( "TOKEN", "weixin" );
traceHttp();
$wechatObj = new wechat ( );
$wechatObj->valid ();
class wechat {
public function valid() {
$echoStr = $_GET ["echostr"];
if ($this->checkSignature ()) {
echo $echoStr;
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;
}
}
}
function traceHttp() {
logger("REMOTE_ADDR:" . $_SERVER ['REMOTE_ADDR'].
((strpos($_SERVER ['REMOTE_ADDR'],"101.226"))?" From WeiXin":" Unknown IP"));
logger("QUERY_STRING:".$_SERVER["QUERY_STRING"]);
}
function logger($content)
{
file_put_contents("\log.html",date('Y-m-d H:i:s ').$content."
",FILE_APPEND);
}
?>
define ( "TOKEN", "weixin" );
traceHttp();
$wechatObj = new wechat ( );
$wechatObj->valid ();
class wechat {
public function valid() {
$echoStr = $_GET ["echostr"];
if ($this->checkSignature ()) {
echo $echoStr;
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;
}
}
}
function traceHttp() {
logger("REMOTE_ADDR:" . $_SERVER ['REMOTE_ADDR'].
((strpos($_SERVER ['REMOTE_ADDR'],"101.226"))?" From WeiXin":" Unknown IP"));
logger("QUERY_STRING:".$_SERVER["QUERY_STRING"]);
}
function logger($content)
{
file_put_contents("\log.html",date('Y-m-d H:i:s ').$content."
",FILE_APPEND);
}
?>
回复讨论(解决方案)
你访问的url是否报错?
都没错,我又在网上下载了一个代码包,但是和之前的一样。我重新上传了一遍,然后就可以访问了,不知道怎么回事。