Media.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. use WeChat\Contracts\Tools;
  18. use WeChat\Exceptions\InvalidResponseException;
  19. /**
  20. * 微信素材管理
  21. * Class Media
  22. * @package WeChat
  23. */
  24. class Media extends BasicWeChat
  25. {
  26. /**
  27. * 新增临时素材
  28. * @param string $filename 文件名称
  29. * @param string $type 媒体文件类型(image|voice|video|thumb)
  30. * @return array
  31. * @throws \WeChat\Exceptions\InvalidResponseException
  32. * @throws \WeChat\Exceptions\LocalCacheException
  33. */
  34. public function add($filename, $type = 'image')
  35. {
  36. if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
  37. throw new InvalidResponseException('Invalid Media Type.', '0');
  38. }
  39. $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type={$type}";
  40. $this->registerApi($url, __FUNCTION__, func_get_args());
  41. return $this->httpPostForJson($url, ['media' => Tools::createCurlFile($filename)], false);
  42. }
  43. /**
  44. * 获取临时素材
  45. * @param string $media_id
  46. * @param string $outType 返回处理函数
  47. * @return array|string
  48. * @throws \WeChat\Exceptions\InvalidResponseException
  49. * @throws \WeChat\Exceptions\LocalCacheException
  50. */
  51. public function get($media_id, $outType = null)
  52. {
  53. $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id={$media_id}";
  54. $this->registerApi($url, __FUNCTION__, func_get_args());
  55. $result = Tools::get($url);
  56. if (is_array($json = json_decode($result, true))) {
  57. if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
  58. [$this->delAccessToken(), $this->isTry = true];
  59. return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
  60. }
  61. return Tools::json2arr($result);
  62. }
  63. return is_null($outType) ? $result : $outType($result);
  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 array $news 文章内容
  83. * @return array
  84. * @throws \WeChat\Exceptions\InvalidResponseException
  85. * @throws \WeChat\Exceptions\LocalCacheException
  86. */
  87. public function updateNews($media_id, $index, $news)
  88. {
  89. $data = ['media_id' => $media_id, 'index' => $index, 'articles' => $news];
  90. $url = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=ACCESS_TOKEN";
  91. $this->registerApi($url, __FUNCTION__, func_get_args());
  92. return $this->httpPostForJson($url, $data);
  93. }
  94. /**
  95. * 上传图文消息内的图片获取URL
  96. * @param mixed $filename
  97. * @return array
  98. * @throws \WeChat\Exceptions\InvalidResponseException
  99. * @throws \WeChat\Exceptions\LocalCacheException
  100. */
  101. public function uploadImg($filename)
  102. {
  103. $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN";
  104. $this->registerApi($url, __FUNCTION__, func_get_args());
  105. return $this->httpPostForJson($url, ['media' => Tools::createCurlFile($filename)], false);
  106. }
  107. /**
  108. * 新增其他类型永久素材
  109. * @param mixed $filename 文件名称
  110. * @param string $type 媒体文件类型(image|voice|video|thumb)
  111. * @param array $description 包含素材的描述信息
  112. * @return array
  113. * @throws \WeChat\Exceptions\InvalidResponseException
  114. * @throws \WeChat\Exceptions\LocalCacheException
  115. */
  116. public function addMaterial($filename, $type = 'image', $description = [])
  117. {
  118. if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
  119. throw new InvalidResponseException('Invalid Media Type.', '0');
  120. }
  121. $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type={$type}";
  122. $this->registerApi($url, __FUNCTION__, func_get_args());
  123. return $this->httpPostForJson($url, ['media' => Tools::createCurlFile($filename), 'description' => Tools::arr2json($description)], false);
  124. }
  125. /**
  126. * 获取永久素材
  127. * @param string $media_id
  128. * @param null|string $outType 输出类型
  129. * @return array|string
  130. * @throws \WeChat\Exceptions\InvalidResponseException
  131. * @throws \WeChat\Exceptions\LocalCacheException
  132. */
  133. public function getMaterial($media_id, $outType = null)
  134. {
  135. $url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN";
  136. $this->registerApi($url, __FUNCTION__, func_get_args());
  137. $result = Tools::post($url, ['media_id' => $media_id]);
  138. if (is_array($json = json_decode($result, true))) {
  139. if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
  140. [$this->delAccessToken(), $this->isTry = true];
  141. return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
  142. }
  143. return Tools::json2arr($result);
  144. }
  145. return is_null($outType) ? $result : $outType($result);
  146. }
  147. /**
  148. * 删除永久素材
  149. * @param string $media_id
  150. * @return array
  151. * @throws \WeChat\Exceptions\InvalidResponseException
  152. * @throws \WeChat\Exceptions\LocalCacheException
  153. */
  154. public function delMaterial($media_id)
  155. {
  156. $url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=ACCESS_TOKEN";
  157. $this->registerApi($url, __FUNCTION__, func_get_args());
  158. return $this->httpPostForJson($url, ['media_id' => $media_id]);
  159. }
  160. /**
  161. * 获取素材总数
  162. * @return array
  163. * @throws \WeChat\Exceptions\InvalidResponseException
  164. * @throws \WeChat\Exceptions\LocalCacheException
  165. */
  166. public function getMaterialCount()
  167. {
  168. $url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=ACCESS_TOKEN";
  169. $this->registerApi($url, __FUNCTION__, func_get_args());
  170. return $this->httpGetForJson($url);
  171. }
  172. /**
  173. * 获取素材列表
  174. * @param string $type
  175. * @param int $offset
  176. * @param int $count
  177. * @return array
  178. * @throws \WeChat\Exceptions\InvalidResponseException
  179. * @throws \WeChat\Exceptions\LocalCacheException
  180. */
  181. public function batchGetMaterial($type = 'image', $offset = 0, $count = 20)
  182. {
  183. if (!in_array($type, ['image', 'voice', 'video', 'news'])) {
  184. throw new InvalidResponseException('Invalid Media Type.', '0');
  185. }
  186. $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN";
  187. $this->registerApi($url, __FUNCTION__, func_get_args());
  188. return $this->httpPostForJson($url, ['type' => $type, 'offset' => $offset, 'count' => $count]);
  189. }
  190. }