ResponseMsg.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * 微信被动回复用户消息
  4. * 当用户发送消息给公众号时,会产生一个POST请求,开发者可以在相应包中返回特定的xml结构来对消息进行回应
  5. */
  6. namespace wx\offiaccount\msg;
  7. use wx\Base;
  8. class ResponseMsg extends Base
  9. {
  10. /**
  11. * 消息回复
  12. * @param $data
  13. * @return string
  14. */
  15. public static function responseText($data)
  16. {
  17. $textTpl = "<xml>";
  18. $textTpl .= "<ToUserName><![CDATA[" . $data['ToUserName'] . "]]></ToUserName>";
  19. $textTpl .= "<FromUserName><![CDATA[" . $data['FromUserName'] . "]]></FromUserName>";
  20. $textTpl .= "<CreateTime>" . time() . "</CreateTime>";
  21. $textTpl .= "<MsgType><![CDATA[" . $data['MsgType'] . "]]></MsgType>";
  22. switch ($data['MsgType']) {
  23. case "text":
  24. $textTpl .= "<Content><![CDATA[" . $data['Content'] . "]]></Content>";
  25. break;
  26. case "image":
  27. $textTpl .= "<Image><MediaId><![CDATA[" . $data['MediaId'] . "]]></MediaId></Image>";
  28. break;
  29. case "voice":
  30. $textTpl .= "<Voice><MediaId><![CDATA[" . $data['MediaId'] . "]]></MediaId></Voice>";
  31. break;
  32. case "video":
  33. $textTpl .= "<Video>";
  34. $textTpl .= "<MediaId><![CDATA[" . $data['MediaId'] . "]]></MediaId>";
  35. if (!empty($data['Title']))
  36. $textTpl .= "<Title><![CDATA[" . $data['Title'] . "]]></Title>";
  37. if (!empty($data['Description']))
  38. $textTpl .= "<Description><![CDATA[" . $data['Description'] . "]]></Description>";
  39. $textTpl .= "</Video>";
  40. break;
  41. case "music":
  42. $textTpl .= "<Music>";
  43. if (!empty($data['Title']))
  44. $textTpl .= "<Title><![CDATA[" . $data['Title'] . "]]></Title>";
  45. if (!empty($data['Description']))
  46. $textTpl .= "<Description><![CDATA[" . $data['Description'] . "]]></Description>";
  47. if (!empty($data['MusicURL']))
  48. $textTpl .= "<MusicURL><![CDATA[" . $data['MusicURL'] . "]]></MusicURL>";
  49. if (!empty($data['HQMusicUrl']))
  50. $textTpl .= "<HQMusicUrl><![CDATA[" . $data['HQMusicUrl'] . "]]></HQMusicUrl>";
  51. $textTpl .= "<ThumbMediaId><![CDATA[" . $data['ThumbMediaId'] . "]]></ThumbMediaId>";
  52. $textTpl .= "</Music>";
  53. break;
  54. case "news":
  55. $textTpl .= "<ArticleCount><![CDATA[" . $data['ArticleCount'] . "]]></ArticleCount>";
  56. $textTpl .= "<Articles>";
  57. for ($i = 0; $i < $data['ArticleCount']; $i++) {
  58. $textTpl .= "<item>";
  59. $textTpl .= "<Title><![CDATA[" . $data['item'][$i]['Title'] . "]]></Title>";
  60. $textTpl .= "<Description><![CDATA[" . $data['item'][$i]['Description'] . "]]></Description>";
  61. $textTpl .= "<PicUrl><![CDATA[" . $data['item'][$i]['PicUrl'] . "]]></PicUrl>";
  62. $textTpl .= "<Url><![CDATA[" . $data['item'][$i]['Url'] . "]]></Url>";
  63. $textTpl .= "</item>";
  64. }
  65. $textTpl .= "</Articles>";
  66. break;
  67. default:
  68. break;
  69. }
  70. $textTpl .= "</xml>";
  71. return $textTpl;
  72. }
  73. }