《PHP實戰:PHP微信開發之微信消息自動回復下所遇到的坑》要點:
本文介紹了PHP實戰:PHP微信開發之微信消息自動回復下所遇到的坑,希望對您有用。如果有疑問,可以聯系我們。
微信回復原理:PHP實例
當普通微信用戶向公眾賬號發送消息時,微信服務器首先收到用戶發送的消息;PHP實例
然后將用戶信息和消息打包成XML格式的數據包,再將這個XML數據包通過POST方法提交到開發者設置的URL上.PHP實例
疑問一:為何使用$GLOBALS["HTTP_RAW_POST_DATA"]保存POST過來的數據,而非$_POST數組?PHP實例
回答:PHP實例
POST只能保存標準的數據類型,對于XML、SOAP或Application/Octet-steam之類的內容則無法解析.
PHP實例
而$GLOBALS["HTTP_RAW_POST_DATA"]和$_POST是一樣的,如果POST過來的數據PHP能夠識別,則可以用$GLOBALS["HTTP_RAW_POST_DATA"]來接收.PHP實例
疑問二:simplexml_load_file()各參數和返回值是什么?PHP實例
回答:PHP實例
參數含義PHP實例
string:需要處理的XML字符串.
PHP實例
class:用來指定新對象,通常設置為"SimpleXMLElement",生成一個簡單XML元素的類.
PHP實例
options:指定附加的Libxml參數,通常設置為常量LIBXML_NOCDATA,表示把CDATA設置為文本節點.
PHP實例
ns:一般省略
PHP實例
is_prefix:一般省略PHP實例
函數執行完成后返回SimpleXMLElement類的一個對象.PHP實例
功能:公眾號只接受文字消息,且做出相應的文字回復.PHP實例
<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(); } } //校驗方法 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(){ //獲取微信服務器POST哀求中的數據 $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 = "您發送的消息是:".$postObj->Content; }else{ $content = "請輸入關鍵字";//消息為空 } } $info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content); echo $info; }else{ echo ""; exit; } } }</span>
功能:公眾號只接受圖片消息,且做出相應的文字回復.PHP實例
<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(); } } //校驗方法 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[image]]></MsgType> <PicUrl><![CDATA[this is a url]]></PicUrl> <MediaId><![CDATA[media_id]]></MediaId> <MsgId>1234567890123456</MsgId> </xml> */ public function responseMsg(){ //獲取微信服務器POST哀求中的數據 $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 = "請發送圖片";//消息為空 } } $info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content); echo $info; }else{ echo ""; exit; } } }</span>
以上是小編給大家分享的微信消息自動回復下所遇到的坑的相關知識,希望對大家有所幫助!PHP實例
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/6746.html