1 | <?php |
||
14 | class Http |
||
15 | { |
||
16 | /** |
||
17 | * Used to identify handler defined by client code |
||
18 | * Maybe useful in the future. |
||
19 | */ |
||
20 | const USER_DEFINED_HANDLER = 'userDefined'; |
||
21 | |||
22 | /** |
||
23 | * Http client. |
||
24 | * |
||
25 | * @var HttpClient |
||
26 | */ |
||
27 | protected $client; |
||
28 | |||
29 | /** |
||
30 | * The middlewares. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $middlewares = []; |
||
35 | |||
36 | /** |
||
37 | * Guzzle client default settings. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected static $defaults = [ |
||
42 | 'curl' => [ |
||
43 | CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, |
||
44 | ], |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Set guzzle default settings. |
||
49 | * |
||
50 | * @param array $defaults |
||
51 | */ |
||
52 | public static function setDefaultOptions($defaults = []) |
||
56 | |||
57 | /** |
||
58 | * Return current guzzle default settings. |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | public static function getDefaultOptions() |
||
66 | |||
67 | /** |
||
68 | * GET request. |
||
69 | * |
||
70 | * @param string $url |
||
71 | * @param array $options |
||
72 | * |
||
73 | * @throws HttpException |
||
74 | * |
||
75 | * @return ResponseInterface |
||
76 | */ |
||
77 | public function get($url, array $options = []) |
||
81 | |||
82 | /** |
||
83 | * POST request. |
||
84 | * |
||
85 | * @param string $url |
||
86 | * @param array|string $options |
||
87 | * |
||
88 | * @throws HttpException |
||
89 | * |
||
90 | * @return ResponseInterface |
||
91 | */ |
||
92 | public function post($url, $options = []) |
||
98 | |||
99 | /** |
||
100 | * JSON request. |
||
101 | * |
||
102 | * @param string $url |
||
103 | * @param string|array $options |
||
104 | * @param array $queries |
||
105 | * @param int $encodeOption |
||
106 | * |
||
107 | * @throws HttpException |
||
108 | * |
||
109 | * @return ResponseInterface |
||
110 | */ |
||
111 | public function json($url, $options = [], $encodeOption = JSON_UNESCAPED_UNICODE, $queries = []) |
||
117 | |||
118 | /** |
||
119 | * Upload file. |
||
120 | * |
||
121 | * @param string $url |
||
122 | * @param array $files |
||
123 | * @param array $form |
||
124 | * |
||
125 | * @throws HttpException |
||
126 | * |
||
127 | * @return ResponseInterface |
||
128 | */ |
||
129 | public function upload($url, array $files = [], array $form = [], array $queries = []) |
||
146 | |||
147 | /** |
||
148 | * Set GuzzleHttp\Client. |
||
149 | * |
||
150 | * @param \GuzzleHttp\Client $client |
||
151 | * |
||
152 | * @return Http |
||
153 | */ |
||
154 | public function setClient(HttpClient $client) |
||
160 | |||
161 | /** |
||
162 | * Return GuzzleHttp\Client instance. |
||
163 | * |
||
164 | * @return \GuzzleHttp\Client |
||
165 | */ |
||
166 | public function getClient() |
||
174 | |||
175 | /** |
||
176 | * Add a middleware. |
||
177 | * |
||
178 | * @param callable $middleware |
||
179 | * |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function addMiddleware(callable $middleware) |
||
188 | |||
189 | /** |
||
190 | * Return all middlewares. |
||
191 | * |
||
192 | * @return array |
||
193 | */ |
||
194 | public function getMiddlewares() |
||
198 | |||
199 | /** |
||
200 | * Make a request. |
||
201 | * |
||
202 | * @param string $url |
||
203 | * @param string $method |
||
204 | * @param array $options |
||
205 | * |
||
206 | * @throws HttpException |
||
207 | * |
||
208 | * @return ResponseInterface |
||
209 | */ |
||
210 | public function request($url, $method = 'GET', $options = []) |
||
231 | |||
232 | /** |
||
233 | * @param \Psr\Http\Message\ResponseInterface|string $body |
||
234 | * |
||
235 | * @throws \EntWeChat\Core\Exceptions\HttpException |
||
236 | * |
||
237 | * @return mixed |
||
238 | */ |
||
239 | public function parseJSON($body) |
||
262 | |||
263 | /** |
||
264 | * Filter the invalid JSON string. |
||
265 | * |
||
266 | * @param \Psr\Http\Message\StreamInterface|string $invalidJSON |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | protected function fuckTheWeChatInvalidJSON($invalidJSON) |
||
274 | |||
275 | /** |
||
276 | * Build a handler. |
||
277 | * |
||
278 | * @return HandlerStack |
||
279 | */ |
||
280 | protected function getHandler() |
||
294 | } |
||
295 |