微信公众号SDK开发(十)

关注事件接口对接

<?php
namespace wechat\build;

//专门处理微信消息
use wechat\Wx;

class Message extends Wx{
	//消息类型为事件类型
	const EVENT_TYPE_SUBSCRIBE='event';
	//请求文本消息
    const $MSG_TYPE_TEXT = 'text';
    //请求图片消息
    const $MSG_TYPE_IMAGE = 'image';
    //请求语音消息
    const $MSG_TYPE_VOICE = 'voice';
    //请求地理位置消息
    const $MSG_TYPE_LOCATION = 'location';
    //请求链接消息
    const $MSG_TYPE_LINK = 'link';
    //请求小视频消息
    const $MSG_TYPE_SMALL_VIDEO = 'shortvideo';
    //请求视频消息
    const $MSG_TYPE_VIDEO = 'video';	
	/**事件消息判断**/
	public function isSubscribeEvent(){
		return $this->message->Event==self::EVENT_TYPE_SUBSCRIBE && $this->message->MsgType=='event';
	}
	/**普通消息判断**/	
    public function isTextMsg(){
		return $this->message->MsgType==self::MSG_TYPE_TEXT;	
	}	
	public function isImageMsg(){
		return $this->message->MsgType==self::MSG_TYPE_IMAGE;	
	}	
	public function isVoiceMsg(){
		return $this->message->MsgType==self::MSG_TYPE_VOICE;	
	}	
	public function isLocationMsg(){
		return $this->message->MsgType==self::MSG_TYPE_LOCATION;	
	}	
	public function isLinkMsg(){
		return $this->message->MsgType==self::MSG_TYPE_LINK;	
	}	
	public function isVideoMsg(){
		return $this->message->MsgType==self::MSG_TYPE_VIDEO;	
	}	
	public function isShortVideoMsg(){
		return $this->message->MsgType==self::MSG_TYPE_SMALL_VIDEO;	
	}	
	
	
	public function text($content){
		//echo 3333;exit;
		$xml='
		<xml> 
            <ToUserName>< ![CDATA[{$message->FromUserName}] ]></ToUserName> 
            <FromUserName>< ![CDATA[{$message->ToUserName}] ]></FromUserName> 
            <CreateTime>{$time}</CreateTime> 
            <MsgType>< ![CDATA[text] ]></MsgType> 
            <Content>< ![CDATA[这是我回复的消息] ]></Content> 
        </xml>
		';
		$text=sprintf($xml,$this->message->FromUserName,$this->message->ToUserName,time(),$content);
		header('Content-Type:application/xml;Charset:utf-8');
		echo $text;
	}
	
	
	
	
	public function image($media_id)
    {
        $xml
              = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>';
        $text = sprintf($xml, $this->message->FromUserName,
            $this->message->ToUserName, time(), self::$REPLY_TYPE_IMAGE,
            $media_id);
        header('Content-type:application/xml');
        die($text);
    }

    /**
     * 回复语音消息
     *
     * @param $media_id
     */
    public function voice($media_id)
    {
        $xml
              = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Voice>
<MediaId><![CDATA[%s]]></MediaId>
</Voice>
</xml>';
        $text = sprintf($xml, $this->message->FromUserName,
            $this->message->ToUserName, time(), self::$REPLY_TYPE_VOICE,
            $media_id);
        header('Content-type:application/xml');
        die($text);
    }

    /**
     * 回复视频消息
     *
     * @param $video
     */
    public function video($video)
    {
        $xml
              = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Video>
<MediaId><![CDATA[%s]]></MediaId>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
</Video>
</xml>';
        $text = sprintf($xml, $this->message->FromUserName,
            $this->message->ToUserName, time(), self::$REPLY_TYPE_VIDEO,
            $video['media_id'], $video['title'], $video['description']);
        header('Content-type:application/xml');
        die($text);
    }

    /**
     * 回复音乐消息
     *
     * @param $music
     */
    public function music($music)
    {
        $xml
              = '<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>
<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
</Music>
</xml>';
        $text = sprintf($xml, $this->message->FromUserName,
            $this->message->ToUserName, time(), self::$REPLY_TYPE_MUSIC,
            $music['title'], $music['description'], $music['musicurl'],
            $music['hqmusicurl'], $music['thumbmediaid']);
        header('Content-type:application/xml');
        die($text);
    }

    /**
     * 回复图文信息
     *
     * @param $news
     */
    public function news($news)
    {
        $xml
               = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>
%s
</Articles>
</xml>';
        $item
               = '<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>';
        $items = '';
        foreach ((array)$news as $n) {
            $items .= sprintf($item, $n['title'], $n['discription'], $n['picurl'], $n['url']);
        }

        $text = sprintf($xml, $this->message->FromUserName,
            $this->message->ToUserName, time(), self::$REPLY_TYPE_NEWS,
            count($news), $items);

        header('Content-type:application/xml');
        die($text);
    }
}

?>		

猜你喜欢

转载自blog.csdn.net/taotaobaobei/article/details/81437725