微信开发平台 接口配置token验证失败 配置失败的原因与解决办法

在微信开发平台 接口配置token之前总是提交失败,找了很多资料,最终在第16行中加了一行代码:

header(‘content-type:text’);

通过验证。

如果依然无法验证,尝试一下把文件字符编码集改为UTF-8 无BOM 编码格式。

最终的验证脚本文件内容如下:

<?php
error_reporting(E_ALL^E_NOTICE^E_WARNING);
//define your token
define(“TOKEN”, “qqyybc0emywuseqvriig1dtb1d4ouyqf”);
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET[“echostr”];

//valid signature , option
if($this->checkSignature()){
header(‘content-type:text’);
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)){
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
libxml_disable_entity_loader(true);
$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(!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 “no data”;
exit;
}
}

private function checkSignature()
{
// you must define TOKEN by yourself
if (!defined(“TOKEN”)) {
throw new Exception(‘TOKEN is not defined!’);
}

$signature = $_GET[“signature”];
$timestamp = $_GET[“timestamp”];
$nonce = $_GET[“nonce”];

$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}

?>

微信原Demo包下载地址:http://mp.weixin.qq.com/mpres/htmledition/res/wx_sample.20140819.zip

作者: sinma

sinma 存在于自然,存在于你身边。

发表评论