转自:点击打开链接
http://lvwenhan.com/create/372.html
实质:就是使用官方文件,调用
$wechatObj->valid();而不是,注释后,调用:
$wechatObj->responseMsg();关于微信公共平台"你的服务未能正确响应Token验证"错误的解决办法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 = "
%s
0
";
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;
}
}
}
?> 以上为腾讯官方提供的接口文件示例。解决问题其实很简单,微信文档貌似没写(或者我没找到==):
验证token时使用:$wechatObj->valid();
正式生产环境中需要回复用户消息时,使用:$wechatObj->responseMsg();
问题解决。
以上就介绍了关于微信公共平台"你的服务未能正确响应Token验证"错误的解决办法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。