微信公众平台入门开发第五章:回复用户不同消息

快讯
定义回复消息处理函数 1、文本消息 private function transmitText($object, $content, $flag = 0) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%d</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); return $resultStr; } 2、图文消息 private function transmitNews($object, $arr_item, $flag = 0) { if(!is_array($arr_item)) return; $itemTpl = "    <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach ($arr_item as $item) $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['Picurl'], $item['Url']); $newsTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[]]></Content> <ArticleCount>%s</ArticleCount> <Articles> $item_str</Articles> <FuncFlag>%s</FuncFlag> </xml>"; $resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item), $flag); return $resultStr; } 3、音乐消息 private function transmitMusic($object, $musicArray, $flag = 0) { $itemTpl = "<Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music>"; $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> $item_str <FuncFlag>%d</FuncFlag> </xml>";   $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $flag); return $resultStr; } 原文:http://www.cnblogs.com/txw1958/p/wechat-tutorial.html 构造不同回复消息类型 1、回复文本 if($keyword == "文本"){ $contentStr = "这是个文本消息"; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); } 效果展示: <a target=微信开发" src="http://img.pintu360.com/wp-content/uploads/2014/03/127.jpg" width="283" height="431" /> 2、回复单图文 else if($keyword == "图文" || $keyword == "单图文"){ $dateArray = array(); $dateArray[] = array("Title"=>"单图文标题", "Description"=>"单图文内容", "Picurl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $resultStr = $this->transmitNews($object, $dateArray, $funcFlag); } 效果展示: 微信开发 3、回复多图文 else if($keyword == "多图文"){ $dateArray = array(); $dateArray[] = array("Title"=>"多图文1标题", "Description"=>"", "Picurl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $dateArray[] = array("Title"=>"多图文2标题", "Description"=>"", "Picurl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $dateArray[] = array("Title"=>"多图文3标题", "Description"=>"", "Picurl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $resultStr = $this->transmitNews($object, $dateArray, $funcFlag); } 效果展示: 微信开发 4、回复音乐 else if($keyword == "音乐"){ $musicArray = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3","HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");$resultStr = $this->transmitMusic($object, $musicArray, $funcFlag); } 效果展示: 微信开发 品途网专栏作者:方倍 责编:肖妍
本文为 品途商业评论( http://www.pintu360.com )作者:创作,责编:途小萌。欢迎转载,转载请注明作者姓名以及原文出处:http://www.pintu360.com/article/54d7017c14ec53c11660f1e8.html。不注明作者和出处品途商业评论有权追究其责任。本文仅代表作者观点,不代表品途商业评论观点。