第二节 消息和事件的接受和回复
2016-12-08 22:57
176 查看
//响应消息 //根据用户传过来的消息类型进行不同的响应 public function responseMsg(){ //1、接收微信服务器POST过来的数据,XML数据包 $postData = file_get_contents('php://input'); if(!$postData) { echo "error"; exit(); } //2、解析XML数据包 libxml_disable_entity_loader(true); $object = simplexml_load_string($postData,"SimpleXMLElement",LIBXML_NOCDATA); //获取消息类型 $MsgType = $object->MsgType; switch ($MsgType) { case 'event': //接收事件推送 $this->receiveEvent($object); break; case 'text': //接收文本消息 echo $this->receiveText($object); break; case 'image': //接收图片消息 echo $this->receiveImage($object); break; case 'location': //接收地理位置消息 echo $this->receiveLocation($object); break; case 'voice': //接收语音消息 echo $this->receiveVoice($object); break; case 'video': //接收视频消息 echo $this->receiveVideo($object); break; case 'link': //接收链接消息 echo $this->receiveLink($object); break; default: break; } } //接收事件推送 private function receiveEvent($obj){ switch ($obj->Event) { //关注事件 case 'subscribe': //扫描带参数的二维码,用户未关注时,进行关注后的事件 if(!empty($obj->EventKey)){ //做相关处理 } $dataArray = array( array( "Title"=>"欢迎关注京东商城", "Description"=>"this is a test", "PicUrl"=>"http://1.zhangjinyu1228.applinzi.com/images/furit.jpg", "Url"=>"https://www.jd.com" ), array( "Title"=>"京东超市", "Description"=>"this is a test", "PicUrl"=>"http://1.zhangjinyu1228.applinzi.com/images/1.jpg", "Url"=>"http://chaoshi.jd.com/" ), array( "Title"=>"京东超市", "Description"=>"this is a test", "PicUrl"=>"http://1.zhangjinyu1228.applinzi.com/images/2.jpg", "Url"=>"http://chaoshi.jd.com/" ), array( "Title"=>"京东超市", "Description"=>"this is a test", "PicUrl"=>"http://1.zhangjinyu1228.applinzi.com/images/3.jpg", "Url"=>"http://chaoshi.jd.com/" ), array( "Title"=>"京东超市", "Description"=>"this is a test", "PicUrl"=>"http://1.zhangjinyu1228.applinzi.com/images/4.jpg", "Url"=>"http://chaoshi.jd.com/" ), ); echo $this->replyNews($obj,$dataArray); break; //取消关注事件 case 'unsubscribe': break; //扫描带参数的二维码,用户已关注时,进行关注后的事件 case 'SCAN': //做相关的处理 break; } } //接收文本消息 private function receiveText($obj){ //获取文本消息的内容 $content = $obj->Content; //发送文本消息 return $this->replyText($obj,$content); } //发送文本消息 private function replyText($obj,$content){ $replyXml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; //返回一个进行xml数据包 $resultStr = sprintf($replyXml,$obj->FromUserName,$obj->ToUserName,time(),$content); return $resultStr; } //接收图片消息 private function receiveImage($obj) { //获取图片消息的内容 $imageArr = array( "PicUrl"=>$obj->PicUrl, "MediaId"=>$obj->MediaId ); //发送图片消息 return $this->replyImage($obj,$imageArr); } //发送图片消息 private function replyImage($obj,$imageArr){ $replyXml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[image]]></MsgType> <Image> <MediaId><![CDATA[%s]]></MediaId> </Image> </xml>"; //返回一个进行xml数据包 $resultStr = sprintf($replyXml,$obj->FromUserName,$obj->ToUserName,time(),$imageArr['MediaId']); return $resultStr; } //接收地理位置消息 private function receiveLocation($obj) { //获取地理位置消息的内容 $locationArr = array( "Location_X"=>$obj->Location_X, "Location_Y"=>"地址位置经度:".$obj->Location_Y, "Label"=>$obj->Label ); //回复文本消息 return $this->replyText($obj,$locationArr['Location_Y']); } //接收语言消息 private function receiveVoice($obj){ //获取语言消息内容 $voiceArr = array( "MediaId"=>$obj->MediaId, "Format"=>$obj->Format ); //回复语言消息 return $this->replyVoice($obj,$voiceArr); } //回复语音消息 private function replyVoice($obj,$voiceArr) { $replyXml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[voice]]></MsgType> <Voice> <MediaId><![CDATA[%s]]></MediaId> </Voice> </xml>"; //返回一个进行xml数据包 $resultStr = sprintf($replyXml,$obj->FromUserName,$obj->ToUserName,time(),$voiceArr['MediaId']); return $resultStr; } //接收视频消息 private function receiveVideo($obj){ //获取视频消息的内容 $videoArr = array( "MediaId"=>$obj->MediaId ); //回复视频消息 return $this->replyVideo($obj,$videoArr); } //回复视频消息 private function replyVideo($obj,$videoArr){ $replyXml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[video]]></MsgType> <Video> <MediaId><![CDATA[%s]]></MediaId> </Video> </xml>"; //返回一个进行xml数据包 $resultStr = sprintf($replyXml,$obj->FromUserName,$obj->ToUserName,time(),$videoArr['MediaId']); return $resultStr; } //接收链接消息 private function receiveLink($obj) { //接收链接消息的内容 $linkArr = array( "Title"=>$obj->Title, "Description"=>$obj->Description, "Url"=>$obj->Url ); //回复文本消息 return $this->replyText($obj,"你发过来的链接地址是{$linkArr['Url']}"); } //回复音乐消息 private function replyMusic($obj,$musicArr) { $replyXml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> <Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> <ThumbMediaId><![CDATA[%s]]></ThumbMediaId> </Music> </xml>"; //返回一个进行xml数据包 $resultStr = sprintf($replyXml,$obj->FromUserName,$obj->ToUserName,time(),$musicArr['Title'],$musicArr['Description'],$musicArr['MusicUrl'],$musicArr['HQMusicUrl'],$musicArr['ThumbMediaId']); return $resultStr; } //回复图文消息 private function replyNews($obj,$newsArr){ $itemStr = ""; if(is_array($newsArr)) { foreach($newsArr as $item) { $itemXml ="<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>"; $itemStr .= sprintf($itemXml,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']); } } $replyXml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>%s</ArticleCount> <Articles> {$itemStr} </Articles> </xml> "; //返回一个进行xml数据包 $resultStr = sprintf($replyXml,$obj->FromUserName,$obj->ToUserName,time(),count($newsArr)); return $resultStr; }
相关文章推荐
- cocos2d-x自定义可点击/可处理事件/可接受触屏消息的Sprite
- java开发微信公众号接受并回复消息[工程代码+图片全解]
- 微信公众号消息接受与回复
- 微信开发四 接受用户普通消息与回复消息
- 【微信】微信开发(2)微信消息接受与回复
- 微信公众号的开发之 回复用户消息和事件响应(四)
- 微信开发中消息接收、回复及事件处理通用函数与php语法的详解(index.php文件)
- [c#]asp.net开发微信公众平台(4)关注事件、用户记录、回复文本消息
- java微信公众号开发1.普通消息的接受并回复
- 微信公众号接受文本消息与自动回复消息
- PHP微信公众平台开:获取接口调用凭据access_token、订阅事件推、消息回复
- 微信开发四 接受用户普通消息与回复消息
- C++如何通过一个响应事件接受多个控件消息
- 微信公众号开发(2)---接收订阅事件推送并回复消息
- java微信开发之接受消息回复图片或者文本
- cocos2d-x学习之菜单与事件,消息触发机制
- Erlang接受消息循环
- 微信公众号开发--接收与回复消息(Java)
- 加载性能计数器时记录事件 ID 2003 警告消息 W3CTRS ID2003
- C#委托、事件、消息(入门级)(再续)