1 | <?php |
||
34 | class Http |
||
35 | { |
||
36 | /** |
||
37 | * Used to identify handler defined by client code |
||
38 | * Maybe useful in the future. |
||
39 | */ |
||
40 | const USER_DEFINED_HANDLER = 'userDefined'; |
||
41 | |||
42 | /** |
||
43 | * Http client. |
||
44 | * |
||
45 | * @var HttpClient |
||
46 | */ |
||
47 | protected $client; |
||
48 | |||
49 | /** |
||
50 | * The middlewares. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $middlewares = []; |
||
55 | |||
56 | /** |
||
57 | * Guzzle client default settings. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected static $defaults = [ |
||
62 | 'curl' => [ |
||
63 | CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, |
||
64 | ], |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * Set guzzle default settings. |
||
69 | * |
||
70 | * @param array $defaults |
||
71 | */ |
||
72 | 9 | public static function setDefaultOptions($defaults = []) |
|
76 | |||
77 | /** |
||
78 | * Return current guzzle default settings. |
||
79 | * |
||
80 | * @return array |
||
81 | */ |
||
82 | 2 | public static function getDefaultOptions() |
|
86 | |||
87 | /** |
||
88 | * GET request. |
||
89 | * |
||
90 | * @param string $url |
||
91 | * @param array $options |
||
92 | * |
||
93 | * @return ResponseInterface |
||
94 | * |
||
95 | * @throws HttpException |
||
96 | */ |
||
97 | 1 | public function get($url, array $options = []) |
|
101 | |||
102 | /** |
||
103 | * POST request. |
||
104 | * |
||
105 | * @param string $url |
||
106 | * @param array|string $options |
||
107 | * |
||
108 | * @return ResponseInterface |
||
109 | * |
||
110 | * @throws HttpException |
||
111 | */ |
||
112 | 1 | public function post($url, $options = []) |
|
118 | |||
119 | /** |
||
120 | * JSON request. |
||
121 | * |
||
122 | * @param string $url |
||
123 | * @param string|array $options |
||
124 | * @param array $queries |
||
125 | * @param int $encodeOption |
||
126 | * |
||
127 | * @return ResponseInterface |
||
128 | * |
||
129 | * @throws HttpException |
||
130 | */ |
||
131 | 1 | public function json($url, $options = [], $encodeOption = JSON_UNESCAPED_UNICODE, $queries = []) |
|
137 | |||
138 | /** |
||
139 | * Upload file. |
||
140 | * |
||
141 | * @param string $url |
||
142 | * @param array $files |
||
143 | * @param array $form |
||
144 | * |
||
145 | * @return ResponseInterface |
||
146 | * |
||
147 | * @throws HttpException |
||
148 | */ |
||
149 | 1 | public function upload($url, array $files = [], array $form = [], array $queries = []) |
|
150 | { |
||
151 | 1 | $multipart = []; |
|
152 | |||
153 | 1 | foreach ($files as $name => $path) { |
|
154 | 1 | $multipart[] = [ |
|
155 | 1 | 'name' => $name, |
|
156 | 1 | 'contents' => fopen($path, 'r'), |
|
157 | ]; |
||
158 | 1 | } |
|
159 | |||
160 | 1 | foreach ($form as $name => $contents) { |
|
161 | 1 | $multipart[] = compact('name', 'contents'); |
|
162 | 1 | } |
|
163 | |||
164 | 1 | return $this->request($url, 'POST', ['query' => $queries, 'multipart' => $multipart]); |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * Set GuzzleHttp\Client. |
||
169 | * |
||
170 | * @param \GuzzleHttp\Client $client |
||
171 | * |
||
172 | * @return Http |
||
173 | */ |
||
174 | 7 | public function setClient(HttpClient $client) |
|
180 | |||
181 | /** |
||
182 | * Return GuzzleHttp\Client instance. |
||
183 | * |
||
184 | * @return \GuzzleHttp\Client |
||
185 | */ |
||
186 | 4 | public function getClient() |
|
187 | { |
||
188 | 4 | if (!($this->client instanceof HttpClient)) { |
|
189 | 1 | $this->client = new HttpClient(); |
|
190 | 1 | } |
|
191 | |||
192 | 4 | return $this->client; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Add a middleware. |
||
197 | * |
||
198 | * @param callable $middleware |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | 8 | public function addMiddleware(callable $middleware) |
|
208 | |||
209 | /** |
||
210 | * Return all middlewares. |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | 8 | public function getMiddlewares() |
|
218 | |||
219 | /** |
||
220 | * Make a request. |
||
221 | * |
||
222 | * @param string $url |
||
223 | * @param string $method |
||
224 | * @param array $options |
||
225 | * |
||
226 | * @return ResponseInterface |
||
227 | * |
||
228 | * @throws HttpException |
||
229 | */ |
||
230 | 3 | public function request($url, $method = 'GET', $options = []) |
|
231 | { |
||
232 | 3 | $method = strtoupper($method); |
|
233 | |||
234 | 3 | $options = array_merge(self::$defaults, $options); |
|
235 | |||
236 | 3 | Log::debug('Client Request:', compact('url', 'method', 'options')); |
|
237 | |||
238 | 3 | $options['handler'] = $this->getHandler(); |
|
239 | |||
240 | 3 | $response = $this->getClient()->request($method, $url, $options); |
|
241 | |||
242 | 3 | Log::debug('API response:', [ |
|
243 | 3 | 'Status' => $response->getStatusCode(), |
|
244 | 3 | 'Reason' => $response->getReasonPhrase(), |
|
245 | 3 | 'Headers' => $response->getHeaders(), |
|
246 | 3 | 'Body' => strval($response->getBody()), |
|
247 | 3 | ]); |
|
248 | |||
249 | 3 | return $response; |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * @param \Psr\Http\Message\ResponseInterface|string $body |
||
254 | * |
||
255 | * @return mixed |
||
256 | * |
||
257 | * @throws \EasyWeChat\Core\Exceptions\HttpException |
||
258 | */ |
||
259 | 10 | public function parseJSON($body) |
|
260 | { |
||
261 | 10 | if ($body instanceof ResponseInterface) { |
|
262 | 2 | $body = $body->getBody(); |
|
263 | 2 | } |
|
264 | |||
265 | // XXX: json maybe contains special chars. So, let's FUCK the WeChat API developers ... |
||
266 | 10 | $body = $this->fuckTheWeChatInvalidJSON($body); |
|
267 | |||
268 | 10 | if (empty($body)) { |
|
269 | 1 | return false; |
|
270 | } |
||
271 | |||
272 | 10 | $contents = json_decode($body, true); |
|
273 | |||
274 | 10 | Log::debug('API response decoded:', compact('contents')); |
|
275 | |||
276 | 10 | if (JSON_ERROR_NONE !== json_last_error()) { |
|
277 | 1 | throw new HttpException('Failed to parse JSON: '.json_last_error_msg()); |
|
278 | } |
||
279 | |||
280 | 10 | return $contents; |
|
281 | } |
||
282 | |||
283 | /** |
||
284 | * Filter the invalid JSON string. |
||
285 | * |
||
286 | * @param \Psr\Http\Message\StreamInterface|string $invalidJSON |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | 10 | protected function fuckTheWeChatInvalidJSON($invalidJSON) |
|
294 | |||
295 | /** |
||
296 | * Build a handler. |
||
297 | * |
||
298 | * @return HandlerStack |
||
299 | */ |
||
300 | 3 | protected function getHandler() |
|
314 | } |
||
315 |