当用户发送消息给公众号时(或某些特定的用户操作引发的事件推送时),会产生一个 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==&mid=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==&mid=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;
}