`

php开发微信公众账号开发包开发教程四之响应消息封装

    博客分类:
  • php
阅读更多
本文主要讲解如何处理如何向用户回复文本消息,语音信息等。本文中的指的信息时被动响应回复的信息,首先参见官方文档http://mp.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E8%A2%AB%E5%8A%A8%E5%93%8D%E5%BA%94%E6%B6%88%E6%81%AF
先分析一段文本回复信息的xml代码:
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你好]]></Content>
</xml>

当用户发送信息到公众号时,微信服务器将向我们的应用发送一段xml,即前一篇文章中所讲的内容,得到信息后我们可以向用户返回一些信息,比如语音,文本等。此时我们则需要按官方api中那样发送一段xml文本即可,读完全部回复信息分析发现每种类型回复信息的xml都有如下部分
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
</xml>

<MsgType>节点后面的元素根据不同回复的消息类型而改变,所以我们可以定义基类:
/**
 * 回复消息基类
 * @author Administrator
 *
 */
abstract  class  AbstractBaseMessage {
	protected $message;
	protected  $fromUsername;
	protected  $toUsername ;
	protected  $time ;
	/**
	 * 获取消息模板
	 */
	protected abstract function getMessageContent();
	/**
	 * 
	 * @param unknown_type $fromUsername 消息发送者
	 * @param unknown_type $toUsername 消息接收者
	 */
	public function __construct($toUsername,$fromUsername){
		 $this->time = time();
		 $this->fromUsername = $fromUsername;
		 $this->toUsername = $toUsername;
//		 echo "父类方法1..\n".$this->toUsername;
	}
	public function getMessage() {
		$messageType = $this->getMessageType();
		$tpl_header = "<xml>
						<ToUserName><![CDATA[{$this->toUsername}]]></ToUserName>
						<FromUserName><![CDATA[{$this->fromUsername}]]></FromUserName>
						<CreateTime>{$this->time}</CreateTime>
						<MsgType><![CDATA[{$messageType}]]></MsgType>";
		$tpl_footer = "</xml>";
		$this->message = $tpl_header.$this->getMessageContent().$tpl_footer;
		return  $this->message;
	}
	protected  abstract  function getMessageType();
}

具体各类消息封装如下:
<?php
require_once 'message/response/AbstractBaseMessage.php';
/**
 * 图片消息
 * @author Administrator
 *
 */

class ImageMessage extends AbstractBaseMessage{
	private $mediaId ;
	/**
	 * 
	 * @param unknown_type $fromUsername
	 * @param unknown_type $toUsername
	 * @param unknown_type $mediaId 通过上传多媒体文件,得到的id。
	 */
	public function __construct($toUsername,$fromUsername,$mediaId){
		$this->mediaId = $mediaId ;
		parent::__construct($toUsername,$fromUsername);
	}
	protected function getMessageType() {
			return "image" ;
		}
/**
	 * 
	 */
	protected function getMessageContent() {
		return "<Image>
				<MediaId><![CDATA[{$this->mediaId}]]></MediaId>
				</Image>";
	}

}
/**
 * 音乐消息
 * @author Administrator
 *
 */
class MusicMessage extends AbstractBaseMessage{
	private $musicUrl ;
	private $hQMusicUrl ;
	private $thumbMediaId ;
	private $title;
	private $description;
	/**
	 * 
	 * @param unknown_type $fromUsername
	 * @param unknown_type $toUsername
	 * @param unknown_type $title 音乐标题
	 * @param unknown_type $description音乐描述
	 * @param unknown_type $musicUrl音乐链接
	 * @param unknown_type $hQMusicUrl高质量音乐链接,WIFI环境优先使用该链接播放音乐
	 * @param unknown_type $thumbMediaId缩略图的媒体id,通过上传多媒体文件,得到的id
	 */
	public function __construct($toUsername,$fromUsername,$title,$description,$musicUrl,$hQMusicUrl,$thumbMediaId){
		$this->title = $title;
		$this->description = $title;
		$this->musicUrl = $musicUrl ;
		$this->hQMusicUrl = $hQMusicUrl;
		$this->thumbMediaId = $thumbMediaId;
		parent::__construct($toUsername,$fromUsername);
	}
	protected function getMessageType() {
			return "music" ;
		}
/**
	 * 
	 */
	protected function getMessageContent() {
		if($this->thumbMediaId == null){
			return "<Music>
					<Title><![CDATA[{$this->title}]]></Title>
					<Description><![CDATA[{$this->description}]]></Description>
					<MusicUrl><![CDATA[{$this->musicUrl}]]></MusicUrl>
					<HQMusicUrl><![CDATA[{$this->hQMusicUrl}]]></HQMusicUrl>
					</Music>";
		}
		return "<Music>
				<Title><![CDATA[{$this->title}]]></Title>
				<Description><![CDATA[{$this->description}]]></Description>
				<MusicUrl><![CDATA[{$this->musicUrl}]]></MusicUrl>
				<HQMusicUrl><![CDATA[{$this->hQMusicUrl}]]></HQMusicUrl>
				<ThumbMediaId><![CDATA[{$this->thumbMediaId}]]></ThumbMediaId>
				</Music>";
	}

}
/**
 * 图片文本信息
 * @author Administrator
 *
 */
class TextImageMessage extends AbstractBaseMessage{
	private $params ;
	/**
	 * 
	 * @param unknown_type $fromUsername
	 * @param unknown_type $toUsername
	 * @param unknown_type $params
	 * @param unknown_type $description
	 * @param unknown_type $picUrl
	 * @param unknown_type $url
	 */
	public function __construct(){
		$args  = func_get_args();
		$arglen = count($args);
		if($arglen ==3) {
			call_user_func_array(array($this,"__construct1"),$args);		
		}else if($arglen == 6){
			call_user_func_array(array($this,"__construct2"),$args);	
		}else {
			die("args length is not 3 or 6");
		}
		
	}
	/**
	 * 
	 * @param unknown_type $fromUsername
	 * @param unknown_type $toUsername
	 * @param unknown_type $params ArticleItem或ArticleItem数组
	 */
	public function __construct1($toUsername,$fromUsername,$params) {
		if(!is_array($params) && !($params instanceof ArticleItem)){
				die("Argument 3 must be an instance of Array or ArticleItem");
			}
		$this->params = $params ;
		parent::__construct($toUsername,$fromUsername);
	}
	/**
	 * 
	 * @param unknown_type $fromUsername
	 * @param unknown_type $toUsername
	 * @param unknown_type $title
	 * @param unknown_type $description
	 * @param unknown_type $picUrl
	 * @param unknown_type $url
	 */
	public function __construct2($toUsername,$fromUsername,$title,$description,$picUrl,$url) {
			$this->title = $title;
			$this->description =$description;
			$this->picUrl = $picUrl;
			$this->url = $url;
			parent::__construct($toUsername,$fromUsername);
	}
	protected function getMessageType() {
			return "news" ;
		}
	
/**
	 * 
	 */
	protected function getMessageContent() {
		print_r($this->params);
		if(is_array($this->params)){
			$len = count($this->params) ;
			$str = "<ArticleCount>{$len}</ArticleCount>
					<Articles>";
		 		 for ($i=0;$i<count($this->params);$i++){
		 		 	if($this->params[$i] instanceof ArticleItem){
		 		 		$str=$str."<item>
										<Title><![CDATA[{$this->params[$i]->tilte}]]></Title> 
										<Description><![CDATA[{$this->params[$i]->description}]]></Description>
										<PicUrl><![CDATA[{$this->params[$i]->picUrl}]]></PicUrl>
										<Url><![CDATA[{$this->params[$i]->url}]]></Url>
									</item>" ;
		 		 	}else {
		 		 		die("数组中第{$i}个元素不是 ArticleItem类型");
		 		 	}
			    }
						
			$str=$str."</Articles>";
			return $str;
		}elseif ($this->params instanceof ArticleItem){
			return "<ArticleCount>1</ArticleCount>
					<Articles>
						<item>
						<Title><![CDATA[{$this->params->tilte}]]></Title> 
						<Description><![CDATA[{$this->params->description}]]></Description>
						<PicUrl><![CDATA[{$this->params->picUrl}]]></PicUrl>
						<Url><![CDATA[{$this->params->url}]]></Url>
						</item>
					</Articles>";
		}else {
			return "<ArticleCount>1</ArticleCount>
					<Articles>
						<item>
						<Title><![CDATA[{$this->title}]]></Title> 
						<Description><![CDATA[{$this->description}]]></Description>
						<PicUrl><![CDATA[{$this->picUrl}]]></PicUrl>
						<Url><![CDATA[{$this->url}]]></Url>
						</item>
					</Articles>";
		}
	}

}
/**
 * 文本消息
 * @author Administrator
 *
 */
class TextMessage extends AbstractBaseMessage{
	private $content ;
	/**
	 * 
	 * @param unknown_type $fromUsername
	 * @param unknown_type $toUsername
	 * @param unknown_type $content 消息内容
	 */
	public function __construct($toUsername,$fromUsername,$content){
//		 echo "子类方法2..\n";
		$this->content = $content ;
		parent::__construct($toUsername,$fromUsername);
	}
/**
	 * 
	 */
	protected function getMessageContent() {
		return "<Content><![CDATA[{$this->content}]]></Content>";
	}

/**
	 * 
	 */
	protected function getMessageType() {
		return "text" ;
	}


}
/**
 * 视频消息
 * @author Administrator
 *
 */
class VideoMessage extends AbstractBaseMessage{
	private $mediaId ;
	private $title;
	private $description;
	/**
	 * 
	 * @param unknown_type $fromUsername
	 * @param unknown_type $toUsername
	 * @param unknown_type $mediaId 通过上传多媒体文件,得到的id。
	 * @param unknown_type $title 视频消息的标题
	 * @param unknown_type $description 视频消息的描述
	 */
	public function __construct($toUsername,$fromUsername,$mediaId,$title,$description){
		$this->mediaId = $mediaId ;
		$this->title = $title;
		$this->description = $title;
		parent::__construct($toUsername,$fromUsername);
	}
	protected function getMessageType() {
			return "video" ;
		}
/**
	 * 
	 */
	protected function getMessageContent() {
		return "<Video>
			<MediaId><![CDATA[{$this->mediaId}]]></MediaId>
			<Title><![CDATA[{$this->title}]]></Title>
			<Description><![CDATA[{$this->description}]]></Description>
			</Video> ";
	}

}
/**
 * 语音消息
 * @author Administrator
 *
 */
class VoiceMessage extends AbstractBaseMessage{
	private $mediaId ;
	/**
	 * 
	 * @param unknown_type $fromUsername
	 * @param unknown_type $toUsername
	 * @param unknown_type $mediaId 通过上传多媒体文件,得到的id。
	 */
	public function __construct($toUsername,$fromUsername,$mediaId){
		$this->mediaId = $mediaId ;
		parent::__construct($toUsername,$fromUsername);
	}
	protected function getMessageType() {
			return "voice" ;
		}
/**
	 * 
	 */
	protected function getMessageContent() {
		return "<Voice>
				<MediaId><![CDATA[{$this->mediaId}]]></MediaId>
				</Voice>";
	}

}

发送图文消息时候如果有个条的时候需要使用的工具类:
/**
 * 图片文本消息项
 * @author Administrator
 *
 */
class ArticleItem {
	public $tilte;
	public $description;
	public $picUrl;
	public $url;
	/**
	 * 图片
	 * @param unknown_type $tilte
	 * @param unknown_type $description
	 * @param unknown_type $picUrl
	 * @param unknown_type $url
	 */
	public function __construct($tilte,$description,$picUrl,$url){
		$this->tilte = $tilte;;
		$this->description = $description;
		$this->picUrl = $picUrl;
		$this->url=$url;
	}
}

然后定义一个消息发送工具类:
class MessageUtil {
	/**
	 * 不可实例化
	 */
	private function __construct(){}
/**
     * 发送文本消息
     * @param unknown_type $fromUsername
     * @param unknown_type $toUsername
     * @param unknown_type $time
     * @param unknown_type $contentStr
     */
    public static function sendTextMessage($toUsername, $fromUsername, $contentStr){
		$textMessage = new TextMessage($toUsername,$fromUsername,$contentStr);
		echo $textMessage->getMessage();
    }
    /**
     * 发送一条图文信息
     * @param $fromUsername
     * @param $toUsername
     * @param $time
     * @param $title
     */
    public static function sendOneTextImageMessage($toUsername,$fromUsername,$title,$description, $picUlr,$url) {
    	$message = new TextImageMessage($toUsername,$fromUsername,$title,$description, $picUlr,$url);
    	echo $message->getMessage();
    }
    /**
     * 发送多条文本图片消息
     * @param unknown_type $fromUsername
     * @param unknown_type $toUsername
     * @param Array $items ArticleItem数组
     */
    public static  function sendMoreTextImageMessage($toUsername,$fromUsername, $items) {
    	$message = new TextImageMessage($toUsername,$fromUsername,$items);
    	echo $message->getMessage();
    }
    /**
     * 发送声音消息
     * @param unknown_type $toUsername
     * @param unknown_type $fromUsername
     * @param unknown_type $mediaId
     */
    public static function sendVoiceMessage($toUsername,$fromUsername,  $mediaId) {
    	$message = new VoiceMessage($toUsername,$fromUsername,$mediaId);
    	echo $message->getMessage();
    }
    /**
     * 发送视频消息
     * @param unknown_type $fromUsername
     * @param unknown_type $toUsername
     * @param unknown_type $mediaId
     * @param unknown_type $title
     * @param unknown_type $description
     */
    public static function sendVideoMessage($fromUsername,$toUsername,$mediaId,$title,$description) {
    	$message = new VideoMessage($fromUsername,$toUsername,$mediaId,$title,$description);
    	echo $message->getMessage();
    }
    /**
     * 发送音乐消息
     * @param $fromUsername
     * @param $toUsername
     * @param $title
     * @param $description
     * @param $musicUrl
     * @param $hQMusicUrl
     * @param $thumbMediaId
     */
    public static function sendMusicMessage($fromUsername,$toUsername,$title,$description,$musicUrl,$hQMusicUrl,$thumbMediaId) {
    	$message = new MusicMessage($fromUsername,$toUsername,$title,$description,$musicUrl,$hQMusicUrl,$thumbMediaId);
    	echo $message->getMessage();
    }
    /**
     * 发送图片消息
     * @param unknown_type $fromUsername
     * @param unknown_type $toUsername
     * @param unknown_type $mediaId
     */
    public static  function sendImageMessage($fromUsername,$toUsername,$mediaId) {
    	$message = new ImageMessage($fromUsername,$toUsername,$mediaId);
    	echo $message->getMessage();
    }
    
}

到此发送消息的类已经封装好了,我们需要发送一条文本信息的时候只需要MessageUtil中的方法即可。需要了解更多请继续关注
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics