|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace YEntWeChat\Material; |
|
4
|
|
|
|
|
5
|
|
|
use YEntWeChat\Core\AbstractAPI; |
|
6
|
|
|
use YEntWeChat\Core\Exceptions\InvalidArgumentException; |
|
7
|
|
|
use YEntWeChat\Support\File; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Temporary. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Temporary extends AbstractAPI |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Allow media type. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $allowTypes = ['image', 'voice', 'video', 'file']; |
|
20
|
|
|
|
|
21
|
|
|
const API_GET = 'https://qyapi.weixin.qq.com/cgi-bin/media/get'; |
|
22
|
|
|
const API_UPLOAD = 'https://qyapi.weixin.qq.com/cgi-bin/media/upload'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Download temporary material. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $mediaId |
|
28
|
|
|
* @param string $directory |
|
29
|
|
|
* @param string $filename |
|
30
|
|
|
* |
|
31
|
|
|
* @throws InvalidArgumentException |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function download($mediaId, $directory, $filename = '') |
|
36
|
|
|
{ |
|
37
|
|
|
if (!is_dir($directory) || !is_writable($directory)) { |
|
38
|
|
|
throw new InvalidArgumentException("Directory does not exist or is not writable: '$directory'."); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$filename = $filename ?: $mediaId; |
|
42
|
|
|
|
|
43
|
|
|
$stream = $this->getStream($mediaId); |
|
44
|
|
|
|
|
45
|
|
|
$filename .= File::getStreamExt($stream); |
|
46
|
|
|
|
|
47
|
|
|
file_put_contents($directory.'/'.$filename, $stream); |
|
48
|
|
|
|
|
49
|
|
|
return $filename; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Fetch item from WeChat server. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $mediaId |
|
56
|
|
|
* |
|
57
|
|
|
* @throws \EntWeChat\Core\Exceptions\RuntimeException |
|
58
|
|
|
* |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getStream($mediaId) |
|
62
|
|
|
{ |
|
63
|
|
|
$response = $this->getHttp()->get(self::API_GET, ['media_id' => $mediaId]); |
|
64
|
|
|
|
|
65
|
|
|
return $response->getBody(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Upload temporary material. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $type |
|
72
|
|
|
* @param string $path |
|
73
|
|
|
* |
|
74
|
|
|
* @throws \EntWeChat\Core\Exceptions\InvalidArgumentException |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function upload($type, $path) |
|
79
|
|
|
{ |
|
80
|
|
|
if (!file_exists($path) || !is_readable($path)) { |
|
81
|
|
|
throw new InvalidArgumentException("File does not exist, or the file is unreadable: '$path'"); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (!in_array($type, $this->allowTypes, true)) { |
|
85
|
|
|
throw new InvalidArgumentException("Unsupported media type: '{$type}'"); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->parseJSON('upload', [self::API_UPLOAD, ['media' => $path], [], ['type' => $type]]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Upload image. |
|
93
|
|
|
* |
|
94
|
|
|
* @param $path |
|
95
|
|
|
* |
|
96
|
|
|
* @throws \EntWeChat\Core\Exceptions\InvalidArgumentException |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
public function uploadImage($path) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->upload('image', $path); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Upload video. |
|
107
|
|
|
* |
|
108
|
|
|
* @param $path |
|
109
|
|
|
* |
|
110
|
|
|
* @throws \EntWeChat\Core\Exceptions\InvalidArgumentException |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function uploadVideo($path) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->upload('video', $path); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Upload voice. |
|
121
|
|
|
* |
|
122
|
|
|
* @param $path |
|
123
|
|
|
* |
|
124
|
|
|
* @throws \EntWeChat\Core\Exceptions\InvalidArgumentException |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
public function uploadVoice($path) |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->upload('voice', $path); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Upload file. |
|
135
|
|
|
* |
|
136
|
|
|
* @param $path |
|
137
|
|
|
* |
|
138
|
|
|
* @throws \EntWeChat\Core\Exceptions\InvalidArgumentException |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
public function uploadFile($path) |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->upload('file', $path); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|