我在新浪sae申請(qǐng)了賬號(hào)之后創(chuàng)建了一個(gè)應(yīng)用作為測(cè)試微信接口使用,代碼用的都是官方教程的,但是在提交服務(wù)器配置的時(shí)候不是連接失敗就是token驗(yàn)證失敗,地址和token我確認(rèn)沒有填寫錯(cuò)誤,實(shí)在找不出原因。
于是我在本地進(jìn)行了一下測(cè)試,把新浪sae日志中心微信發(fā)送的get請(qǐng)求復(fù)制過來,加在本地的地址后面模仿微信的請(qǐng)求,! ! ![圖片描述][2] 結(jié)果發(fā)現(xiàn),程序里接收的timestamp和nonce加上token的字符串按要求排序后和鏈接里的signature根本不相同。! !![圖片描述][3] 有大神遇到過這個(gè)問題嗎?還是我哪里操作不對(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; } }
}
`
我在新浪sae申請(qǐng)了賬號(hào)之后創(chuàng)建了一個(gè)應(yīng)用作為測(cè)試微信接口使用,代碼用的都是官方教程的,但是在提交服務(wù)器配置的時(shí)候不是連接失敗就是token驗(yàn)證失敗,地址和token我確認(rèn)沒有填寫錯(cuò)誤,實(shí)在找不出原因。
于是我在本地進(jìn)行了一下測(cè)試,把新浪sae日志中心微信發(fā)送的get請(qǐng)求復(fù)制過來,加在本地的地址后面模仿微信的請(qǐng)求,! ! ![圖片描述][2] 結(jié)果發(fā)現(xiàn),程序里接收的timestamp和nonce加上token的字符串按要求排序后和鏈接里的signature根本不相同。! !![圖片描述][3] 有大神遇到過這個(gè)問題嗎?還是我哪里操作不對(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; } }
}
`
做微信這塊最好不要用sae,sae好像要實(shí)名認(rèn)證才能正常訪問一些消息。
你直接
<?php echo $_GET['echostr']; ?>
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ù),請(qǐng)稍后再試。
ase需要實(shí)名認(rèn)證后,才能驗(yàn)證成功的
微信是一款手機(jī)通信軟件,支持通過手機(jī)網(wǎng)絡(luò)發(fā)送語音短信、視頻、圖片和文字。微信可以單聊及群聊,還能根據(jù)地理位置找到附近的人,帶給大家全新的移動(dòng)溝通體驗(yàn),有需要的小伙伴快來保存下載體驗(yàn)吧!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)