微信公共号token验证失败解决办法之一

2018-01-12 11:16:18来源:网络收集作者:纳米程序员人点击

分享

阿里云爆款

按照网上教程,下了微信公共号php验证token的模板,在新浪云sae上建了php应用,但是死活token验证失败。网上查了各种资料,有一种说法是,sae如果没实名认证,会在传输过来的信息中加些东西。解决办法就是加


header('content-type:text');


结果还真通过了。现在分享出来供大家参考。我是在valid()函数里加的。代码如下:



//define your token
define("TOKEN", "test");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
public function valid()
{
header('content-type:text');
$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;
}
}
}
?>


最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台