Home >php教程 >php手册 >微信公众平台开发(十) 消息回复总结,公众平台

微信公众平台开发(十) 消息回复总结,公众平台

WBOY
WBOYOriginal
2016-06-13 09:27:361177browse

微信公众平台开发(十) 消息回复总结,公众平台

原文地址:http://www.cnblogs.com/mchina/p/3364377.html

一、简介

微信公众平台提供了三种消息回复的格式,即文本回复、音乐回复和图文回复,在这一篇文章中,我们将对这三种消息回复的格式做一下简单讲解,然后封装成函数,以供读者使用。

二、思路分析

对于每一个POST请求,开发者在响应包中返回特定xml结构,对该消息进行响应(现支持回复文本、图文、语音、视频、音乐)。

三、文本回复

3.1 文本回复xml 结构

 <span><<span>xml<span>>
 <span><<span>ToUserName<span>><span><![CDATA[<span>toUser<span>]]><span></<span>ToUserName<span>>
 <span><<span>FromUserName<span>><span><![CDATA[<span>fromUser<span>]]><span></<span>FromUserName<span>>
 <span><<span>CreateTime<span>>12345678<span></<span>CreateTime<span>>
 <span><<span>MsgType<span>><span><![CDATA[<span>text<span>]]><span></<span>MsgType<span>>
 <span><<span>Content<span>><span><![CDATA[<span>content<span>]]><span></<span>Content<span>>
 <span></<span>xml<span>></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

3.2 结构说明

3.3 具体实施

针对上面给出的xml 结构,我们只需要在对应的位置填上内容,然后格式化输出就可以了。

说明:

ToUserName 位置上填写的是$fromUsername = $postObj->FromUserName,就是把消息返回给发送信息过来的用户,即接收方账号。

FromUserName 位置上填写的是$toUsername = $postObj->ToUserName,既是开发者微信号。

这是官方的文本回复,只需实例化它的responseMsg() 方法就可以回复 “Welcome to wechat world!” 消息了。

这里我们稍做修改,返回fromUsername 和toUsername 消息,便于读者理解以上的说明。

3.4 测试结果

3.5 封装成可调用的函数

我们可以将上面的内容封装成函数,在需要回复文本的地方直接调用,方便简洁,responseText.func.inc.php 代码如下。

<span>function _response_text(<span>$object,<span>$content<span>){
    <span>$textTpl = "<span><xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[text]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                <FuncFlag>%d</FuncFlag>
                </xml>"<span>;
    <span>$resultStr = <span>sprintf(<span>$textTpl, <span>$object->FromUserName, <span>$object->ToUserName, <span>time(), <span>$content, <span>$flag<span>);
    <span>return <span>$resultStr<span>;
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

这样,只要传入$object 和$content,然后在需要回复文本的文件中引入该文件,然后调用 _response_text() 方法,就可以直接回复文本了。 

3.6 测试代码

3.6.1 在主文件中引入回复文本的函数文件

<span>require_once 'responseText.func.inc.php';</span>

3.6.2 普通消息回复

<span>public <span>function handleText(<span>$postObj<span>)
    {
        <span>$keyword = <span>trim(<span>$postObj-><span>Content);

        <span>if(!<span>empty( <span>$keyword<span> ))
        {<br />            <span>$contentStr = "微信公众平台-文本回复功能源代码"<span>;
            <span>//<span>$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            <span>$resultStr = _response_text(<span>$postObj,<span>$contentStr<span>);
            <span>echo <span>$resultStr<span>;
        }<span>else<span>{
            <span>echo "Input something..."<span>;
        }
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

3.6.3 关注时回复

<span>public <span>function handleEvent(<span>$object<span>)
    {
        <span>$contentStr = ""<span>;
        <span>switch (<span>$object-><span>Event)
        {
            <span>case "subscribe":
                <span>$contentStr = "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"<span>;
                <span>break<span>;
            <span>default :
                <span>$contentStr = "Unknow Event: ".<span>$object-><span>Event;
                <span>break<span>;
        }
        <span>$resultStr = _response_text(<span>$object, <span>$contentStr<span>);
        <span>return <span>$resultStr<span>;
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

3.7 测试结果

回复文本成功。

四、图文回复

4.1 图文回复xml 结构

 <span><<span>xml<span>>
 <span><<span>ToUserName<span>><span><![CDATA[<span>toUser<span>]]><span></<span>ToUserName<span>>
 <span><<span>FromUserName<span>><span><![CDATA[<span>fromUser<span>]]><span></<span>FromUserName<span>>
 <span><<span>CreateTime<span>>12345678<span></<span>CreateTime<span>>
 <span><<span>MsgType<span>><span><![CDATA[<span>news<span>]]><span></<span>MsgType<span>>
 <span><<span>ArticleCount<span>>2<span></<span>ArticleCount<span>>
 <span><<span>Articles<span>>
 <span><<span>item<span>>
 <span><<span>Title<span>><span><![CDATA[<span>title1<span>]]><span></<span>Title<span>> 
 <span><<span>Description<span>><span><![CDATA[<span>description1<span>]]><span></<span>Description<span>>
 <span><<span>PicUrl<span>><span><![CDATA[<span>picurl<span>]]><span></<span>PicUrl<span>>
 <span><<span>Url<span>><span><![CDATA[<span>url<span>]]><span></<span>Url<span>>
 <span></<span>item<span>>
 <span><<span>item<span>>
 <span><<span>Title<span>><span><![CDATA[<span>title<span>]]><span></<span>Title<span>>
 <span><<span>Description<span>><span><![CDATA[<span>description<span>]]><span></<span>Description<span>>
 <span><<span>PicUrl<span>><span><![CDATA[<span>picurl<span>]]><span></<span>PicUrl<span>>
 <span><<span>Url<span>><span><![CDATA[<span>url<span>]]><span></<span>Url<span>>
 <span></<span>item<span>>
 <span></<span>Articles<span>>
 <span></<span>xml<span>> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

4.2 结构说明

类似文本回复的格式,只需要在相应的位置填上对应的内容就可以回复图文信息了。

4.3 具体实施

图文回复可以是单图文,也可以是多图文,这里我们先以单图文的案例来引导读者,然后再引出多图文。

我们将回复图文的xml 结构分解成为以下三个结构,图文头,图文体,图文尾,图文体就是图文回复时看到的标题,描述,图片URL和原文URL。

$newsTplHead = "<span><<span>xml<span>>
                <span><<span>ToUserName<span>><span><![CDATA[<span>%s<span>]]><span></<span>ToUserName<span>>
                <span><<span>FromUserName<span>><span><![CDATA[<span>%s<span>]]><span></<span>FromUserName<span>>
                <span><<span>CreateTime<span>>%s<span></<span>CreateTime<span>>
                <span><<span>MsgType<span>><span><![CDATA[<span>news<span>]]><span></<span>MsgType<span>>
                <span><<span>ArticleCount<span>>1<span></<span>ArticleCount<span>>
                <span><<span>Articles<span>><span>";
$newsTplBody = "<span><<span>item<span>>
                <span><<span>Title<span>><span><![CDATA[<span>%s<span>]]><span></<span>Title<span>> 
                <span><<span>Description<span>><span><![CDATA[<span>%s<span>]]><span></<span>Description<span>>
                <span><<span>PicUrl<span>><span><![CDATA[<span>%s<span>]]><span></<span>PicUrl<span>>
                <span><<span>Url<span>><span><![CDATA[<span>%s<span>]]><span></<span>Url<span>>
                <span></<span>item<span>><span>";
$newsTplFoot = "<span></<span>Articles<span>>
                <span><<span>FuncFlag<span>>0<span></<span>FuncFlag<span>>
                <span></<span>xml<span>>";</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

接下来,我们对三段结构分别插入对应内容:

A. $newsTplHead

$header = sprintf($newsTplHead, $object->FromUserName, $object->ToUserName, time());

B. $newsTplBody

<span>$title = $newsContent['title'];
$desc = $newsContent['description'];
$picUrl = $newsContent['picUrl'];
$url = $newsContent['url'];
$body = sprintf($newsTplBody, $title, $desc, $picUrl, $url);</span>

说明:$newsContent 是从主文件传入函数的图文数组。

C. $newsTplFoot

<span>$FuncFlag = 0;
$footer = sprintf($newsTplFoot, $FuncFlag);</span>

然后将三段进行拼接返回就可以回复单条图文了。

return $header.$body.$footer;

将以上内容写到一个函数里,命名为 _response_news() 函数,以供下面调用测试。

4.4 测试代码

4.4.1 在主文件中引入回复图文的函数文件

<span>require_once 'responseNews.func.inc.php';</span>

4.4.2 创建数组并传入

在主文件中,只需要向 _response_news() 函数中传入一个数组和$postObj 即可。

<span>$record=<span>array<span>(
    'title' =>'山塘街',
    'description' =>'山塘街东起阊门渡僧桥,西至苏州名胜虎丘山的望山桥,长约七里,所以苏州俗语说&ldquo;七里山塘到虎丘&rdquo;...',
    'picUrl' => 'http://thinkshare.duapp.com/images/suzhou.jpg',
    'url' =>'http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5NDM0NTEyMg==&appmsgid=10000046&itemidx=1&sign=9e7707d5615907d483df33ee449b378d#wechat_redirect'<span>
);

<span>$resultStr = _response_news(<span>$postObj,<span>$record<span>);
<span>echo <span>$resultStr;</span></span></span></span></span></span></span></span></span></span>

4.5 测试结果

点击进入查看

单图文回复测试成功。

4.6 多图文回复

有了上面的引导,读者应该能够想到回复多图文的思路了,就是将多维数组中的值循环放到相应的位置,然后拼接起来就可以了,下面进行讲解。 

4.6.1 获取图文条数

<span>$bodyCount = <span>count(<span>$newsContent);</span></span></span>

4.6.2 判断图文条数

因为微信限制了回复的图文消息数为10条以内,所以需要判断图文条数,如果小于10条,则图文数等于原来的图文数,如果大于等于10条,则强制限制为10条。

<span>$bodyCount = <span>$bodyCount < 10 ? <span>$bodyCount : 10;</span></span></span>

4.6.3 组织图文体

图文头和图文尾和上面单图文一样,不再赘述,主要是图文体的组织。

用foreach 循环出数组的内容并赋予图文体,并进行拼接:

<span>foreach(<span>$newsContent <span>as <span>$key => <span>$value<span>){
    <span>$body .= <span>sprintf(<span>$newsTplBody, <span>$value['title'], <span>$value['description'], <span>$value['picUrl'], <span>$value['url'<span>]);
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span>

说明:$newsContent 是从主文件传入函数的图文数组。

4.6.4 拼接并返回

<span>return <span>$header.<span>$body.<span>$footer;</span></span></span></span>

将以上内容写到一个函数里,命名为 _response_multiNews() 函数,以供下面调用测试。

4.7 测试多图文

4.7.1 在主文件中引入回复多图文的函数文件

<span>require_once 'responseMultiNews.func.inc.php';</span>

4.7.2 创建多维数组并传入

<span>$record[0]=<span>array<span>(
    'title' =>'观前街',
    'description' =>'观前街位于江苏苏州市区,是成街于清朝时期的百年商业老街,街上老店名店云集,名声远播海内外...',
    'picUrl' => 'http://joythink.duapp.com/images/suzhou.jpg',
    'url' =>'http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5NDM0NTEyMg==&appmsgid=10000052&itemidx=1&sign=90518631fd3e85dd1fde7f77c04e44d5#wechat_redirect'<span>
);

......

<span>$record[11]=<span>array<span>(
    'title' =>'平江路',
    'description' =>'平江路位于苏州古城东北,是一条傍河的小路,北接拙政园,南眺双塔,全长1606米,是苏州一条历史攸久的经典水巷。宋元时候苏州又名平江,以此名路...',
    'picUrl' => 'http://joythink.duapp.com/images/suzhouScenic/pingjianglu.jpg',
    'url' =>'http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5NDM0NTEyMg==&appmsgid=10000056&itemidx=1&sign=ef18a26ce78c247f3071fb553484d97a#wechat_redirect'<span>
);

<span>$resultStr = _response_multiNews(<span>$postObj,<span>$record<span>);
<span>echo <span>$resultStr;</span></span></span></span></span></span></span></span></span></span></span></span></span></span>

4.8 测试多图文结果

点击进入查看

测试多图文成功。

五、音乐回复

微信还提供了一种消息回复的格式,即音乐回复,下面我们编写程序测试一下。

注意:由于音乐版权的问题,现在很少有回复音乐的API,开放的API 查询出来的音乐信息也有很多是不正确的。所以在这里,我们上传几首音乐到自己的服务器空间测试。

本地文件:

测试是否能够正常播放:

5.1 音乐回复xml 结构

 <span><<span>xml<span>>
 <span><<span>ToUserName<span>><span><![CDATA[<span>toUser<span>]]><span></<span>ToUserName<span>>
 <span><<span>FromUserName<span>><span><![CDATA[<span>fromUser<span>]]><span></<span>FromUserName<span>>
 <span><<span>CreateTime<span>>12345678<span></<span>CreateTime<span>>
 <span><<span>MsgType<span>><span><![CDATA[<span>music<span>]]><span></<span>MsgType<span>>
 <span><<span>Music<span>>
 <span><<span>Title<span>><span><![CDATA[<span>TITLE<span>]]><span></<span>Title<span>>
 <span><<span>Description<span>><span><![CDATA[<span>DESCRIPTION<span>]]><span></<span>Description<span>>
 <span><<span>MusicUrl<span>><span><![CDATA[<span>MUSIC_Url<span>]]><span></<span>MusicUrl<span>>
 <span><<span>HQMusicUrl<span>><span><![CDATA[<span>HQ_MUSIC_Url<span>]]><span></<span>HQMusicUrl<span>>
 <span></<span>Music<span>>
 <span></<span>xml<span>></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

5.2 结构说明

5.3 具体实施

我们先做一个固定的歌曲回复来引导读者,然后再引出更高级别的歌曲查询回复。

5.3.1 在xml 结构的相应位置插入相应数据

<span><<span>Music<span>>
<span><<span>Title<span>><span><![CDATA[<span>Far Away From Home<span>]]><span></<span>Title<span>>
<span><<span>Description<span>><span><![CDATA[<span>Groove Coverage<span>]]><span></<span>Description<span>>
<span><<span>MusicUrl<span>><span><![CDATA[<span>http://thinkshare.duapp.com/music/10001.mp3<span>]]><span></<span>MusicUrl<span>>
<span><<span>HQMusicUrl<span>><span><![CDATA[<span>http://thinkshare.duapp.com/music/10001.mp3<span>]]><span></<span>HQMusicUrl<span>>
<span></<span>Music<span>></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

5.3.2 测试代码

<span>$resultStr = _response_music(<span>$postObj,<span>$keyword<span>);
<span>echo <span>$resultStr;                    </span></span></span></span></span></span>

5.3.3 测试结果

5.4 模拟点歌

有了上面的简单案例引导,读者应该可以想到模拟点歌的具体实现了吧,下面就来简单介绍一下。

思路:将歌曲代码和对应的歌曲名存入数据库,用户输入歌曲名,在数据库中找到歌曲名对应的歌曲编号,然后就可以生成MusicUrl 回复用户了。

5.4.1 创建数据库

建表语句及数据文件:

<span>CREATE <span>TABLE <span>IF <span>NOT <span>EXISTS<span> `tbl_music` (
  `music_id` <span>int(<span>11) <span>NOT <span>NULL<span>,
  `music_name` <span>varchar(<span>40) <span>NOT <span>NULL<span>,
  `music_singer` <span>varchar(<span>40) <span>NOT <span>NULL<span>,
  `music_lrc` <span>text <span>NOT <span>NULL<span>,
  <span>PRIMARY <span>KEY<span> (`music_id`)
) ENGINE<span>=MyISAM <span>DEFAULT CHARSET<span>=<span>utf8;

<span>INSERT <span>INTO `tbl_music` (`music_id`, `music_name`, `music_singer`, `music_lrc`) <span>VALUES<span>
(<span>10001, <span>'<span>Far Away From Home<span>', <span>'<span>Groove Coverage<span>', <span>'<span>far away from home<span>'<span>),
(<span>10002, <span>'<span>The Dawn<span>', <span>'<span>Dreamtale<span>', <span>'<span>the dawn<span>'<span>),
(<span>20002, <span>'<span>董小姐<span>', <span>'<span>宋冬野<span>', <span>'<span>董小姐<span>'<span>),
(<span>20001, <span>'<span>左边<span>', <span>'<span>杨丞琳<span>', <span>'<span>左边<span>');</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

5.4.2 _response_music() 函数编写

A. 引入数据库操作文件

<span>require_once('mysql_bae.func.php');</span>

B. 数据库操作及数据处理

<span>$query = "SELECT * FROM tbl_music WHERE music_name LIKE '%<span>$musicKeyword%'"<span>;
<span>$result = _select_data(<span>$query<span>);
<span>$rows = <span>mysql_fetch_array(<span>$result,<span> MYSQL_ASSOC);

<span>$music_id = <span>$rows[music_id];</span></span></span></span></span></span></span></span></span></span></span></span>

注: $musicKeyword 为从主文件传入的歌曲名关键字,这里使用模糊查询,只取第一条数据。

C. 判断是否查询到

<span>if(<span>$music_id <> ''<span>)
{
    <span>$music_name = <span>$rows<span>[music_name];
    <span>$music_singer = <span>$rows<span>[music_singer];
    <span>$musicUrl = "http://thinkshare.duapp.com/music/".<span>$music_id.".mp3"<span>;
    <span>$HQmusicUrl = "http://thinkshare.duapp.com/music/".<span>$music_id.".mp3"<span>;

    <span>$resultStr = <span>sprintf(<span>$musicTpl, <span>$object->FromUserName, <span>$object->ToUserName, <span>time(), <span>$music_name, <span>$music_singer, <span>$musicUrl, <span>$HQmusicUrl<span>);
    <span>return <span>$resultStr<span>;
}<span>else<span>{
    <span>return ""<span>;    
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

说明:如果查询到歌曲信息,按照xml 结构返回数据;如果未查询到,则返回空,用于主文件判断。

将以上代码封装成 _response_music() 函数并保存为responseMusic.func.inc.php 文件供主文件调用。

5.4.3 测试代码

A. 引入回复音乐和回复文本的函数文件

//引入回复音乐的函数文件
require_once 'responseMusic.func.inc.php';
//引入回复文本的函数文件
<span>require_once 'responseText.func.inc.php';</span>

B. 调用

<span>if(!<span>empty( <span>$keyword<span> ))
{
    <span>$resultStr = _response_music(<span>$postObj,<span>$keyword<span>);
    <span>if(<span>$resultStr <> ''<span>)
    {
        <span>echo <span>$resultStr<span>;
    }<span>else<span>
    {
        <span>echo _response_text(<span>$postObj,"未查询到【".<span>$keyword."】的歌曲信息!"<span>);    
    }
    
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

说明:如果查询到歌曲信息,则返回所得信息,如果未查询到,则调用 _response_text() 函数返回文本信息。

5.5 模拟点歌测试

回复音乐测试成功。

微信公众平台开发者模式回复图文消息代码,要全套的最好有文字说明的

$resultStr="\n \n \n ".time()."\n \n 1\n \n \n \n <description>\n <picurl>\n <url>\n \n \n <funcflag>0\n ";echo $resultStr;$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, time(), $msgType, $title,$desription);看了下你的这句$newsTpl应该是一个已经定义好的模版,后面是替换的参数所以可以查看下模版是否正确<br>给你个我代码里面用到的例子吧,具体里面字符串的话自己替换<br>希望您能采纳!谢谢<br><br>   <p class="header2"><span class="icon i-relatedanswer"><h3>怎在微信公众平台自动回复中设置图文并存的消息回复?</h3></span></p> <p class="best-replyer"></p> <p class="ft p1">在“高级功能“——”编辑者模式“——关键词回复”——“选图文”<br><br>二自动回复里面想是图文的话 需要进“开发者模式” 只不过开发者模式是需要接口的<br>  </p> </funcflag></url></picurl></description>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn