您的位置:首页 > 编程语言 > PHP开发

第二节 消息和事件的接受和回复

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xml php 服务器 数据 函数