微信訊息管理之被動回覆使用者訊息
當用戶傳送訊息給公眾號時(或某些特定的使用者操作引發的事件推送時),會產生一個POST請求,開發者可以在響應包(Get)中返回特定XML結構,來對該訊息進行響應(現支援回覆文字、圖片、圖文、語音、視訊、音樂)。嚴格來說,傳送被動響應訊息其實並不是一種介面,而是對微信伺服器發過來訊息的一次回覆。
首先處理使用者發過來的訊息,然後根據訊息伺服器做出相應的響應。
public function responseMsg(){ //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//使用者端發來的資料 //if (!empty($postStr)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); //通過simplexml進行資料解析,轉換為物件 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); switch($postObj->MsgType){//使用者傳送的訊息型別 case "event": $this->_doEvent($postObj); break; case "text": $this->_doText($postObj); break; case "image": $this->_doImage($postObj); break; case "voice": $this->_doVoice($postObj); break; case "music": $this->_doMusic($postObj); break; case "location": $this->_doLocation($postObj); break; default: break; } }
1.文字訊息
//文字訊息 private function _doText($postObj){ $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>"; $msgType = "text"; switch($keyword){ case '初音科技': $contentStr = "提供一流的技術支援!!"; break; case 'PHP': $contentStr = "最美的語言!!"; break; case '圖文': $newsArr = array( array( 'Title'=>'初音科技,專為高階網路定製', 'Description'=>'初音科技,做一個品牌影響一個行業!', 'PicUrl'=>'http://edu.zbxiaochengxu.com/o_1b9pjfv6i1111gp69pp1m45sd7q.jpg', 'Url'=>'http://mp.weixin.qq.com/s?__biz=MzA4NTkzMzI0Ng==∣=2652036751&idx=2&sn=641c21e3831c094cdaf40e115c9eaee6&scene=0#wechat_redirect', ), array( 'Title'=>'初音科技,專為高階網路定製', 'Description'=>'初音科技,做一個品牌影響一個行業!', 'PicUrl'=>'http://qfenxiang.chuyin.net.cn/weixinopen/images/0.jpg', 'Url'=>'http://mp.weixin.qq.com/s?__biz=MzA4NTkzMzI0Ng==∣=2652036751&idx=2&sn=641c21e3831c094cdaf40e115c9eaee6&scene=0#wechat_redirect', ) ); $this->_replayNews($postObj,$newsArr); break; default: $contentStr = "啟軍測試微信公眾號響應!!"; break; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }
2.圖片訊息
//回覆圖片訊息 private function _doImage($postObj,$mediaId){ //$contentStr = "您傳送的是圖片資訊!訊息型別為:".$postObj->PicUrl."\nMediaId為:".$postObj->MediaId; if( $postObj->MsgType == 'image'){ $mediaId = $postObj->MediaId; } $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $time = time(); $imageTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Image> <MediaId><![CDATA[%s]]></MediaId> </Image> </xml>"; $msgType = "image"; //$this->_replayText($postObj,$contentStr); $resultStr = sprintf($imageTpl,$fromUsername,$toUsername,$time,$msgType,$mediaId); echo $resultStr; }
3.語音訊息
//回覆語音訊息 private function _doVoice($postObj,$mediaId){ if( $postObj->MsgType == 'voice'){ //$mediaId = $postObj->MediaId; $contentStr = "您傳送的是語音資訊!\n你說的是:".$postObj->Recognition; $this->_replayText($postObj,$contentStr); exit(); } $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $time = time(); $voiceTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Voice> <MediaId><![CDATA[%s]]></MediaId> </Voice> </xml>"; $msgType = "voice"; $resultStr = sprintf($voiceTpl,$fromUsername,$toUsername,$time,$msgType,$mediaId); echo $resultStr; }
4.音樂訊息
//音樂訊息
private function _doMusic($postObj){
$fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $time = time(); $musicTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music> </xml>"; $msgType = "music"; $title = "還魂門"; $desc = "電視劇<老九門>主題曲"; $url = "http://qfenxiang.chuyin.net.cn/weixinopen/musics/music.mp3"; $hqurl = "http://qfenxiang.chuyin.net.cn/weixinopen/musics/music.mp3"; $resultStr = sprintf($musicTpl,$fromUsername,$toUsername,$time,$msgType,$title,$desc,$url,$hqurl,$contentStr); echo $resultStr;
}
5.圖文訊息
//[封裝]圖文回覆 private function _replayNews($postObj,$newsArr){ if( !is_array($newsArr) || sizeof($newsArr)<1 ){ $newsArr = array( array( 'Title'=>'全民創業蒙的就是你,來一盆冷水吧!', 'Description'=>'全民創業已經如火如荼,然而創業是一個非常自我的過程,它是一種生活方式的選擇。從外部的推動有助於提高創業的存活率,但是未必能夠提高創新的成功率。', 'PicUrl'=>'http://yun.topthink.com/Uploads/Editor/2015-07-30/55b991cad4c48.jpg', 'Url'=>'http://www.topthink.com/topic/11991.html', ) ); } $itemStr = ''; foreach($newsArr as $item){ $newsData = '<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>'; $itemStr .= sprintf($newsData,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']); } $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $time = time(); $newsTpl = '<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>%s</ArticleCount> <Articles>'.$itemStr.'</Articles> </xml>'; $msgType = 'news'; $count = count($newsArr); $resultStr = sprintf($newsTpl,$fromUsername,$toUsername,$time,$msgType,$count,$str); echo $resultStr; }