PHP微信開發(fā)之微信消息自動(dòng)回復(fù)下所遇到的坑
微信回復(fù)原理:
當(dāng)普通微信用戶向公眾賬號(hào)發(fā)送消息時(shí),微信服務(wù)器首先收到用戶發(fā)送的消息;
然后將用戶信息和消息打包成XML格式的數(shù)據(jù)包,再將這個(gè)XML數(shù)據(jù)包通過POST方法提交到開發(fā)者設(shè)置的URL上。
疑問一:為何使用$GLOBALS["HTTP_RAW_POST_DATA"]保存POST過來的數(shù)據(jù),而非$_POST數(shù)組?
回答:
POST只能保存標(biāo)準(zhǔn)的數(shù)據(jù)類型,對(duì)于XML、SOAP或Application/Octet-steam之類的內(nèi)容則無法解析。
而$GLOBALS["HTTP_RAW_POST_DATA"]和$_POST是一樣的,如果POST過來的數(shù)據(jù)PHP能夠識(shí)別,則可以用$GLOBALS["HTTP_RAW_POST_DATA"]來接收。
疑問二:simplexml_load_file()各參數(shù)和返回值是什么?
回答:
參數(shù)含義
string:需要處理的XML字符串。
class:用來指定新對(duì)象,通常設(shè)置為"SimpleXMLElement",生成一個(gè)簡(jiǎn)單XML元素的類。
options:指定附加的Libxml參數(shù),通常設(shè)置為常量LIBXML_NOCDATA,表示把CDATA設(shè)置為文本節(jié)點(diǎn)。
ns:一般省略
is_prefix:一般省略
函數(shù)執(zhí)行完成后返回SimpleXMLElement類的一個(gè)對(duì)象。
功能:公眾號(hào)只接受文字消息,且做出相應(yīng)的文字回復(fù)。
<span style="font-family:Courier New;font-size:14px;"><?php define("TOKEN","weixin"); $weixinObj = new Wechat(); $weixinObj->valid(); class Wechat{ public function valid(){ $echoStr = $_GET['echostr']; //如果是第一次接入 if($this->checkSignature() && $echoStr ){ echo $echoStr; exit; }else{ $this->responseMsg(); } } //校驗(yàn)方法 private function checkSignature(){ $signature = $_GET['signature']; $timestamp = $_GET['timestamp']; $nonce = $_GET['nonce']; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if($tmpStr == $signature){ return true; }else{ return false; } } /* 普通文本消息 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[this is a test]]></Content> </xml> */ public function responseMsg(){ //獲取微信服務(wù)器POST請(qǐng)求中的數(shù)據(jù) $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if( !empty($postStr) ){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUser = $postObj->FromUserName; $toUser = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; if( strtolower($postObj->MsgType)!='text' ){ $msgType = "text"; $content = "我只接受文本消息"; }else{ $msgType = "text"; if( !empty($keyword) ){ $content = "您發(fā)送的消息是:".$postObj->Content; }else{ $content = "請(qǐng)輸入關(guān)鍵字";//消息為空 } } $info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content); echo $info; }else{ echo ""; exit; } } }</span>
功能:公眾號(hào)只接受圖片消息,且做出相應(yīng)的文字回復(fù)。
<span style="font-family:Courier New;font-size:14px;"><?php define("TOKEN","weixin"); $weixinObj = new Wechat(); $weixinObj->valid(); class Wechat{ public function valid(){ $echoStr = $_GET['echostr']; //如果是第一次接入 if($this->checkSignature() && $echoStr ){ echo $echoStr; exit; }else{ $this->responseMsg(); } } //校驗(yàn)方法 private function checkSignature(){ $signature = $_GET['signature']; $timestamp = $_GET['timestamp']; $nonce = $_GET['nonce']; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if($tmpStr == $signature){ return true; }else{ return false; } } /* 接收?qǐng)D片消息格式 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[image]]></MsgType> <PicUrl><![CDATA[this is a url]]></PicUrl> <MediaId><![CDATA[media_id]]></MediaId> <MsgId>1234567890123456</MsgId> </xml> */ public function responseMsg(){ //獲取微信服務(wù)器POST請(qǐng)求中的數(shù)據(jù) $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if( !empty($postStr) ){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUser = $postObj->FromUserName; $toUser = $postObj->ToUserName; $time = time(); $msgType= $postObj->MsgType; $picUrl = $postObj->PicUrl; $mediaId = $postObj->MediaId; $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; if( strtolower($msgType)!='image' ){ $msgType = "text"; $content = "我只接受圖片消息"; }else{ $msgType = "text"; if( !empty( $picUrl ) ){ $content = "圖片鏈接為:".$picUrl."\n"; $content .= "媒體id:".$mediaId; }else{ $content = "請(qǐng)發(fā)送圖片";//消息為空 } } $info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content); echo $info; }else{ echo ""; exit; } } }</span>
以上是小編給大家分享的微信消息自動(dòng)回復(fù)下所遇到的坑的相關(guān)知識(shí),希望對(duì)大家有所幫助!
- php微信開發(fā)之音樂回復(fù)功能
- php微信開發(fā)之關(guān)鍵詞回復(fù)功能
- php微信公眾號(hào)開發(fā)(4)php實(shí)現(xiàn)自定義關(guān)鍵字回復(fù)
- php版微信開發(fā)之接收消息,自動(dòng)判斷及回復(fù)相應(yīng)消息的方法
- php版微信公眾平臺(tái)回復(fù)中文出現(xiàn)亂碼問題的解決方法
- 微信公眾號(hào)開發(fā)之文本消息自動(dòng)回復(fù)php代碼
- 驗(yàn)證token、回復(fù)圖文\文本、推送消息的實(shí)用微信類php代碼
- PHP微信開發(fā)之模板消息回復(fù)
- PHP微信開發(fā)之文本自動(dòng)回復(fù)
- php微信開發(fā)之圖片回復(fù)功能
相關(guān)文章
跟我學(xué)Laravel之請(qǐng)求(Request)的生命周期
這篇文檔包含了關(guān)于請(qǐng)求生命周期的高層次概述,以及啟動(dòng)文件和應(yīng)用程序事件的相關(guān)內(nèi)容。是篇非常不錯(cuò)的文章,有需要的朋友可以參考下2014-10-10PHP將頁面中點(diǎn)擊數(shù)量高的鏈接進(jìn)行高亮顯示的方法
這里來介紹PHP將頁面中點(diǎn)擊數(shù)量高的鏈接進(jìn)行高亮顯示的方法,主要受到WordPress中某插件的啟發(fā),需要的朋友可以參考下2016-05-05解決laravel5中auth用戶登錄其他頁面獲取不到登錄信息的問題
今天小編就為大家分享一篇解決laravel5中auth用戶登錄其他頁面獲取不到登錄信息的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10Swoole?webSocket消息服務(wù)系統(tǒng)代碼設(shè)計(jì)詳解
這篇文章主要為大家介紹了Swoole?webSocket消息服務(wù)系統(tǒng)代碼設(shè)計(jì)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03TP5框架實(shí)現(xiàn)的數(shù)據(jù)庫備份功能示例
這篇文章主要介紹了TP5框架實(shí)現(xiàn)的數(shù)據(jù)庫備份功能,結(jié)合實(shí)例形式分析了TP5數(shù)據(jù)庫備份功能相關(guān)原理及實(shí)現(xiàn)方法,需要的朋友可以參考下2020-04-04ThinkPHP模板判斷輸出Empty標(biāo)簽用法詳解
這篇文章主要介紹了ThinkPHP模板判斷輸出Empty標(biāo)簽用法,需要的朋友可以參考下2014-06-06PHP連接MySQL數(shù)據(jù)庫三種實(shí)現(xiàn)方法
這篇文章主要介紹了PHP連接MySQL數(shù)據(jù)庫三種實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12