微信公众平台系列-2被动消息回复

部分代码封装:

<?php

namespace Wx\WxUtil\MessageManagement;

/**
 * Created by PhpStorm.
 * User: wangyetao
 * Date: 18-1-11
 * Time: 上午11:38
 * 被动回复用户消息
 *
 * 1 回复文本消息
 * <xml>
 *   <ToUserName>< ![CDATA[toUser] ]></ToUserName>
 *   <FromUserName>< ![CDATA[fromUser] ]></FromUserName>
 *   <CreateTime>12345678</CreateTime>
 *   <MsgType>< ![CDATA[text] ]></MsgType>
 *   <Content>< ![CDATA[你好] ]></Content>
 * </xml>
 * 2 回复图片消息
 * 3 回复语音消息
 * 4 回复视频消息
 * 5 回复音乐消息
 * 6 回复图文消息
 * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140543
 */
class ReplyMsgUtil {


    //1 回复文本消息
    public static function replyTextMsg($object, $content) {
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    </xml>";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $resultStr;
    }

    //2 回复图片消息
    public static function replyImageMsg($object, $media_id) {
        $textTpl = "<xml>
                    <ToUserName>< ![CDATA[%s] ]></ToUserName>
                    <FromUserName>< ![CDATA[%s] ]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType>< ![CDATA[image] ]></MsgType>
                    <Image>
                        <MediaId>< ![CDATA[%s] ]></MediaId>
                    </Image>
                    </xml>";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $media_id);
        return $resultStr;
    }

    //3 回复语音消息
    public static function replyVoiceMsg($object) {
        $textTpl = "";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        return $resultStr;
    }

    //4 回复视频消息
    public static function replyVideoMsg($object) {

        $textTpl = "";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        return $resultStr;
    }

    //5 回复音乐消息
    public static function replyMusicMsg($object) {
        $textTpl = "";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        return $resultStr;
    }

    //6 回复图文消息(单-多)
    public static function replyNewsMsg($object, $newsArray) {

        if (!is_array($newsArray)) {
            return;
        }
        $itemTpl = "<item>
                    <Title><![CDATA[%s]]></Title>
                    <Description><![CDATA[%s]]></Description>
                    <PicUrl><![CDATA[%s]]></PicUrl>
                    <Url><![CDATA[%s]]></Url>
                    </item>";

        $item_str = "";
        foreach ($newsArray as $item) {
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
        }
        $xmlTpl = "<xml>
                   <ToUserName><![CDATA[%s]]></ToUserName>
                   <FromUserName><![CDATA[%s]]></FromUserName>
                   <CreateTime>%s</CreateTime>
                   <MsgType><![CDATA[news]]></MsgType>
                   <ArticleCount>%s</ArticleCount>
                   <Articles>$item_str</Articles>
                   </xml>";

        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
        //NOTIC: [2] sprintf(): Too few arguments
        return $result;

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dnbug Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值