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 catching 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 | public function __construct($config = null) |
||
58 | |||
59 | /** |
||
60 | * Get the Guzzle client instance. |
||
61 | * |
||
62 | * @return \GuzzleHttp\Client |
||
63 | */ |
||
64 | public function getClient() |
||
68 | |||
69 | /** |
||
70 | * Trun on/off Guzzle exceptions. |
||
71 | * |
||
72 | * @param bool $throws |
||
73 | * @return $this |
||
74 | */ |
||
75 | public function withExceptions($throws) |
||
79 | |||
80 | /** |
||
81 | * Get the request options. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | public function getOptions() |
||
89 | |||
90 | /** |
||
91 | * Merge request options. |
||
92 | * |
||
93 | * @param array $options |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function mergeOptions(array ...$options) |
||
102 | |||
103 | /** |
||
104 | * Set a request option using "dot" notation. |
||
105 | * |
||
106 | * @param string $key |
||
107 | * @param mixed $value |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function option($key, $value) |
||
118 | |||
119 | /** |
||
120 | * Set the request header. |
||
121 | * |
||
122 | * @param string $name |
||
123 | * @param mixed $value |
||
124 | * @return $this |
||
125 | */ |
||
126 | public function header($name, $value) |
||
130 | |||
131 | /** |
||
132 | * Set the request content type. |
||
133 | * |
||
134 | * @param string $type |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function contentType($type) |
||
141 | |||
142 | /** |
||
143 | * Set the request accept type. |
||
144 | * |
||
145 | * @param string $type |
||
146 | * @return $this |
||
147 | */ |
||
148 | public function accept($type) |
||
152 | |||
153 | /** |
||
154 | * Set the request accept type to JSON. |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function acceptJson() |
||
162 | |||
163 | /** |
||
164 | * Specify where the body of a response will be saved. |
||
165 | * Set the "sink" option. |
||
166 | * |
||
167 | * @param mixed $value |
||
168 | * @return $this |
||
169 | */ |
||
170 | public function saveTo($value) |
||
174 | |||
175 | /** |
||
176 | * Get the Guzzle response instance. |
||
177 | * |
||
178 | * @return \GuzzleHttp\Psr7\Response|null |
||
179 | */ |
||
180 | public function getResponse() |
||
184 | |||
185 | /** |
||
186 | * Get the status code of response. |
||
187 | * |
||
188 | * @return int |
||
189 | */ |
||
190 | public function getStatusCode() |
||
196 | |||
197 | /** |
||
198 | * Get the response header value. |
||
199 | * |
||
200 | * @param string $name |
||
201 | * @return mixed |
||
202 | */ |
||
203 | public function getHeader($name) |
||
209 | |||
210 | /** |
||
211 | * Get all response headers values. |
||
212 | * |
||
213 | * @return array |
||
214 | */ |
||
215 | public function getHeaders() |
||
219 | |||
220 | /** |
||
221 | * Get response body. |
||
222 | * |
||
223 | * @return \GuzzleHttp\Psr7\Stream|null |
||
224 | */ |
||
225 | public function getBody() |
||
231 | |||
232 | /** |
||
233 | * Get response content. |
||
234 | * |
||
235 | * @return string|null |
||
236 | */ |
||
237 | public function getContent() |
||
243 | |||
244 | /** |
||
245 | * Get JSON decoded response content. |
||
246 | * |
||
247 | * @param bool $assoc |
||
248 | * @return mixed |
||
249 | */ |
||
250 | public function getJson($assoc = true) |
||
256 | |||
257 | /** |
||
258 | * Make request to an URL. |
||
259 | * |
||
260 | * @param string $url |
||
261 | * @param string $method |
||
262 | * @param array $options |
||
263 | * @return $this |
||
264 | */ |
||
265 | public function request($url, $method = 'GET', $options = []) |
||
277 | |||
278 | /** |
||
279 | * Make request to an URL, expecting JSON content. |
||
280 | * |
||
281 | * @param string $url |
||
282 | * @param string $method |
||
283 | * @param array $options |
||
284 | * @return $this |
||
285 | */ |
||
286 | public function requestJson($url, $method = 'GET', $options = []) |
||
292 | |||
293 | /** |
||
294 | * Request the URL and return the response content. |
||
295 | * |
||
296 | * @param string $url |
||
297 | * @param string $method |
||
298 | * @param array $options |
||
299 | * @return string|null |
||
300 | */ |
||
301 | public function fetchContent($url, $method = 'GET', $options = []) |
||
305 | |||
306 | /** |
||
307 | * Request the URL and return the JSON decoded response content. |
||
308 | * |
||
309 | * @param string $url |
||
310 | * @param string $method |
||
311 | * @param array $options |
||
312 | * @param bool $assoc |
||
313 | * @return mixed |
||
314 | */ |
||
315 | public function fetchJson($url, $method = 'GET', $options = [], $assoc = true) |
||
319 | |||
320 | /** |
||
321 | * Any unhandled methods will be sent to $this->option() to set request option. |
||
322 | * |
||
323 | * @param string $name |
||
324 | * @param array $args |
||
325 | * @return $this |
||
326 | */ |
||
327 | public function __call($name, $args) |
||
331 | } |
||
332 |