我在新浪sae申請(qǐng)了賬號(hào)之后創(chuàng)建了一個(gè)應(yīng)用作為測(cè)試微信接口使用,代碼用的都是官方教程的,但是在提交服務(wù)器配置的時(shí)候不是連接失敗就是token驗(yàn)證失敗,地址和token我確認(rèn)沒(méi)有填寫(xiě)錯(cuò)誤,實(shí)在找不出原因。
于是我在本地進(jìn)行了一下測(cè)試,把新浪sae日志中心微信發(fā)送的get請(qǐng)求復(fù)制過(guò)來(lái),加在本地的地址后面模仿微信的請(qǐng)求,!
!
![圖片描述][2]
結(jié)果發(fā)現(xiàn),程序里接收的timestamp和nonce加上token的字符串按要求排序后和鏈接里的signature根本不相同。!
!![圖片描述][3]
有大神遇到過(guò)這個(gè)問(wèn)題嗎?還是我哪里操作不對(duì)?程序是照搬官方文檔的啊。求指點(diǎn)啊!
`
/**
wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->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 = "<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 "";
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
print_r($tmpArr);
echo "<hr>";
sort($tmpArr,SORT_STRING);
print_r($tmpArr);
echo "<hr>";
$tmpStr = implode('', $tmpArr );
echo $tmpStr;
echo "<hr>";
$tmpStr = sha1( $tmpStr );
echo "sha1加密后:".$tmpStr;
echo "<hr>";
echo 'signature:'.$signature;
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
`
認(rèn)證高級(jí)PHP講師
sae需要實(shí)名認(rèn)證,如果不進(jìn)行實(shí)名認(rèn)證,SAE會(huì)在回復(fù)的內(nèi)容中會(huì)帶上干擾的html內(nèi)容信息,從而導(dǎo)致Token驗(yàn)證失敗或者該公眾號(hào)暫時(shí)無(wú)法提供服務(wù),請(qǐng)稍后再試。