`

php开发微信公众账号开发包开发教程三之请求消息封装

    博客分类:
  • php
阅读更多
本章讲解如何接收消息,当用户发送消息给公众号时,微信服务器会将信息封装成xml然后转发给我们自己的应用服务器,再将我们响应的信息返回给用户,请参见官方apihttp://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E6%99%AE%E9%80%9A%E6%B6%88%E6%81%AFhttp://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E4%BA%8B%E4%BB%B6%E6%8E%A8%E9%80%81 根据文档内容,当微信服务器发送过来的消息大多格式如下
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[CLICK]]></Event>
<EventKey><![CDATA[EVENTKEY]]></EventKey>
</xml>

根据用户发送的消息类型,稍微有些不同,所以我们要做的事情就是根据接收的xml然后进行解析,获取消息的类型,是文本信息,语音信息,菜单事件等进行解析,阅读完全部文档后发现请求的xml中
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
是固定的,不同的是MsgType用于标识消息的类型,然后xml后面的内容稍有所不同,
所以我们定义请求消息基类:
<?php 
/**
 * 请求信息基类
 * @author Administrator
 *
 */
abstract class  AbstractBaseRequestMessage {
	public $postObj;
	public  $fromUsername;
	public  $toUsername ;
	public  $keyword ;
	public  $msgType; //事件类型 event,text voice vide 等;
	public $time ;
	/**
	 * 
	 * @param unknown_type $postObj 请求的xml对象
	 * */
	public function __construct($postObj){
		$this->postObj = $postObj;
        $this->parsePostObject();
	}
	/**
	 * 解析post对象
	 * ,根据不同的消息类型需要解析xml
	 * 对象中的更多信息时可以覆盖此方法
	 */
	protected function parsePostObject(){
		$this->fromUsername = $this->postObj->FromUserName;
        $this->toUsername = $this->postObj->ToUserName;
        $this->keyword = trim($this->postObj->Content);
        $this->msgType = $this->postObj->MsgType; //消息类型 event,text等;
        $this->time = $this->postObj->CreateTime;
	}
}


?>


然后根据文档内容定义各种请求类型
/**
 * 文本信息请求类
 * @author Administrator
 *
 */
class TextRequsetMessage extends AbstractBaseRequestMessage{
	
	
}
/**
 * 事件请求类,事件没有细分
 * @author Administrator
 *
 */
class EventRequestMessage extends AbstractBaseRequestMessage {
	public $customEvent;
	public $eventKey;
	protected function parsePostObject(){
		 $this->customEvent = $this->postObj->Event;//subscribe CLICK
          $this->eventKey = $this->postObj->EventKey ;//
          parent::parsePostObject();
		}
}
/**
 * 图片请求类
 * @author Administrator
 *
 */
class ImageRequestMessage extends AbstractBaseRequestMessage {
	public $mediaId;
	public $picUrl;
	protected function parsePostObject(){
		$this->mediaId = $this->postObj->MediaId;
		$this->picUrl = $this->postObj->PicUrl;
		parent::parsePostObject();
	}
	
}
/**
 * 语音请求类
 * @author Administrator
 *
 */
class VoiceRequestMessage extends AbstractBaseRequestMessage {
	public $mediaId;
	public $format;
	protected function parsePostObject(){
		$this->mediaId = $this->postObj->MediaId;
		$this->format = $this->postObj->Format;
		parent::parsePostObject();
	}
	
}
/**
 * 视频请求消息
 * @author Administrator
 *
 */
class VideRequestMessage extends AbstractBaseRequestMessage {
	public $mediaId;
	public $thumbMediaId;
	protected function parsePostObject(){
		$this->mediaId = $this->postObj->MediaId;
		$this->thumbMediaId = $this->postObj->ThumbMediaId;
		parent::parsePostObject();
	}
	
}
/**
 * 地理位置请求信息
 * @author Administrator
 *
 */
class LocationRequestMessage extends AbstractBaseRequestMessage {
	public $location_X;
	public $location_Y;
	public $scale;
	public $label;
	protected function parsePostObject(){
		$this->location_X = $this->postObj->Location_X;
		$this->location_Y = $this->postObj->Location_Y;
		$this->scale = $this->postObj->Scale;
		$this->label = $this->postObj->Label;
		parent::parsePostObject();
	}
	
}

到此请求消息封装完成,下一章将讲解如何封装被动回复信息
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics