Draft.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WeChat;
  16. use WeChat\Contracts\BasicWeChat;
  17. /**
  18. * 微信草稿箱管理
  19. * Class Draft
  20. * @author taoxin
  21. * @package WeChat
  22. */
  23. class Draft extends BasicWeChat
  24. {
  25. /**
  26. * 新建草稿
  27. * @param $articles
  28. * @return array
  29. * @throws \WeChat\Exceptions\InvalidResponseException
  30. * @throws \WeChat\Exceptions\LocalCacheException
  31. */
  32. public function add($articles)
  33. {
  34. $url = "https://api.weixin.qq.com/cgi-bin/draft/add?access_token=ACCESS_TOKEN";
  35. $this->registerApi($url, __FUNCTION__, func_get_args());
  36. return $this->httpPostForJson($url, ['articles' => $articles]);
  37. }
  38. /**
  39. * 获取草稿
  40. * @param string $media_id
  41. * @param string $outType 返回处理函数
  42. * @return array
  43. * @throws \WeChat\Exceptions\InvalidResponseException
  44. * @throws \WeChat\Exceptions\LocalCacheException
  45. */
  46. public function get($media_id, $outType = null)
  47. {
  48. $url = "https://api.weixin.qq.com/cgi-bin/draft/get?access_token=ACCESS_TOKEN";
  49. $this->registerApi($url, __FUNCTION__, func_get_args());
  50. return $this->httpPostForJson($url, ['media_id' => $media_id]);
  51. }
  52. /**
  53. * 删除草稿
  54. * @param string $media_id
  55. * @return array
  56. * @throws \WeChat\Exceptions\InvalidResponseException
  57. * @throws \WeChat\Exceptions\LocalCacheException
  58. */
  59. public function delete($media_id)
  60. {
  61. $url = "https://api.weixin.qq.com/cgi-bin/draft/delete?access_token=ACCESS_TOKEN";
  62. $this->registerApi($url, __FUNCTION__, func_get_args());
  63. return $this->httpPostForJson($url, ['media_id' => $media_id]);
  64. }
  65. /**
  66. * 新增图文素材
  67. * @param array $data 文件名称
  68. * @return array
  69. * @throws \WeChat\Exceptions\InvalidResponseException
  70. * @throws \WeChat\Exceptions\LocalCacheException
  71. */
  72. public function addNews($data)
  73. {
  74. $url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN";
  75. $this->registerApi($url, __FUNCTION__, func_get_args());
  76. return $this->httpPostForJson($url, $data);
  77. }
  78. /**
  79. * 修改草稿
  80. * @param string $media_id 要修改的图文消息的id
  81. * @param int $index 要更新的文章在图文消息中的位置(多图文消息时,此字段才有意义),第一篇为0
  82. * @param $articles
  83. * @return array
  84. * @throws \WeChat\Exceptions\InvalidResponseException
  85. * @throws \WeChat\Exceptions\LocalCacheException
  86. */
  87. public function update($media_id, $index, $articles)
  88. {
  89. $data = ['media_id' => $media_id, 'index' => $index, 'articles' => $articles];
  90. $url = "https://api.weixin.qq.com/cgi-bin/draft/update?access_token=ACCESS_TOKEN";
  91. $this->registerApi($url, __FUNCTION__, func_get_args());
  92. return $this->httpPostForJson($url, $data);
  93. }
  94. /**
  95. * 获取草稿总数
  96. * @return array
  97. * @throws \WeChat\Exceptions\InvalidResponseException
  98. * @throws \WeChat\Exceptions\LocalCacheException
  99. */
  100. public function getCount()
  101. {
  102. $url = "https://api.weixin.qq.com/cgi-bin/draft/count?access_token=ACCESS_TOKEN";
  103. $this->registerApi($url, __FUNCTION__, func_get_args());
  104. return $this->httpGetForJson($url);
  105. }
  106. /**
  107. * 获取草稿列表
  108. * @param int $offset 从全部素材的该偏移位置开始返回,0表示从第一个素材返回
  109. * @param int $count 返回素材的数量,取值在1到20之间
  110. * @param int $no_content 1 表示不返回 content 字段,0 表示正常返回,默认为 0
  111. * @return array
  112. * @throws \WeChat\Exceptions\InvalidResponseException
  113. * @throws \WeChat\Exceptions\LocalCacheException
  114. */
  115. public function batchGet($offset = 0, $count = 20, $no_content = 0)
  116. {
  117. $url = "https://api.weixin.qq.com/cgi-bin/draft/batchget?access_token=ACCESS_TOKEN";
  118. $this->registerApi($url, __FUNCTION__, func_get_args());
  119. return $this->httpPostForJson($url, ['no_content' => $no_content, 'offset' => $offset, 'count' => $count]);
  120. }
  121. }