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) |
|
94 | { |
||
95 | $params = [ |
||
96 | 1 | 'description' => json_encode( |
|
97 | [ |
||
98 | 1 | 'title' => $title, |
|
99 | 1 | 'introduction' => $description, |
|
100 | ] |
||
101 | 1 | , JSON_UNESCAPED_UNICODE), |
|
102 | 1 | ]; |
|
103 | |||
104 | 1 | return $this->uploadMedia('video', $path, $params); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Upload articles. |
||
109 | * |
||
110 | * @param array|Article $articles |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 1 | public function uploadArticle($articles) |
|
115 | { |
||
116 | 1 | if (!empty($articles['title']) || $articles instanceof Article) { |
|
117 | 1 | $articles = [$articles]; |
|
118 | 1 | } |
|
119 | |||
120 | 1 | $params = ['articles' => array_map(function ($article) { |
|
121 | 1 | if ($article instanceof Article) { |
|
122 | 1 | return $article->only([ |
|
123 | 1 | 'title', 'thumb_media_id', 'author', 'digest', |
|
124 | 1 | 'show_cover_pic', 'content', 'content_source_url', |
|
125 | 1 | ]); |
|
126 | } |
||
127 | |||
128 | 1 | return $article; |
|
129 | 1 | }, $articles)]; |
|
130 | |||
131 | 1 | return $this->parseJSON('json', [self::API_NEWS_UPLOAD, $params]); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Update article. |
||
136 | * |
||
137 | * @param string $mediaId |
||
138 | * @param array $article |
||
139 | * @param int $index |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | 1 | public function updateArticle($mediaId, $article, $index = 0) |
|
153 | |||
154 | /** |
||
155 | * Upload image for article. |
||
156 | * |
||
157 | * @param string $path |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | 1 | public function uploadArticleImage($path) |
|
165 | |||
166 | /** |
||
167 | * Fetch material. |
||
168 | * |
||
169 | * @param string $mediaId |
||
170 | * |
||
171 | * @return mixed |
||
172 | */ |
||
173 | 1 | public function get($mediaId) |
|
174 | { |
||
175 | 1 | $response = $this->getHttp()->json(self::API_GET, ['media_id' => $mediaId]); |
|
176 | |||
177 | 1 | foreach ($response->getHeader('Content-Type') as $mime) { |
|
178 | 1 | if (preg_match('/(image|video|audio)/i', $mime)) { |
|
179 | 1 | return $response->getBody(); |
|
180 | } |
||
181 | 1 | } |
|
182 | |||
183 | 1 | $json = $this->getHttp()->parseJSON($response); |
|
184 | |||
185 | // XXX: 微信开发这帮混蛋,尼玛文件二进制输出不带header,简直日了!!! |
||
186 | 1 | if (!$json) { |
|
187 | return $response->getBody(); |
||
188 | } |
||
189 | |||
190 | 1 | $this->checkAndThrow($json); |
|
191 | |||
192 | 1 | return $json; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Delete material by media ID. |
||
197 | * |
||
198 | * @param string $mediaId |
||
199 | * |
||
200 | * @return bool |
||
201 | */ |
||
202 | 1 | public function delete($mediaId) |
|
206 | |||
207 | /** |
||
208 | * List materials. |
||
209 | * |
||
210 | * example: |
||
211 | * |
||
212 | * { |
||
213 | * "total_count": TOTAL_COUNT, |
||
214 | * "item_count": ITEM_COUNT, |
||
215 | * "item": [{ |
||
216 | * "media_id": MEDIA_ID, |
||
217 | * "name": NAME, |
||
218 | * "update_time": UPDATE_TIME |
||
219 | * }, |
||
220 | * // more... |
||
221 | * ] |
||
222 | * } |
||
223 | * |
||
224 | * @param string $type |
||
225 | * @param int $offset |
||
226 | * @param int $count |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | 1 | public function lists($type, $offset = 0, $count = 20) |
|
240 | |||
241 | /** |
||
242 | * Get stats of materials. |
||
243 | * |
||
244 | * @return array |
||
245 | */ |
||
246 | 1 | public function stats() |
|
250 | |||
251 | /** |
||
252 | * Upload material. |
||
253 | * |
||
254 | * @param string $type |
||
255 | * @param string $path |
||
256 | * @param array $form |
||
257 | * |
||
258 | * @return string |
||
259 | * |
||
260 | * @throws InvalidArgumentException |
||
261 | */ |
||
262 | 5 | protected function uploadMedia($type, $path, array $form = []) |
|
272 | |||
273 | /** |
||
274 | * Get API by type. |
||
275 | * |
||
276 | * @param string $type |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | 5 | public function getAPIByType($type) |
|
292 | } |
||
293 |