SendMsg.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Http\Controllers\Wechat;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. class SendMsg extends Controller
  6. {
  7. //
  8. /**
  9. * 发送文本消息
  10. * @param object $object
  11. * @return string
  12. */
  13. public static function text($object)
  14. {
  15. $template = '<xml>
  16. <ToUserName><![CDATA[%s]]></ToUserName>
  17. <FromUserName><![CDATA[%s]]></FromUserName>
  18. <CreateTime>%d</CreateTime>
  19. <MsgType><![CDATA[text]]></MsgType>
  20. <Content><![CDATA[%s]]></Content>
  21. <FuncFlag>0</FuncFlag>
  22. </xml>';
  23. $result = sprintf($template, $object->FromUserName, $object->ToUserName, $object->CreateTime, $object->Content);
  24. return $result;
  25. }
  26. public static function image($object)
  27. {
  28. $template = '<xml>
  29. <ToUserName><![CDATA[%s]]></ToUserName>
  30. <FromUserName><![CDATA[%s]]></FromUserName>
  31. <CreateTime>%d</CreateTime>
  32. <MsgType><![CDATA[image]]></MsgType>
  33. <Image><MediaId><![CDATA[%s]]></MediaId></Image>
  34. </xml>';
  35. $result = sprintf($template, $object->FromUserName, $object->ToUserName, $object->CreateTime, $object->MediaId);
  36. return $result;
  37. }
  38. public static function news($object, array $array)
  39. {
  40. $template = '<item>
  41. <Title><![CDATA[%s]]></Title>
  42. <Description><![CDATA[%s]]></Description>
  43. <PicUrl><![CDATA[%s]]></PicUrl>
  44. <Url><![CDATA[%s]]></Url>
  45. </item>';
  46. $item = null;
  47. foreach ($array as $value) {
  48. $item .= sprintf($template, @$value['Title'], @$value['Description'], @$value['PicUrl'], @$value['Url']);
  49. }
  50. $template = '<xml>
  51. <ToUserName><![CDATA[%s]]></ToUserName>
  52. <FromUserName><![CDATA[%s]]></FromUserName>
  53. <CreateTime>%d</CreateTime>
  54. <MsgType><![CDATA[news]]></MsgType>
  55. <ArticleCount>%d</ArticleCount>
  56. <Articles>%s</Articles>
  57. </xml>';
  58. $result = sprintf(
  59. $template,
  60. $object->FromUserName,
  61. $object->ToUserName,
  62. $object->CreateTime,
  63. count($array),
  64. $item
  65. );
  66. return $result;
  67. }
  68. }