123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace WeChat;
- use WeChat\Contracts\BasicWeChat;
- use WeChat\Contracts\Tools;
- use WeChat\Exceptions\InvalidResponseException;
- class Media extends BasicWeChat
- {
-
- public function add($filename, $type = 'image')
- {
- if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
- throw new InvalidResponseException('Invalid Media Type.', '0');
- }
- $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type={$type}";
- $this->registerApi($url, __FUNCTION__, func_get_args());
- return $this->httpPostForJson($url, ['media' => Tools::createCurlFile($filename)], false);
- }
-
- public function get($media_id, $outType = null)
- {
- $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id={$media_id}";
- $this->registerApi($url, __FUNCTION__, func_get_args());
- $result = Tools::get($url);
- if (is_array($json = json_decode($result, true))) {
- if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
- [$this->delAccessToken(), $this->isTry = true];
- return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
- }
- return Tools::json2arr($result);
- }
- return is_null($outType) ? $result : $outType($result);
- }
-
- public function addNews($data)
- {
- $url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN";
- $this->registerApi($url, __FUNCTION__, func_get_args());
- return $this->httpPostForJson($url, $data);
- }
-
- public function updateNews($media_id, $index, $news)
- {
- $data = ['media_id' => $media_id, 'index' => $index, 'articles' => $news];
- $url = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=ACCESS_TOKEN";
- $this->registerApi($url, __FUNCTION__, func_get_args());
- return $this->httpPostForJson($url, $data);
- }
-
- public function uploadImg($filename)
- {
- $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN";
- $this->registerApi($url, __FUNCTION__, func_get_args());
- return $this->httpPostForJson($url, ['media' => Tools::createCurlFile($filename)], false);
- }
-
- public function addMaterial($filename, $type = 'image', $description = [])
- {
- if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
- throw new InvalidResponseException('Invalid Media Type.', '0');
- }
- $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type={$type}";
- $this->registerApi($url, __FUNCTION__, func_get_args());
- return $this->httpPostForJson($url, ['media' => Tools::createCurlFile($filename), 'description' => Tools::arr2json($description)], false);
- }
-
- public function getMaterial($media_id, $outType = null)
- {
- $url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN";
- $this->registerApi($url, __FUNCTION__, func_get_args());
- $result = Tools::post($url, ['media_id' => $media_id]);
- if (is_array($json = json_decode($result, true))) {
- if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
- [$this->delAccessToken(), $this->isTry = true];
- return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
- }
- return Tools::json2arr($result);
- }
- return is_null($outType) ? $result : $outType($result);
- }
-
- public function delMaterial($media_id)
- {
- $url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=ACCESS_TOKEN";
- $this->registerApi($url, __FUNCTION__, func_get_args());
- return $this->httpPostForJson($url, ['media_id' => $media_id]);
- }
-
- public function getMaterialCount()
- {
- $url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=ACCESS_TOKEN";
- $this->registerApi($url, __FUNCTION__, func_get_args());
- return $this->httpGetForJson($url);
- }
-
- public function batchGetMaterial($type = 'image', $offset = 0, $count = 20)
- {
- if (!in_array($type, ['image', 'voice', 'video', 'news'])) {
- throw new InvalidResponseException('Invalid Media Type.', '0');
- }
- $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN";
- $this->registerApi($url, __FUNCTION__, func_get_args());
- return $this->httpPostForJson($url, ['type' => $type, 'offset' => $offset, 'count' => $count]);
- }
- }
|