国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
7.4KB

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