Media.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/WeChatDeveloper
  12. // +----------------------------------------------------------------------
  13. namespace WeChat;
  14. use WeChat\Contracts\BasicWeChat;
  15. use WeChat\Contracts\Tools;
  16. use WeChat\Exceptions\InvalidResponseException;
  17. /**
  18. * 微信素材管理
  19. * Class Media
  20. * @package WeChat
  21. */
  22. class Media extends BasicWeChat
  23. {
  24. /**
  25. * 新增临时素材
  26. * @param string $filename 文件名称
  27. * @param string $type 媒体文件类型(image|voice|video|thumb)
  28. * @return array
  29. * @throws Exceptions\LocalCacheException
  30. * @throws InvalidResponseException
  31. */
  32. public function add($filename, $type = 'image')
  33. {
  34. if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
  35. throw new InvalidResponseException('Invalid Media Type.', '0');
  36. }
  37. $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type={$type}";
  38. $this->registerApi($url, __FUNCTION__, func_get_args());
  39. return $this->httpPostForJson($url, ['media' => Tools::createCurlFile($filename)], false);
  40. }
  41. /**
  42. * 获取临时素材
  43. * @param string $media_id
  44. * @param string $outType 返回处理函数
  45. * @return array|string
  46. * @throws Exceptions\LocalCacheException
  47. * @throws InvalidResponseException
  48. */
  49. public function get($media_id, $outType = null)
  50. {
  51. $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id={$media_id}";
  52. $this->registerApi($url, __FUNCTION__, func_get_args());
  53. $result = Tools::get($url);
  54. if (is_array($json = json_decode($result, true))) {
  55. if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
  56. [$this->delAccessToken(), $this->isTry = true];
  57. return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
  58. }
  59. return Tools::json2arr($result);
  60. }
  61. return is_null($outType) ? $result : $outType($result);
  62. }
  63. /**
  64. * 新增图文素材
  65. * @param array $data 文件名称
  66. * @return array
  67. * @throws Exceptions\LocalCacheException
  68. * @throws InvalidResponseException
  69. */
  70. public function addNews($data)
  71. {
  72. $url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN";
  73. $this->registerApi($url, __FUNCTION__, func_get_args());
  74. return $this->httpPostForJson($url, $data);
  75. }
  76. /**
  77. * 更新图文素材
  78. * @param string $media_id 要修改的图文消息的id
  79. * @param int $index 要更新的文章在图文消息中的位置(多图文消息时,此字段才有意义),第一篇为0
  80. * @param array $news 文章内容
  81. * @return array
  82. * @throws Exceptions\LocalCacheException
  83. * @throws InvalidResponseException
  84. */
  85. public function updateNews($media_id, $index, $news)
  86. {
  87. $data = ['media_id' => $media_id, 'index' => $index, 'articles' => $news];
  88. $url = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=ACCESS_TOKEN";
  89. $this->registerApi($url, __FUNCTION__, func_get_args());
  90. return $this->httpPostForJson($url, $data);
  91. }
  92. /**
  93. * 上传图文消息内的图片获取URL
  94. * @param mixed $filename
  95. * @return array
  96. * @throws Exceptions\LocalCacheException
  97. * @throws InvalidResponseException
  98. */
  99. public function uploadImg($filename)
  100. {
  101. $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN";
  102. $this->registerApi($url, __FUNCTION__, func_get_args());
  103. return $this->httpPostForJson($url, ['media' => Tools::createCurlFile($filename)], false);
  104. }
  105. /**
  106. * 新增其他类型永久素材
  107. * @param mixed $filename 文件名称
  108. * @param string $type 媒体文件类型(image|voice|video|thumb)
  109. * @param array $description 包含素材的描述信息
  110. * @return array
  111. * @throws Exceptions\LocalCacheException
  112. * @throws InvalidResponseException
  113. */
  114. public function addMaterial($filename, $type = 'image', $description = [])
  115. {
  116. if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
  117. throw new InvalidResponseException('Invalid Media Type.', '0');
  118. }
  119. $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type={$type}";
  120. $this->registerApi($url, __FUNCTION__, func_get_args());
  121. return $this->httpPostForJson($url, ['media' => Tools::createCurlFile($filename), 'description' => Tools::arr2json($description)], false);
  122. }
  123. /**
  124. * 获取永久素材
  125. * @param string $media_id
  126. * @param null|string $outType 输出类型
  127. * @return array|string
  128. * @throws Exceptions\LocalCacheException
  129. * @throws InvalidResponseException
  130. */
  131. public function getMaterial($media_id, $outType = null)
  132. {
  133. $url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN";
  134. $this->registerApi($url, __FUNCTION__, func_get_args());
  135. $result = Tools::post($url, ['media_id' => $media_id]);
  136. if (is_array($json = json_decode($result, true))) {
  137. if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
  138. [$this->delAccessToken(), $this->isTry = true];
  139. return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
  140. }
  141. return Tools::json2arr($result);
  142. }
  143. return is_null($outType) ? $result : $outType($result);
  144. }
  145. /**
  146. * 删除永久素材
  147. * @param string $media_id
  148. * @return array
  149. * @throws Exceptions\LocalCacheException
  150. * @throws InvalidResponseException
  151. */
  152. public function delMaterial($media_id)
  153. {
  154. $url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=ACCESS_TOKEN";
  155. $this->registerApi($url, __FUNCTION__, func_get_args());
  156. return $this->httpPostForJson($url, ['media_id' => $media_id]);
  157. }
  158. /**
  159. * 获取素材总数
  160. * @return array
  161. * @throws Exceptions\LocalCacheException
  162. * @throws InvalidResponseException
  163. */
  164. public function getMaterialCount()
  165. {
  166. $url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=ACCESS_TOKEN";
  167. $this->registerApi($url, __FUNCTION__, func_get_args());
  168. return $this->httpGetForJson($url);
  169. }
  170. /**
  171. * 获取素材列表
  172. * @param string $type
  173. * @param int $offset
  174. * @param int $count
  175. * @return array
  176. * @throws Exceptions\LocalCacheException
  177. * @throws InvalidResponseException
  178. */
  179. public function batchGetMaterial($type = 'image', $offset = 0, $count = 20)
  180. {
  181. if (!in_array($type, ['image', 'voice', 'video', 'news'])) {
  182. throw new InvalidResponseException('Invalid Media Type.', '0');
  183. }
  184. $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN";
  185. $this->registerApi($url, __FUNCTION__, func_get_args());
  186. return $this->httpPostForJson($url, ['type' => $type, 'offset' => $offset, 'count' => $count]);
  187. }
  188. }