我是用thinkphp開發的,這是訊息推送地址的代碼:
sReqTimeStamp = I ( 'get.timestamp' ); $this->sReqNonce = I ( 'get.nonce' ); $this->sEncryptMsg = I ( 'get.msg_signature' ); $this->wxcpt = new \WXBizMsgCrypt ( $token, $encodingaeskey, $appid); $sMsg = ""; // 解析之後的明文 $errCode = $this->wxcpt->DecryptMsg ( $this->sEncryptMsg, $this->sReqTimeStamp, $this->sReqNonce, $content, $sMsg ); if ($errCode != 0) { exit (); } else { // 解密成功,sMsg即為xml格式的明文 $content = $sMsg; } } $data = new \SimpleXMLElement ( $content ); // $data || die ( '參數擷取失敗' ); foreach ( $data as $key => $value ) { $this->data [$key] = safe ( strval ( $value ) ); } $this->replyText("好好學習"); } /* 回複簡訊 */ public function replyText($content) { $msg ['Content'] = $content; $this->_replyData ( $msg, 'text' ); } /* 發送回複訊息到平台 */ private function _replyData($msg, $msgType) { $msg ['ToUserName'] = $this->data ['FromUserName']; $msg ['FromUserName'] = $this->data ['ToUserName']; $msg ['CreateTime'] = NOW_TIME; $msg ['MsgType'] = $msgType; if ($_REQUEST ['doNotInit']) { dump ( $msg ); exit (); } $xml = new \SimpleXMLElement ( '' ); $this->_data2xml ( $xml, $msg ); $str = $xml->asXML (); if ($_GET ['encrypt_type'] == 'aes') { $sEncryptMsg = ""; // xml格式的密文 $errCode = $this->wxcpt->EncryptMsg ( $str, $this->sReqTimeStamp, $this->sReqNonce, $sEncryptMsg ); if ($errCode == 0) { $str = $sEncryptMsg; } else { } } echo ($str); } /* 組裝xml資料 */ public function _data2xml($xml, $data, $item = 'item') { foreach ( $data as $key => $value ) { is_numeric ( $key ) && ($key = $item); if (is_array ( $value ) || is_object ( $value )) { $child = $xml->addChild ( $key ); $this->_data2xml ( $child, $value, $item ); } else { if (is_numeric ( $value )) { $child = $xml->addChild ( $key, $value ); } else { $child = $xml->addChild ( $key ); $node = dom_import_simplexml ( $child ); $node->appendChild ( $node->ownerDocument->createCDATASection ( $value ) ); } } } }}
其實我是想知道怎麼去調試,代碼肯定是有問題的。。。
回複內容:
我是用thinkphp開發的,這是訊息推送地址的代碼:
sReqTimeStamp = I ( 'get.timestamp' ); $this->sReqNonce = I ( 'get.nonce' ); $this->sEncryptMsg = I ( 'get.msg_signature' ); $this->wxcpt = new \WXBizMsgCrypt ( $token, $encodingaeskey, $appid); $sMsg = ""; // 解析之後的明文 $errCode = $this->wxcpt->DecryptMsg ( $this->sEncryptMsg, $this->sReqTimeStamp, $this->sReqNonce, $content, $sMsg ); if ($errCode != 0) { exit (); } else { // 解密成功,sMsg即為xml格式的明文 $content = $sMsg; } } $data = new \SimpleXMLElement ( $content ); // $data || die ( '參數擷取失敗' ); foreach ( $data as $key => $value ) { $this->data [$key] = safe ( strval ( $value ) ); } $this->replyText("好好學習"); } /* 回複簡訊 */ public function replyText($content) { $msg ['Content'] = $content; $this->_replyData ( $msg, 'text' ); } /* 發送回複訊息到平台 */ private function _replyData($msg, $msgType) { $msg ['ToUserName'] = $this->data ['FromUserName']; $msg ['FromUserName'] = $this->data ['ToUserName']; $msg ['CreateTime'] = NOW_TIME; $msg ['MsgType'] = $msgType; if ($_REQUEST ['doNotInit']) { dump ( $msg ); exit (); } $xml = new \SimpleXMLElement ( '' ); $this->_data2xml ( $xml, $msg ); $str = $xml->asXML (); if ($_GET ['encrypt_type'] == 'aes') { $sEncryptMsg = ""; // xml格式的密文 $errCode = $this->wxcpt->EncryptMsg ( $str, $this->sReqTimeStamp, $this->sReqNonce, $sEncryptMsg ); if ($errCode == 0) { $str = $sEncryptMsg; } else { } } echo ($str); } /* 組裝xml資料 */ public function _data2xml($xml, $data, $item = 'item') { foreach ( $data as $key => $value ) { is_numeric ( $key ) && ($key = $item); if (is_array ( $value ) || is_object ( $value )) { $child = $xml->addChild ( $key ); $this->_data2xml ( $child, $value, $item ); } else { if (is_numeric ( $value )) { $child = $xml->addChild ( $key, $value ); } else { $child = $xml->addChild ( $key ); $node = dom_import_simplexml ( $child ); $node->appendChild ( $node->ownerDocument->createCDATASection ( $value ) ); } } } }}
其實我是想知道怎麼去調試,代碼肯定是有問題的。。。
可以通過寫檔案或者資料庫的方式調試,我一般是寫檔案!比如代碼的入口開始寫檔案,記錄使用者openid以及發送內容,然後以此類推,最極端的情況是每行代碼後面都跟上調試資訊,當然,這沒必要哈!只需要在你感興趣的有疑問的地方加就好了!如果前一個調試資訊有了,後一個調試資訊沒出來,肯定是中間的代碼有問題!另外記得檢查下代碼有沒有語法錯誤之類的,在編輯器裡開啟看看
公眾號開發文檔裡又個php的例子,先把那個例子跑起來。然後對照那個例子一點一點地調試你的代碼,先保證你和對接沒問題。
然後,我是自己做了個用戶端類比使用者給後台發訊息,測試自己的邏輯對不對。
信信通 關注事件 <xml><ToUserName><![CDATA[toUser]]></toUserName><FromUserName><![CDATA[mocker]]></fromUserName><CreateTime>1348831865</createTime><MsgType><![CDATA[event]]></msgType><Event><![CDATA[subscribe]]></event></xml> send 簡訊 send 菜單訊息 send 位置事件 <xml><ToUserName><![CDATA[toUser]]></toUserName><FromUserName><![CDATA[mocker]]></fromUserName> <CreateTime>1351776365</createTime> <MsgType><![CDATA[location]]></msgType> <Location_X>23.134521</location_X> <Location_Y>113.358803</location_Y> <Scale>20</scale> <Label><![CDATA[位置資訊]]></label> <MsgId>9876543210123456</msgId></xml> send 二維碼關注 send 情境二維碼 send 完成群發 send 執行結果
我經常這麼乾的
1、在電腦上建立共用wifi
2、手機通過共用wifi上網,使用
3、在電腦上開Wireshark,然後抓你建立的共用wifi中的資料
4、在wireshark中跟蹤請求和響應
還有個小事情可以處理下,就是設定你的伺服器不啟用gzip
。
最近官方提供了兩個調試途徑
登入你的公眾號,找到開發人員中心。
附上最近基於進階介面開發的活動工具
連結
我提供兩個方案,樓主自己看看你適不適合自己。
一是,把請求的資料寫到log檔案,一開始就從接受資料的位置,然後慢慢的往下推,到無法記錄資料的時候就問題就在剛剛跳過的那裡了,當然,語法錯誤真方法就無效了,語法錯誤的時候,你可以把授權的操作關掉(無授權請忽略),然後瀏覽器直接存取連結,看看報錯位置。
第二,就是用官方給出的開發人員工具進行調試,工具可以在呢開發人員文檔那裡下載,位置大概是:開始開發->開發人員調試工具
以上是個人呢開發時裡面使用的調試方式,因為個人剛剛畢業不久,如果說錯了,還請各位大神指正
可以試試用這個工具調試:
軟體下載地址 是windows版的
先用將自己的openid列印在日誌中,再複製到本地測試,發送訊息可以本地測試;或者用qq瀏覽器有個調試工具 選擇伺服器調試 啟動你本地的項目 將產生的外部連結配置在公眾號裡 就可以本地測試