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 | 10 | public function __construct($config = null) |
|
49 | { |
||
50 | 10 | if (is_string($config)) { |
|
51 | 1 | $this->options(['base_uri' => $config]); |
|
52 | 10 | } elseif (is_array($config)) { |
|
53 | 1 | $this->options($config); |
|
54 | 1 | } |
|
55 | |||
56 | 10 | $this->client = new Client($this->options); |
|
57 | 10 | } |
|
58 | |||
59 | /** |
||
60 | * Get the Guzzle client instance. |
||
61 | * |
||
62 | * @return \GuzzleHttp\Client |
||
63 | */ |
||
64 | 2 | public function getClient() |
|
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 | 6 | 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 | 4 | public function removeOptions($key = null) |
|
112 | { |
||
113 | 4 | if (is_null($key)) { |
|
114 | 4 | $this->options = []; |
|
115 | 4 | } else { |
|
116 | 1 | Arr::forget($this->options, is_array($key) ? $key : func_get_args()); |
|
117 | } |
||
118 | |||
119 | 4 | return $this; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Set a request option using "dot" notation. |
||
124 | * |
||
125 | * @param string $key |
||
126 | * @param mixed $value |
||
127 | * @return $this |
||
128 | */ |
||
129 | 2 | public function option($key, $value) |
|
130 | { |
||
131 | 2 | if ($key) { |
|
132 | 2 | Arr::set($this->options, $key, $value); |
|
133 | 2 | } |
|
134 | |||
135 | 2 | return $this; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * Set the request header. |
||
140 | * |
||
141 | * @param string $name |
||
142 | * @param mixed $value |
||
143 | * @return $this |
||
144 | */ |
||
145 | public function header($name, $value) |
||
149 | |||
150 | /** |
||
151 | * Set the request content type. |
||
152 | * |
||
153 | * @param string $type |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function contentType($type) |
||
160 | |||
161 | /** |
||
162 | * Set the request accept type. |
||
163 | * |
||
164 | * @param string $type |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function accept($type) |
||
171 | |||
172 | /** |
||
173 | * Set the request accept type to JSON. |
||
174 | * |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function acceptJson() |
||
181 | |||
182 | /** |
||
183 | * Specify where the body of a response will be saved. |
||
184 | * Set the "sink" option. |
||
185 | * |
||
186 | * @param mixed $value |
||
187 | * @return $this |
||
188 | */ |
||
189 | public function saveTo($value) |
||
193 | |||
194 | /** |
||
195 | * Get the Guzzle response instance. |
||
196 | * |
||
197 | * @return \GuzzleHttp\Psr7\Response|null |
||
198 | */ |
||
199 | 1 | public function getResponse() |
|
203 | |||
204 | /** |
||
205 | * Get the status code of response. |
||
206 | * |
||
207 | * @return int |
||
208 | */ |
||
209 | public function getStatusCode() |
||
215 | |||
216 | /** |
||
217 | * Get the response header value. |
||
218 | * |
||
219 | * @param string $name |
||
220 | * @return mixed |
||
221 | */ |
||
222 | public function getHeader($name) |
||
228 | |||
229 | /** |
||
230 | * Get all response headers values. |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | public function getHeaders() |
||
238 | |||
239 | /** |
||
240 | * Get response body. |
||
241 | * |
||
242 | * @return \GuzzleHttp\Psr7\Stream|null |
||
243 | */ |
||
244 | public function getBody() |
||
250 | |||
251 | /** |
||
252 | * Get response content. |
||
253 | * |
||
254 | * @return string|null |
||
255 | */ |
||
256 | public function getContent() |
||
262 | |||
263 | /** |
||
264 | * Get JSON decoded response content. |
||
265 | * |
||
266 | * @param bool $assoc |
||
267 | * @return mixed |
||
268 | */ |
||
269 | public function getJson($assoc = true) |
||
275 | |||
276 | /** |
||
277 | * Make request to an URL. |
||
278 | * |
||
279 | * @param string $url |
||
280 | * @param string $method |
||
281 | * @param array $options |
||
282 | * @return $this |
||
283 | */ |
||
284 | 2 | public function request($url, $method = 'GET', $options = []) |
|
298 | |||
299 | /** |
||
300 | * Make request to an URL, expecting JSON content. |
||
301 | * |
||
302 | * @param string $url |
||
303 | * @param string $method |
||
304 | * @param array $options |
||
305 | * @return $this |
||
306 | */ |
||
307 | public function requestJson($url, $method = 'GET', $options = []) |
||
313 | |||
314 | /** |
||
315 | * Request the URL and return the response content. |
||
316 | * |
||
317 | * @param string $url |
||
318 | * @param string $method |
||
319 | * @param array $options |
||
320 | * @return string|null |
||
321 | */ |
||
322 | public function fetchContent($url, $method = 'GET', $options = []) |
||
326 | |||
327 | /** |
||
328 | * Request the URL and return the JSON decoded response content. |
||
329 | * |
||
330 | * @param string $url |
||
331 | * @param string $method |
||
332 | * @param array $options |
||
333 | * @param bool $assoc |
||
334 | * @return mixed |
||
335 | */ |
||
336 | public function fetchJson($url, $method = 'GET', $options = [], $assoc = true) |
||
340 | |||
341 | /** |
||
342 | * Any unhandled methods will be sent to $this->option() to set request option. |
||
343 | * |
||
344 | * @param string $name |
||
345 | * @param array $args |
||
346 | * @return $this |
||
347 | */ |
||
348 | 1 | public function __call($name, $args) |
|
352 | } |
||
353 |