我在新浪sae申請了帳號之後建立了一個應用作為測試介面使用,代碼用的都是官方教程的,但是在提交伺服器配置的時候不是串連失敗就是token驗證失敗,地址和token我確認沒有填寫錯誤,實在找不出原因。
於是我在本地進行了一下測試,把新浪sae日誌中心發送的get提取複寫過來,加在本地的地址後面模仿的請求,!!![圖片描述][2]結果發現,程式裡接收的timestamp和nonce加上token的字串按要求排序後和連結裡的signature根本不相同。!!![圖片描述][3]有大神遇到過這個問題嗎?還是我哪裡操作不對?程式是照搬官方文檔的啊。求指點啊!`
/**
//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 = " %s %s %s %s %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); print_r($tmpArr); echo ""; sort($tmpArr,SORT_STRING); print_r($tmpArr); echo ""; $tmpStr = implode('', $tmpArr ); echo $tmpStr; echo ""; $tmpStr = sha1( $tmpStr ); echo "sha1加密後:".$tmpStr; echo ""; echo 'signature:'.$signature; if( $tmpStr == $signature ){ return true; }else{ return false; }}
}
`
回複內容:
我在新浪sae申請了帳號之後建立了一個應用作為測試介面使用,代碼用的都是官方教程的,但是在提交伺服器配置的時候不是串連失敗就是token驗證失敗,地址和token我確認沒有填寫錯誤,實在找不出原因。
於是我在本地進行了一下測試,把新浪sae日誌中心發送的get提取複寫過來,加在本地的地址後面模仿的請求,!!![圖片描述][2]結果發現,程式裡接收的timestamp和nonce加上token的字串按要求排序後和連結裡的signature根本不相同。!!![圖片描述][3]有大神遇到過這個問題嗎?還是我哪裡操作不對?程式是照搬官方文檔的啊。求指點啊!`
/**
//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 = " %s %s %s %s %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); print_r($tmpArr); echo ""; sort($tmpArr,SORT_STRING); print_r($tmpArr); echo ""; $tmpStr = implode('', $tmpArr ); echo $tmpStr; echo ""; $tmpStr = sha1( $tmpStr ); echo "sha1加密後:".$tmpStr; echo ""; echo 'signature:'.$signature; if( $tmpStr == $signature ){ return true; }else{ return false; }}
}
`
做這塊最好不要用sae,sae好像要實名認證才能正常訪問一些訊息。
你直接
sae需要實名認證,如果不進行實名認證,SAE會在回複的內容中會帶上幹擾的html內容資訊,從而導致Token驗證失敗或者該公眾號暫時無法提供服務,請稍後再試。
ase需要實名認證後,才能驗證成功的