公众平台开发(97)图⽂消息
在这篇公众平台开发教程中,我们将介绍如何灵活的使⽤图⽂消息功能。我们将只介绍被动回复消息中的图⽂消息,发送客服消息及⾼级发消息接⼝的发送的图⽂消息与本⽂介绍的图⽂消息的各情况基本⼀致。
本⽂分为以下四个部分:
1. 图⽂消息的定义
2. 图⽂消息的实现
3. 图⽂消息的类型
4. 图⽂消息的回复
⼀、图⽂消息的定义
在公众平台消息中,发送被动响应消息中的图⽂消息的XML结构如下所⽰。
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA[title1]]></Title>
<Description><![CDATA[description1]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
<item>
<Title><![CDATA[title]]></Title>
<Description><![CDATA[description]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
</Articles>
</xml>
其参数说明如下.
参数是否
必须
说明
ToUserName是接收⽅帐号(收到的OpenID)
FromUserName是开发者号
CreateTime是消息创建时间(整型)
MsgType是news
ArticleCount是图⽂消息个数,限制为10条以内
Articles是多条图⽂消息信息,默认第⼀个item为⼤图,注意,如果图⽂数
超过10,则将会⽆响应
Title否图⽂消息标题
Description否图⽂消息描述
PicUrl否图⽚链接,⽀持JPG、PNG格式,较好的效果为⼤图360*200,
⼩图200*200
海陆的个人资料
Url否点击图⽂消息跳转链接
从中可以知道,图⽂消息的类型为news,图⽂消息个数最⼤为10(注意在编辑模式中,可以设置最⼤条数为8)。超过10条,将不再响应。
多图⽂消息中会有⼤图和⼩图的区别,第⼀个item中的图⽚为⼤图,其他item中的图⽚为⼩图。
⼆、图⽂消息的实现
根据上述定义,我们定义图⽂消息的回复代码实现如下:
//回复图⽂消息
private function transmitNews($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));
return$result;
}
上述代码中,先将各item连接形成item_str,再将item_str赋值到xml模板中,组装⼀个图⽂消息。组装时,将object中的发送、接收⽅互换位置,计算出图⽂项的个数。⽽在构造图⽂消息并使⽤图⽂回复的代码如下所⽰徐圣恩
if (strstr($keyword, "单图⽂")){
$content = array();
$content[] = array("Title"=>"单图⽂标题",  "Description"=>"单图⽂内容", "PicUrl"=>"discuzli/weixin/weather/icon/cartoon.jpg", "Url" =>"mblogs/?u=txw1958");
}else if (strstr($keyword, "图⽂") || strstr($keyword, "多图⽂")){
$content = array();
$content[] = array("Title"=>"多图⽂1标题", "Description"=>"", "PicUrl"=>"discuzli/weixin/weather/icon/cartoon.jpg", "Url" =>"mblogs/?u=txw1958");
$content[] = array("Title"=>"多图⽂2标题", "Description"=>"", "PicUrl"=>"d.hiphotos.bdimg/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"mblogs/?u=txw1958");
$content[] = array("Title"=>"多图⽂3标题", "Description"=>"", "PicUrl"=>"g.hiphotos.bdimg/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"mblogs/?u=txw1958");
}
if(is_array($content)){
if (isset($content[0]['PicUrl'])){
$result = $this->transmitNews($object, $content);
}else if (isset($content['MusicUrl'])){
$result = $this->transmitMusic($object, $content);
}
}else{
$result = $this->transmitText($object, $content);
}
⼀个完整的体验代码可参考
三、图⽂消息的类型
图⽂消息从item的个数上来分,可以分为单图⽂消息和多图⽂消息,其中单图⽂消息中item数为1,多图⽂消息中item数从2~10都包括。
虽然图⽂消息只有两种类型,但其实可以通过设置不同的参数构造出更多的展⽰效果。
单图⽂消息
单图⽂消息就是⼀个图⽂消息。
下⾯代码定义⼀个基本的图⽂消息
$content = array();
$content[] = array("Title" =>"⼤学英语四六级成绩查询",
"Description" =>"点击图⽚进⼊",
"PicUrl" =>"365jia/uploads/13/0301/5130c2ff93618.jpg",
"Url" =>"israel.sinaapp/cet/index.php?openid=".$object->FromUserName);
它的回复效果如图所⽰。其特点是标题粗体显⽰,内容字体则为灰⾊显⽰,如果有图⽚,则同时显⽰⽇期。
再看⼀下不定义图⽚和链接时的情况,代码如下
$aqiArray = array();
$aqiArray[] = array("Title" =>$cityAir[0]['area']."空⽓质量",
"Description" =>"空⽓质量指数(AQI):".$cityAir[0]['aqi']."\n".
"空⽓质量等级:".$cityAir[0]['quality']."\n".
"细颗粒物(PM2.5):".$cityAir[0]['pm2_5']."\n".
"可吸⼊颗粒物(PM10):".$cityAir[0]['pm10']."\n".
"⼀氧化碳(CO):".$cityAir[0]['co']."\n".
"⼆氧化氮(NO2):".$cityAir[0]['no2']."\n".
"⼆氧化硫(SO2):".$cityAir[0]['so2']."\n".
"臭氧(O3):".$cityAir[0]['o3']."\n".
"更新时间:".preg_replace("/([a-zA-Z])/i", " ", $cityAir[0]['time_point']);
"PicUrl" =>"",
"Url" =>"");
其效果如下所⽰。
可以看到,这时,由于没有图⽚,所以也不显⽰⽇期了,另外没有带链接,所以“查看全⽂”也不显⽰了。
多图⽂
多图⽂消息⼀个最⼤的特点就是:描述内容不会在返回中显⽰,所以没有必要定义描述了。
下⾯是⼀个基本的多图⽂消息的定义
$content = array();
$content[] = array("Title"=>"多图⽂1标题", "Description"=>"", "PicUrl"=>"discuzli/weixin/weather/icon/cartoon.jpg", "Url" =>"mblogs/?u=txw1958");
$content[] = array("Title"=>"多图⽂2标题", "Description"=>"", "PicUrl"=>"d.hiphotos.bdimg/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"mblogs/?u=txw1958");
$content[] = array("Title"=>"多图⽂3标题", "Description"=>"", "PicUrl"=>"g.hiphotos.bdimg/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"mblogs/?u=txw1958");
其实现效果如下
如果觉得⾸图太⼤,占地⽅,也可以不填写。
⽐如这样的代码
$content = array();
$content[] = array("Title" =>"公众平台开发教程", "Description" =>"", "PicUrl" =>"", "Url" =>"");
$content[] = array("Title" =>"【基础⼊门】免费\n1. 申请服务器资源\n2. 启⽤开发模式\n3. 消息类型详解\n4. 获取接收消息\n5. 回复不同消息", "Description" =>"", "PicUrl" =>"e.hiphotos.bdimg/wisegame/pic/item/9e1f4134970a304e1e39 $content[] = array("Title" =>"【初级教程】双11六折促销\n1.⼩黄鸡机器⼈\n2.英语类公众账号开发", "Description" =>"", "PicUrl" =>"g.hiphotos.bdimg/wisegame/pic/item/3166d0160924ab186196512537fae6cd7b890b24.jpg",
"Url" =>"htt 其效果如下所⽰
还可以所有的图⽚都不填,都⽤于来显⽰⽂字。
⽐如如下代码
$content = array();
$content[] = array("Title" =>"欢迎关注⽅倍⼯作室","Description" =>"", "PicUrl" =>"", "Url" =>"");
$content[] = array("Title" =>"【1】新闻天⽓空⽓股票星座\n".
"【2】快递⼈品算命解梦附近苹果\n".
"【3】公交⽕车汽车航班路况违章\n".
"【4】翻译百科双语听⼒成语历史\n".
"【5】团购充值菜谱贺卡景点冬吴\n".
"【6】情侣相夫妻相亲⼦相⼥⼈味\n".
"【7】相册游戏笑话答题点歌树洞\n".
"【8】微社区四六级华强北世界杯\n\n".
"更多精彩,即将亮相,敬请期待!";, "Description" =>"", "PicUrl" =>"", "Url" =>"");
$content[] = array("Title" =>"回复对应数字查看使⽤⽅法\n发送 0 返回本菜单", "Description" =>"", "PicUrl" =>"", "Url" =>"");
其效果如下所⽰幼儿园元旦节目主持词
四、图⽂消息的回复
因为图⽂消息有更好的视觉效果,很多朋友都想要实现图⽂消息的回复。主要有以下⼏种情况
1. 关注时回复图⽂消息
放到订阅事件下就⾏了,代码如下
//接收事件消息
private function receiveEvent($object)
{
吃橘子会变阳性?$content = "";
switch ($object->Event)
{
整容失败的明星case "subscribe":
$content = array();
$content[] = array("Title"=>"多图⽂1标题", "Description"=>"", "PicUrl"=>"discuzli/weixin/weather/icon/cartoon.jpg", "Url" =>"mblogs/?u=txw1958");
$content[] = array("Title"=>"多图⽂2标题", "Description"=>"", "PicUrl"=>"d.hiphotos.bdimg/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"mblogs/?u=txw1958");
$content[] = array("Title"=>"多图⽂3标题", "Description"=>"", "PicUrl"=>"g.hiphotos.bdimg/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"mblogs/?u=txw1958");
break;
case "unsubscribe":
$content = "取消关注";
break;
}
if(is_array($content)){
if (isset($content[0])){
$result = $this->transmitNews($object, $content);
}else if (isset($content['MusicUrl'])){
$result = $this->transmitMusic($object, $content);
}
}else{
$result = $this->transmitText($object, $content);
}
return$result;
}
2. 发送关键字回复图⽂消息
通过判断关键字既可实现,
//接收⽂本消息
private function receiveText($object)
{
$keyword = trim($object->Content);
if (strstr($keyword, "单图⽂")){
$content = array();
$content[] = array("Title"=>"单图⽂标题",  "Description"=>"单图⽂内容", "PicUrl"=>"discuzli/weixin/weather/icon/cartoon.jpg", "Url" =>"mblogs/?u=txw1958");
}else if (strstr($keyword, "图⽂") || strstr($keyword, "多图⽂")){
$content = array();
$content[] = array("Title"=>"多图⽂1标题", "Description"=>"", "PicUrl"=>"discuzli/weixin/weather/icon/cartoon.jpg", "Url" =>"mblogs/?u=txw1958");
$content[] = array("Title"=>"多图⽂2标题", "Description"=>"", "PicUrl"=>"d.hiphotos.bdimg/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"mblogs/?u=txw1958");
$content[] = array("Title"=>"多图⽂3标题", "Description"=>"", "PicUrl"=>"g.hiphotos.bdimg/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"mblogs/?u=txw1958");
}else{
$content = date("Y-m-d H:i:s",time())."\n技术⽀持⽅倍⼯作室";
}
if(is_array($content)){
if (isset($content[0]['PicUrl'])){
$result = $this->transmitNews($object, $content);
}else if (isset($content['MusicUrl'])){
$result = $this->transmitMusic($object, $content);
}
}else{
$result = $this->transmitText($object, $content);
}
}
3. 点击菜单时回复图⽂消息
在菜单点击事件中响应即可
//接收事件消息
private function receiveEvent($object)
{
$content = "";
switch ($object->Event)
{
case "subscribe":
$content = "欢迎关注⽅倍⼯作室 ";
break;
case "CLICK":
switch ($object->EventKey)
{
case "COMPANY":
$content = array();
$content[] = array("Title"=>"多图⽂1标题", "Description"=>"", "PicUrl"=>"discuzli/weixin/weather/icon/cartoon.jpg", "Url" =>"mblogs/?u=txw1958");
break;
default:
$content = "点击菜单:".$object->EventKey;
break;
}
break;
}
if(is_array($content)){
if (isset($content[0])){
$result = $this->transmitNews($object, $content);
}else if (isset($content['MusicUrl'])){
借条怎么写范本图片$result = $this->transmitMusic($object, $content);
}
}else{
$result = $this->transmitText($object, $content);
}
return$result;
}
4. 回复多个图⽂消息
由于回复消息⼀次只能回复⼀条,因此要回复多个图⽂消息,就需要使⽤其他接⼝。这需要已经认证的才能拥有权限。
可以回复图⽂消息的接⼝有客服接⼝及⾼级发接⼝,如果被动回复不够,就可使⽤客服接⼝来发送。