1 | <?php |
||
30 | class Material extends AbstractAPI |
||
31 | { |
||
32 | /** |
||
33 | * Allow media type. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $allowTypes = ['image', 'voice', 'video', 'thumb', 'news_image']; |
||
38 | |||
39 | const API_GET = 'https://api.weixin.qq.com/cgi-bin/material/get_material'; |
||
40 | const API_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/material/add_material'; |
||
41 | const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/material/del_material'; |
||
42 | const API_STATS = 'https://api.weixin.qq.com/cgi-bin/material/get_materialcount'; |
||
43 | const API_LISTS = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material'; |
||
44 | const API_NEWS_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/material/add_news'; |
||
45 | const API_NEWS_UPDATE = 'https://api.weixin.qq.com/cgi-bin/material/update_news'; |
||
46 | const API_NEWS_IMAGE_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg'; |
||
47 | |||
48 | /** |
||
49 | * Upload image. |
||
50 | * |
||
51 | * @param string $path |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | 1 | public function uploadImage($path) |
|
59 | |||
60 | /** |
||
61 | * Upload voice. |
||
62 | * |
||
63 | * @param string $path |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | 1 | public function uploadVoice($path) |
|
71 | |||
72 | /** |
||
73 | * Upload thumb. |
||
74 | * |
||
75 | * @param string $path |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 1 | public function uploadThumb($path) |
|
83 | |||
84 | /** |
||
85 | * Upload video. |
||
86 | * |
||
87 | * @param string $path |
||
88 | * @param string $title |
||
89 | * @param string $description |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | 1 | public function uploadVideo($path, $title, $description) |
|
105 | |||
106 | /** |
||
107 | * Upload articles. |
||
108 | * |
||
109 | * @param array|Article $articles |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 1 | public function uploadArticle($articles) |
|
114 | { |
||
115 | 1 | if (!empty($articles['title']) || $articles instanceof Article) { |
|
116 | 1 | $articles = [$articles]; |
|
117 | 1 | } |
|
118 | |||
119 | 1 | $params = ['articles' => array_map(function ($article) { |
|
120 | 1 | if ($article instanceof Article) { |
|
121 | 1 | return $article->only([ |
|
122 | 1 | 'title', 'thumb_media_id', 'author', 'digest', |
|
123 | 1 | 'show_cover_pic', 'content', 'content_source_url', |
|
124 | 1 | ]); |
|
125 | } |
||
126 | |||
127 | 1 | return $article; |
|
128 | 1 | }, $articles)]; |
|
129 | |||
130 | 1 | return $this->parseJSON('json', [self::API_NEWS_UPLOAD, $params]); |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Update article. |
||
135 | * |
||
136 | * @param string $mediaId |
||
137 | * @param array $article |
||
138 | * @param int $index |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | 1 | public function updateArticle($mediaId, $article, $index = 0) |
|
152 | |||
153 | /** |
||
154 | * Upload image for article. |
||
155 | * |
||
156 | * @param string $path |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | 1 | public function uploadArticleImage($path) |
|
164 | |||
165 | /** |
||
166 | * Fetch material. |
||
167 | * |
||
168 | * @param string $mediaId |
||
169 | * |
||
170 | * @return mixed |
||
171 | */ |
||
172 | 1 | public function get($mediaId) |
|
193 | |||
194 | /** |
||
195 | * Delete material by media ID. |
||
196 | * |
||
197 | * @param string $mediaId |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | 1 | public function delete($mediaId) |
|
205 | |||
206 | /** |
||
207 | * List materials. |
||
208 | * |
||
209 | * example: |
||
210 | * |
||
211 | * { |
||
212 | * "total_count": TOTAL_COUNT, |
||
213 | * "item_count": ITEM_COUNT, |
||
214 | * "item": [{ |
||
215 | * "media_id": MEDIA_ID, |
||
216 | * "name": NAME, |
||
217 | * "update_time": UPDATE_TIME |
||
218 | * }, |
||
219 | * // more... |
||
220 | * ] |
||
221 | * } |
||
222 | * |
||
223 | * @param string $type |
||
224 | * @param int $offset |
||
225 | * @param int $count |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | 1 | public function lists($type, $offset = 0, $count = 20) |
|
239 | |||
240 | /** |
||
241 | * Get stats of materials. |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | 1 | public function stats() |
|
249 | |||
250 | /** |
||
251 | * Upload material. |
||
252 | * |
||
253 | * @param string $type |
||
254 | * @param string $path |
||
255 | * @param array $form |
||
256 | * |
||
257 | * @return string |
||
258 | * |
||
259 | * @throws InvalidArgumentException |
||
260 | */ |
||
261 | 5 | protected function uploadMedia($type, $path, array $form = []) |
|
271 | |||
272 | /** |
||
273 | * Get API by type. |
||
274 | * |
||
275 | * @param string $type |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | 5 | public function getAPIByType($type) |
|
291 | } |
||
292 |