Material.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. /**
  11. * Material.php.
  12. *
  13. * @author overtrue <i@overtrue.me>
  14. * @copyright 2015 overtrue <i@overtrue.me>
  15. *
  16. * @see https://github.com/overtrue
  17. * @see http://overtrue.me
  18. */
  19. namespace EasyWeChat\Material;
  20. use EasyWeChat\Core\AbstractAPI;
  21. use EasyWeChat\Core\Exceptions\InvalidArgumentException;
  22. use EasyWeChat\Message\Article;
  23. /**
  24. * Class Material.
  25. */
  26. class Material extends AbstractAPI
  27. {
  28. /**
  29. * Allow media type.
  30. *
  31. * @var array
  32. */
  33. protected $allowTypes = ['image', 'voice', 'video', 'thumb', 'news_image'];
  34. const API_GET = 'https://api.weixin.qq.com/cgi-bin/material/get_material';
  35. const API_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/material/add_material';
  36. const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/material/del_material';
  37. const API_STATS = 'https://api.weixin.qq.com/cgi-bin/material/get_materialcount';
  38. const API_LISTS = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material';
  39. const API_NEWS_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/material/add_news';
  40. const API_NEWS_UPDATE = 'https://api.weixin.qq.com/cgi-bin/material/update_news';
  41. const API_NEWS_IMAGE_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg';
  42. /**
  43. * Upload image.
  44. *
  45. * @param string $path
  46. *
  47. * @return \EasyWeChat\Support\Collection
  48. */
  49. public function uploadImage($path)
  50. {
  51. return $this->uploadMedia('image', $path);
  52. }
  53. /**
  54. * Upload voice.
  55. *
  56. * @param string $path
  57. *
  58. * @return \EasyWeChat\Support\Collection
  59. */
  60. public function uploadVoice($path)
  61. {
  62. return $this->uploadMedia('voice', $path);
  63. }
  64. /**
  65. * Upload thumb.
  66. *
  67. * @param string $path
  68. *
  69. * @return \EasyWeChat\Support\Collection
  70. */
  71. public function uploadThumb($path)
  72. {
  73. return $this->uploadMedia('thumb', $path);
  74. }
  75. /**
  76. * Upload video.
  77. *
  78. * @param string $path
  79. * @param string $title
  80. * @param string $description
  81. *
  82. * @return \EasyWeChat\Support\Collection
  83. */
  84. public function uploadVideo($path, $title, $description)
  85. {
  86. $params = [
  87. 'description' => json_encode(
  88. [
  89. 'title' => $title,
  90. 'introduction' => $description,
  91. ], JSON_UNESCAPED_UNICODE),
  92. ];
  93. return $this->uploadMedia('video', $path, $params);
  94. }
  95. /**
  96. * Upload articles.
  97. *
  98. * @param array|Article $articles
  99. *
  100. * @return \EasyWeChat\Support\Collection
  101. */
  102. public function uploadArticle($articles)
  103. {
  104. if (!empty($articles['title']) || $articles instanceof Article) {
  105. $articles = [$articles];
  106. }
  107. $params = ['articles' => array_map(function ($article) {
  108. if ($article instanceof Article) {
  109. return $article->only([
  110. 'title', 'thumb_media_id', 'author', 'digest',
  111. 'show_cover_pic', 'content', 'content_source_url',
  112. ]);
  113. }
  114. return $article;
  115. }, $articles)];
  116. return $this->parseJSON('json', [self::API_NEWS_UPLOAD, $params]);
  117. }
  118. /**
  119. * Update article.
  120. *
  121. * @param string $mediaId
  122. * @param array $article
  123. * @param int $index
  124. *
  125. * @return \EasyWeChat\Support\Collection
  126. */
  127. public function updateArticle($mediaId, $article, $index = 0)
  128. {
  129. $params = [
  130. 'media_id' => $mediaId,
  131. 'index' => $index,
  132. 'articles' => isset($article['title']) ? $article : (isset($article[$index]) ? $article[$index] : []),
  133. ];
  134. return $this->parseJSON('json', [self::API_NEWS_UPDATE, $params]);
  135. }
  136. /**
  137. * Upload image for article.
  138. *
  139. * @param string $path
  140. *
  141. * @return \EasyWeChat\Support\Collection
  142. */
  143. public function uploadArticleImage($path)
  144. {
  145. return $this->uploadMedia('news_image', $path);
  146. }
  147. /**
  148. * Fetch material.
  149. *
  150. * @param string $mediaId
  151. *
  152. * @return mixed
  153. */
  154. public function get($mediaId)
  155. {
  156. $response = $this->getHttp()->json(self::API_GET, ['media_id' => $mediaId]);
  157. foreach ($response->getHeader('Content-Type') as $mime) {
  158. if (preg_match('/(image|video|audio)/i', $mime)) {
  159. return $response->getBody();
  160. }
  161. }
  162. $json = $this->getHttp()->parseJSON($response);
  163. // XXX: 微信开发这帮混蛋,尼玛文件二进制输出不带header,简直日了!!!
  164. if (!$json) {
  165. return $response->getBody();
  166. }
  167. $this->checkAndThrow($json);
  168. return $json;
  169. }
  170. /**
  171. * Delete material by media ID.
  172. *
  173. * @param string $mediaId
  174. *
  175. * @return \EasyWeChat\Support\Collection
  176. */
  177. public function delete($mediaId)
  178. {
  179. return $this->parseJSON('json', [self::API_DELETE, ['media_id' => $mediaId]]);
  180. }
  181. /**
  182. * List materials.
  183. *
  184. * example:
  185. *
  186. * {
  187. * "total_count": TOTAL_COUNT,
  188. * "item_count": ITEM_COUNT,
  189. * "item": [{
  190. * "media_id": MEDIA_ID,
  191. * "name": NAME,
  192. * "update_time": UPDATE_TIME
  193. * },
  194. * // more...
  195. * ]
  196. * }
  197. *
  198. * @param string $type
  199. * @param int $offset
  200. * @param int $count
  201. *
  202. * @return array
  203. */
  204. public function lists($type, $offset = 0, $count = 20)
  205. {
  206. $params = [
  207. 'type' => $type,
  208. 'offset' => intval($offset),
  209. 'count' => min(20, $count),
  210. ];
  211. return $this->parseJSON('json', [self::API_LISTS, $params]);
  212. }
  213. /**
  214. * Get stats of materials.
  215. *
  216. * @return array
  217. */
  218. public function stats()
  219. {
  220. return $this->parseJSON('get', [self::API_STATS]);
  221. }
  222. /**
  223. * Upload material.
  224. *
  225. * @param string $type
  226. * @param string $path
  227. * @param array $form
  228. *
  229. * @return \EasyWeChat\Support\Collection
  230. *
  231. * @throws InvalidArgumentException
  232. */
  233. protected function uploadMedia($type, $path, array $form = [])
  234. {
  235. if (!file_exists($path) || !is_readable($path)) {
  236. throw new InvalidArgumentException("File does not exist, or the file is unreadable: '$path'");
  237. }
  238. $form['type'] = $type;
  239. return $this->parseJSON('upload', [$this->getAPIByType($type), ['media' => $path], $form]);
  240. }
  241. /**
  242. * Get API by type.
  243. *
  244. * @param string $type
  245. *
  246. * @return string
  247. */
  248. public function getAPIByType($type)
  249. {
  250. switch ($type) {
  251. case 'news_image':
  252. $api = self::API_NEWS_IMAGE_UPLOAD;
  253. break;
  254. default:
  255. $api = self::API_UPLOAD;
  256. }
  257. return $api;
  258. }
  259. }