微信公众号SDK开发(四)

文本消息回复,图片消息回复,音乐消息回复,语音消息回复,新闻消息回复,小视频消息回复,视频消息回复,位置消息回复

 

<?php
namespace wechat\build;

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

class Message extends Wx{

	//请求文本消息
    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 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);
    }
}

?>		

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值