1 | <?php |
||
10 | class HttpClient |
||
11 | { |
||
12 | /** |
||
13 | * The Guzzle client. |
||
14 | * |
||
15 | * @var \GuzzleHttp\Client |
||
16 | */ |
||
17 | protected $client; |
||
18 | |||
19 | /** |
||
20 | * The Guzzle response. |
||
21 | * |
||
22 | * @var \GuzzleHttp\Psr7\Response |
||
23 | */ |
||
24 | protected $response; |
||
25 | |||
26 | /** |
||
27 | * The request options. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $options = [ |
||
32 | 'connect_timeout' => 5, |
||
33 | 'timeout' => 25, |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * Indicates whether throws Guzzle exceptions. |
||
38 | * |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $withExceptions = false; |
||
42 | |||
43 | /** |
||
44 | * Create a http client instance. |
||
45 | * |
||
46 | * @param array|string $config base_uri or any request options |
||
47 | */ |
||
48 | 16 | public function __construct($config = null) |
|
58 | |||
59 | /** |
||
60 | * Get the Guzzle client instance. |
||
61 | * |
||
62 | * @return \GuzzleHttp\Client |
||
63 | */ |
||
64 | 2 | public function getClient() |
|
65 | 1 | { |
|
66 | 2 | return $this->client; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Trun on/off Guzzle exceptions. |
||
71 | * |
||
72 | * @param bool $throws |
||
73 | * @return $this |
||
74 | */ |
||
75 | 1 | public function withExceptions($throws) |
|
81 | |||
82 | /** |
||
83 | * Get the request options. |
||
84 | * |
||
85 | * @return array |
||
86 | */ |
||
87 | 9 | public function getOptions() |
|
91 | |||
92 | /** |
||
93 | * Merge request options. |
||
94 | * |
||
95 | * @param array $options |
||
96 | * @return $this |
||
97 | */ |
||
98 | 4 | public function options(array ...$options) |
|
104 | |||
105 | /** |
||
106 | * Remove one or many options using "dot" notation. |
||
107 | * |
||
108 | * @param string|array|null $key |
||
109 | * @return $this |
||
110 | */ |
||
111 | 9 | public function removeOptions($key = null) |
|
121 | |||
122 | /** |
||
123 | * Set a request option using "dot" notation. |
||
124 | * |
||
125 | * @param string $key |
||
126 | * @param mixed $value |
||
127 | * @return $this |
||
128 | */ |
||
129 | 7 | public function option($key, $value) |
|
137 | |||
138 | /** |
||
139 | * Get a request option using "dot" notation. |
||
140 | * |
||
141 | * @param string $key |
||
142 | * @return mixed |
||
143 | */ |
||
144 | 3 | public function getOption($key) |
|
148 | |||
149 | /** |
||
150 | * Set the request header. |
||
151 | * |
||
152 | * @param string $name |
||
153 | * @param mixed $value |
||
154 | * @return $this |
||
155 | */ |
||
156 | 4 | public function header($name, $value) |
|
160 | |||
161 | /** |
||
162 | * Set the request content type. |
||
163 | * |
||
164 | * @param string $type |
||
165 | * @return $this |
||
166 | */ |
||
167 | 1 | public function contentType($type) |
|
171 | |||
172 | /** |
||
173 | * Set the request accept type. |
||
174 | * |
||
175 | * @param string $type |
||
176 | * @return $this |
||
177 | */ |
||
178 | 2 | public function accept($type) |
|
182 | |||
183 | /** |
||
184 | * Set the request accept type to JSON. |
||
185 | * |
||
186 | * @return $this |
||
187 | */ |
||
188 | 1 | public function acceptJson() |
|
192 | |||
193 | /** |
||
194 | * Specify where the body of a response will be saved. |
||
195 | * Set the "sink" option. |
||
196 | * |
||
197 | * @param mixed $value |
||
198 | * @return $this |
||
199 | */ |
||
200 | 1 | public function saveTo($value) |
|
204 | |||
205 | /** |
||
206 | * Get the Guzzle response instance. |
||
207 | * |
||
208 | * @return \GuzzleHttp\Psr7\Response|null |
||
209 | */ |
||
210 | 1 | public function getResponse() |
|
214 | |||
215 | /** |
||
216 | * Get the status code of response. |
||
217 | * |
||
218 | * @return int |
||
219 | */ |
||
220 | public function getStatusCode() |
||
226 | |||
227 | /** |
||
228 | * Get the response header value. |
||
229 | * |
||
230 | * @param string $name |
||
231 | * @return mixed |
||
232 | */ |
||
233 | public function getHeader($name) |
||
239 | |||
240 | /** |
||
241 | * Get all response headers values. |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | public function getHeaders() |
||
249 | |||
250 | /** |
||
251 | * Get response body. |
||
252 | * |
||
253 | * @return \GuzzleHttp\Psr7\Stream|null |
||
254 | */ |
||
255 | public function getBody() |
||
261 | |||
262 | /** |
||
263 | * Get response content. |
||
264 | * |
||
265 | * @return string|null |
||
266 | */ |
||
267 | public function getContent() |
||
273 | |||
274 | /** |
||
275 | * Get JSON decoded response content. |
||
276 | * |
||
277 | * @param bool $assoc |
||
278 | * @return mixed |
||
279 | */ |
||
280 | public function getJson($assoc = true) |
||
286 | |||
287 | /** |
||
288 | * Make request to an URL. |
||
289 | * |
||
290 | * @param string $url |
||
291 | * @param string $method |
||
292 | * @param array $options |
||
293 | * @return $this |
||
294 | */ |
||
295 | 2 | public function request($url, $method = 'GET', $options = []) |
|
309 | |||
310 | /** |
||
311 | * Make request to an URL, expecting JSON content. |
||
312 | * |
||
313 | * @param string $url |
||
314 | * @param string $method |
||
315 | * @param array $options |
||
316 | * @return $this |
||
317 | */ |
||
318 | public function requestJson($url, $method = 'GET', $options = []) |
||
324 | |||
325 | /** |
||
326 | * Request the URL and return the response content. |
||
327 | * |
||
328 | * @param string $url |
||
329 | * @param string $method |
||
330 | * @param array $options |
||
331 | * @return string|null |
||
332 | */ |
||
333 | public function fetchContent($url, $method = 'GET', $options = []) |
||
337 | |||
338 | /** |
||
339 | * Request the URL and return the JSON decoded response content. |
||
340 | * |
||
341 | * @param string $url |
||
342 | * @param string $method |
||
343 | * @param array $options |
||
344 | * @param bool $assoc |
||
345 | * @return mixed |
||
346 | */ |
||
347 | public function fetchJson($url, $method = 'GET', $options = [], $assoc = true) |
||
351 | |||
352 | /** |
||
353 | * Any unhandled methods will be sent to $this->option() to set request option. |
||
354 | * |
||
355 | * @param string $name |
||
356 | * @param array $args |
||
357 | * @return $this |
||
358 | */ |
||
359 | 1 | public function __call($name, $args) |
|
363 | } |
||
364 |