123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- /**
- * 微信被动回复用户消息
- * 当用户发送消息给公众号时,会产生一个POST请求,开发者可以在相应包中返回特定的xml结构来对消息进行回应
- */
- namespace wx\offiaccount\msg;
- use wx\Base;
- class ResponseMsg extends Base
- {
- /**
- * 消息回复
- * @param $data
- * @return string
- */
- public static function responseText($data)
- {
- $textTpl = "<xml>";
- $textTpl .= "<ToUserName><![CDATA[" . $data['ToUserName'] . "]]></ToUserName>";
- $textTpl .= "<FromUserName><![CDATA[" . $data['FromUserName'] . "]]></FromUserName>";
- $textTpl .= "<CreateTime>" . time() . "</CreateTime>";
- $textTpl .= "<MsgType><![CDATA[" . $data['MsgType'] . "]]></MsgType>";
- switch ($data['MsgType']) {
- case "text":
- $textTpl .= "<Content><![CDATA[" . $data['Content'] . "]]></Content>";
- break;
- case "image":
- $textTpl .= "<Image><MediaId><![CDATA[" . $data['MediaId'] . "]]></MediaId></Image>";
- break;
- case "voice":
- $textTpl .= "<Voice><MediaId><![CDATA[" . $data['MediaId'] . "]]></MediaId></Voice>";
- break;
- case "video":
- $textTpl .= "<Video>";
- $textTpl .= "<MediaId><![CDATA[" . $data['MediaId'] . "]]></MediaId>";
- if (!empty($data['Title']))
- $textTpl .= "<Title><![CDATA[" . $data['Title'] . "]]></Title>";
- if (!empty($data['Description']))
- $textTpl .= "<Description><![CDATA[" . $data['Description'] . "]]></Description>";
- $textTpl .= "</Video>";
- break;
- case "music":
- $textTpl .= "<Music>";
- if (!empty($data['Title']))
- $textTpl .= "<Title><![CDATA[" . $data['Title'] . "]]></Title>";
- if (!empty($data['Description']))
- $textTpl .= "<Description><![CDATA[" . $data['Description'] . "]]></Description>";
- if (!empty($data['MusicURL']))
- $textTpl .= "<MusicURL><![CDATA[" . $data['MusicURL'] . "]]></MusicURL>";
- if (!empty($data['HQMusicUrl']))
- $textTpl .= "<HQMusicUrl><![CDATA[" . $data['HQMusicUrl'] . "]]></HQMusicUrl>";
- $textTpl .= "<ThumbMediaId><![CDATA[" . $data['ThumbMediaId'] . "]]></ThumbMediaId>";
- $textTpl .= "</Music>";
- break;
- case "news":
- $textTpl .= "<ArticleCount><![CDATA[" . $data['ArticleCount'] . "]]></ArticleCount>";
- $textTpl .= "<Articles>";
- for ($i = 0; $i < $data['ArticleCount']; $i++) {
- $textTpl .= "<item>";
- $textTpl .= "<Title><![CDATA[" . $data['item'][$i]['Title'] . "]]></Title>";
- $textTpl .= "<Description><![CDATA[" . $data['item'][$i]['Description'] . "]]></Description>";
- $textTpl .= "<PicUrl><![CDATA[" . $data['item'][$i]['PicUrl'] . "]]></PicUrl>";
- $textTpl .= "<Url><![CDATA[" . $data['item'][$i]['Url'] . "]]></Url>";
- $textTpl .= "</item>";
- }
- $textTpl .= "</Articles>";
- break;
- default:
- break;
- }
- $textTpl .= "</xml>";
- return $textTpl;
- }
- }
|