|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/wechat. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace EasyWeChat\Work\Media; |
|
13
|
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\BaseClient; |
|
15
|
|
|
use EasyWeChat\Kernel\Http\StreamResponse; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Client. |
|
19
|
|
|
* |
|
20
|
|
|
* @author mingyoung <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Client extends BaseClient |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Get media. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $mediaId |
|
28
|
|
|
* |
|
29
|
|
|
* @return array|\EasyWeChat\Kernel\Http\Response|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
|
30
|
|
|
* |
|
31
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
32
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function get(string $mediaId) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$response = $this->requestRaw('cgi-bin/media/get', 'GET', [ |
|
37
|
|
|
'query' => [ |
|
38
|
1 |
|
'media_id' => $mediaId, |
|
39
|
|
|
], |
|
40
|
|
|
]); |
|
41
|
|
|
|
|
42
|
1 |
|
if (false !== stripos($response->getHeaderLine('Content-Type'), 'text/plain')) { |
|
43
|
1 |
|
return $this->castResponseToType($response, $this->app['config']->get('response_type')); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
return StreamResponse::buildFromPsrResponse($response); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Upload Image. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $path |
|
53
|
|
|
* |
|
54
|
|
|
* @return mixed |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function uploadImage(string $path) |
|
57
|
|
|
{ |
|
58
|
1 |
|
return $this->upload('image', $path); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Upload Voice. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $path |
|
65
|
|
|
* |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function uploadVoice(string $path) |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->upload('voice', $path); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Upload Video. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $path |
|
77
|
|
|
* |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function uploadVideo(string $path) |
|
81
|
|
|
{ |
|
82
|
1 |
|
return $this->upload('video', $path); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Upload File. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $path |
|
89
|
|
|
* |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function uploadFile(string $path) |
|
93
|
|
|
{ |
|
94
|
1 |
|
return $this->upload('file', $path); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Upload media. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $type |
|
101
|
|
|
* @param string $path |
|
102
|
|
|
* |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
* |
|
105
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
106
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function upload(string $type, string $path) |
|
109
|
|
|
{ |
|
110
|
|
|
$files = [ |
|
111
|
1 |
|
'media' => $path, |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
1 |
|
return $this->httpUpload('cgi-bin/media/upload', $files, [], compact('type')); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|