1. Introduction
WeChat public platform provides three message reply formats, namely text reply, music reply and graphic reply. In this article, we will briefly explain these three message reply formats and then encapsulate them. into a function for readers to use.
2. Analysis of ideas
For each POST request, the developer returns a specific xml structure in the response package to respond to the message (now supports reply text, graphics, voice, video, and music).
3. Text reply
3.1 Text reply xml structure
<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 Structure Description
3.3 Specific implementation
For the xml structure given above, we only need to fill in the content in the corresponding position, and then format the output.
Instructions:
ToUserName is filled in with $fromUsername = $postObj->FromUserName, which is to return the message to the user who sent the message, that is, the receiver account.
FromUserName is filled in with $toUsername = $postObj->ToUserName, which is the developer’s WeChat account.
This is the official text reply. Just instantiate its responseMsg() method to reply to the "Welcome to wechat world!" message.
Here we make a slight modification and return fromUsername and toUsername messages to facilitate readers to understand the above instructions.
3.4 Test results
3.5 Encapsulated into a callable function
We can encapsulate the above content into a function and call it directly where the reply text is needed. It is convenient and concise. The code of responseText.func.inc.php is as follows.
<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>
In this way, just pass in $object and $content, then introduce the file into the file that needs to reply to the text, and then call the _response_text() method to directly reply to the text.
3.6 Test Code
3.6.1 Introduce the function file of reply text into the main file
<span>require_once 'responseText.func.inc.php';</span>
3.6.2 Ordinary message reply
<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 Reply when following
<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 test results
Reply text successfully.
4. Graphic and text reply
4.1 Image and text reply xml structure
<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 Structure Description
Similar to the text reply format, you only need to fill in the corresponding content in the corresponding position to reply to the graphic message.
4.3 Specific implementation
An image-text reply can be a single image-text or multiple images-text. Here we first use a single-image-text case to guide readers, and then introduce multiple images-text.
We decompose the xml structure of the image and text reply into the following three structures, image and text header, image and text body, image and text tail. The image and text body is the title, description, image URL and original text URL that you see when replying to the image and text. .
$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' =>'山塘街东起阊门渡僧桥,西至苏州名胜虎丘山的望山桥,长约七里,所以苏州俗语说“七里山塘到虎丘”...', '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="