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) |
|
79 | |||
80 | /** |
||
81 | * Get the request options. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | 6 | public function getOptions() |
|
89 | |||
90 | /** |
||
91 | * Merge request options. |
||
92 | * |
||
93 | * @param array $options |
||
94 | * @return $this |
||
95 | */ |
||
96 | 4 | public function options(array ...$options) |
|
102 | |||
103 | /** |
||
104 | * Remove one or many options using "dot" notation. |
||
105 | * |
||
106 | * @param string|array|null $key |
||
107 | * @return $this |
||
108 | */ |
||
109 | 4 | public function removeOptions($key = null) |
|
110 | { |
||
111 | 4 | if (is_null($key)) { |
|
112 | 4 | $this->options = []; |
|
113 | 4 | } else { |
|
114 | 1 | Arr::forget($this->options, is_array($key) ? $key : func_get_args()); |
|
115 | } |
||
116 | |||
117 | 4 | return $this; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Set a request option using "dot" notation. |
||
122 | * |
||
123 | * @param string $key |
||
124 | * @param mixed $value |
||
125 | * @return $this |
||
126 | */ |
||
127 | 2 | public function option($key, $value) |
|
128 | { |
||
129 | 2 | if ($key) { |
|
130 | 2 | Arr::set($this->options, $key, $value); |
|
131 | 2 | } |
|
132 | |||
133 | 2 | return $this; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Set the request header. |
||
138 | * |
||
139 | * @param string $name |
||
140 | * @param mixed $value |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function header($name, $value) |
||
147 | |||
148 | /** |
||
149 | * Set the request content type. |
||
150 | * |
||
151 | * @param string $type |
||
152 | * @return $this |
||
153 | */ |
||
154 | public function contentType($type) |
||
158 | |||
159 | /** |
||
160 | * Set the request accept type. |
||
161 | * |
||
162 | * @param string $type |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function accept($type) |
||
169 | |||
170 | /** |
||
171 | * Set the request accept type to JSON. |
||
172 | * |
||
173 | * @return $this |
||
174 | */ |
||
175 | public function acceptJson() |
||
179 | |||
180 | /** |
||
181 | * Specify where the body of a response will be saved. |
||
182 | * Set the "sink" option. |
||
183 | * |
||
184 | * @param mixed $value |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function saveTo($value) |
||
191 | |||
192 | /** |
||
193 | * Get the Guzzle response instance. |
||
194 | * |
||
195 | * @return \GuzzleHttp\Psr7\Response|null |
||
196 | */ |
||
197 | 1 | public function getResponse() |
|
201 | |||
202 | /** |
||
203 | * Get the status code of response. |
||
204 | * |
||
205 | * @return int |
||
206 | */ |
||
207 | public function getStatusCode() |
||
213 | |||
214 | /** |
||
215 | * Get the response header value. |
||
216 | * |
||
217 | * @param string $name |
||
218 | * @return mixed |
||
219 | */ |
||
220 | public function getHeader($name) |
||
226 | |||
227 | /** |
||
228 | * Get all response headers values. |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | public function getHeaders() |
||
236 | |||
237 | /** |
||
238 | * Get response body. |
||
239 | * |
||
240 | * @return \GuzzleHttp\Psr7\Stream|null |
||
241 | */ |
||
242 | public function getBody() |
||
248 | |||
249 | /** |
||
250 | * Get response content. |
||
251 | * |
||
252 | * @return string|null |
||
253 | */ |
||
254 | public function getContent() |
||
260 | |||
261 | /** |
||
262 | * Get JSON decoded response content. |
||
263 | * |
||
264 | * @param bool $assoc |
||
265 | * @return mixed |
||
266 | */ |
||
267 | public function getJson($assoc = true) |
||
273 | |||
274 | /** |
||
275 | * Make request to an URL. |
||
276 | * |
||
277 | * @param string $url |
||
278 | * @param string $method |
||
279 | * @param array $options |
||
280 | * @return $this |
||
281 | */ |
||
282 | 2 | public function request($url, $method = 'GET', $options = []) |
|
296 | |||
297 | /** |
||
298 | * Make request to an URL, expecting JSON content. |
||
299 | * |
||
300 | * @param string $url |
||
301 | * @param string $method |
||
302 | * @param array $options |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function requestJson($url, $method = 'GET', $options = []) |
||
311 | |||
312 | /** |
||
313 | * Request the URL and return the response content. |
||
314 | * |
||
315 | * @param string $url |
||
316 | * @param string $method |
||
317 | * @param array $options |
||
318 | * @return string|null |
||
319 | */ |
||
320 | public function fetchContent($url, $method = 'GET', $options = []) |
||
324 | |||
325 | /** |
||
326 | * Request the URL and return the JSON decoded response content. |
||
327 | * |
||
328 | * @param string $url |
||
329 | * @param string $method |
||
330 | * @param array $options |
||
331 | * @param bool $assoc |
||
332 | * @return mixed |
||
333 | */ |
||
334 | public function fetchJson($url, $method = 'GET', $options = [], $assoc = true) |
||
338 | |||
339 | /** |
||
340 | * Any unhandled methods will be sent to $this->option() to set request option. |
||
341 | * |
||
342 | * @param string $name |
||
343 | * @param array $args |
||
344 | * @return $this |
||
345 | */ |
||
346 | 1 | public function __call($name, $args) |
|
350 | } |
||
351 |