微信公众平台开发接口将不再支持多图文。 官方消息如下: > 微信公众平台对多图文消息类型进行了调整。从2018年10月12日起,被动回复消息与客服消息接口的图文消息类型中图文数目只能为一条,请知悉。 微信团队 2018年09月30日 根据这一规则,本书中涉及回复多图文消息的SDK需要进行修改,**只需要修改消息类型和客服接口的2个SDK**,其他地方均保持不动,修改方法如下。 1、被动回复图文消息的修改 在回复图文消息的时候,检查图文是单图文还是多图文,如果是多图文,则转换为文字进行回复,如果多图文中有链接,则做成A标签。如果是单图文则保持不变。代码如下 ``` //回复图文消息 private function transmitNews($object, $newsArray) { if(!is_array($newsArray)){ return ""; } //多图文转文本回复 if (count($newsArray) > 1){ $content = ""; foreach ($newsArray as &$item) { $content .= "\n\n".(empty($item["Url"]) ? $item["Title"] : "<a href='".$item["Url"]."'>".$item["Title"]."</a>"); } $result = $this->transmitText($object, trim($content)); return $result; } $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; } ``` 2、客服消息的修改方法 思路和上述一样,将多图文转为文字或A标签。 代码如下 ``` public function send_custom_message($touser, $type, $data) { $msg = array('touser' =>$touser); $msg['msgtype'] = $type; switch($type) { case 'text': $msg[$type] = array('content'=>urlencode($data)); break; case 'news': if (count($data) == 1){ $data2 = array(); foreach ($data as &$item) { $item2 = array(); foreach ($item as $k => $v) { $item2[strtolower($k)] = urlencode($v); } $data2[] = $item2; } $msg[$type] = array('articles'=>$data2); }else{ //多图文转文本 $content = ""; foreach ($data as &$item) { $url = isset($item["url"]) ? $item["url"] : $item["Url"]; $title = isset($item["title"]) ? $item["title"]:$item["Title"]; $content .= "\n\n".(empty($url) ? $title : "<a href='".$url."'>".$title."</a>"); } $msg['msgtype'] = 'text'; $msg['text'] = array('content'=>urlencode(trim($content))); } break; case 'music': case 'image': case 'voice': case 'video': case 'wxcard': $msg[$type] = $data; break; default: $msg['text'] = array('content'=>urlencode("不支持的消息类型 ".$type)); break; } $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token; return $this->http_request($url, urldecode(json_encode($msg))); } ```