php token验证失败的解决办法:1、保障添加的服务器是联通,并且url是能够访问;2、token不能重复;3、服务器上的token要改时,要和配置表单上的一致。
要返回参数给微信,返回成功则成为开发者; 所以准备的一下代码 respond.php:
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;
}
}
}
?> 只要两个条件就可以验证成功 一、你的服务器一定是通的,保证你url是能访问的。 二、token最好是不要和别人重复的; 三、服务器上的token要改,要有配置表单上的一样,他们对应就可以了
相关学习推荐:PHP编程从入门到精通
以上就是php token验证失败怎么办?的详细内容,更多请关注 模板之家(www.mb5.com.cn) 其它相关文章! |