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 | * Create a http client instance. |
||
38 | * |
||
39 | * @param array|string $config base_uri or any request options |
||
40 | */ |
||
41 | public function __construct($config = null) |
||
51 | |||
52 | /** |
||
53 | * Get the Guzzle client instance. |
||
54 | * |
||
55 | * @return \GuzzleHttp\Client |
||
56 | */ |
||
57 | public function getClient() |
||
61 | |||
62 | /** |
||
63 | * Get the request options. |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | public function getOptions() |
||
71 | |||
72 | /** |
||
73 | * Merge request options. |
||
74 | * |
||
75 | * @param array $options |
||
76 | * @return $this |
||
77 | */ |
||
78 | public function mergeOptions(array ...$options) |
||
84 | |||
85 | /** |
||
86 | * Set a request option using "dot" notation. |
||
87 | * |
||
88 | * @param string $key |
||
89 | * @param mixed $value |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function option($key, $value) |
||
100 | |||
101 | /** |
||
102 | * Set the request header. |
||
103 | * |
||
104 | * @param string $name |
||
105 | * @param mixed $value |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function header($name, $value) |
||
112 | |||
113 | /** |
||
114 | * Set the request content type. |
||
115 | * |
||
116 | * @param string $type |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function contentType($type) |
||
123 | |||
124 | /** |
||
125 | * Set the request accept type. |
||
126 | * |
||
127 | * @param string $type |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function accept($type) |
||
134 | |||
135 | /** |
||
136 | * Set the request accept type to JSON. |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function acceptJson() |
||
144 | |||
145 | /** |
||
146 | * Specify where the body of a response will be saved. |
||
147 | * Set the "sink" option. |
||
148 | * |
||
149 | * @param mixed $value |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function saveTo($value) |
||
156 | |||
157 | /** |
||
158 | * Get the Guzzle response instance. |
||
159 | * |
||
160 | * @return \GuzzleHttp\Psr7\Response|null |
||
161 | */ |
||
162 | public function getResponse() |
||
166 | |||
167 | /** |
||
168 | * Get the status code of response. |
||
169 | * |
||
170 | * @return int |
||
171 | */ |
||
172 | public function getStatusCode() |
||
178 | |||
179 | /** |
||
180 | * Get the response header value. |
||
181 | * |
||
182 | * @param string $name |
||
183 | * @return mixed |
||
184 | */ |
||
185 | public function getHeader($name) |
||
191 | |||
192 | /** |
||
193 | * Get all response headers values. |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | public function getHeaders() |
||
201 | |||
202 | /** |
||
203 | * Get response body. |
||
204 | * |
||
205 | * @return \GuzzleHttp\Psr7\Stream|null |
||
206 | */ |
||
207 | public function getBody() |
||
213 | |||
214 | /** |
||
215 | * Get response content. |
||
216 | * |
||
217 | * @return string|null |
||
218 | */ |
||
219 | public function getContent() |
||
225 | |||
226 | /** |
||
227 | * Get JSON decoded response content. |
||
228 | * |
||
229 | * @param bool $assoc |
||
230 | * @return mixed |
||
231 | */ |
||
232 | public function getJson($assoc = true) |
||
238 | |||
239 | /** |
||
240 | * Make request to an URL. |
||
241 | * |
||
242 | * @param string $url |
||
243 | * @param string $method |
||
244 | * @param array $options |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function request($url, $method = 'GET', $options = []) |
||
256 | |||
257 | /** |
||
258 | * Make request to an URL, expecting JSON content. |
||
259 | * |
||
260 | * @param string $url |
||
261 | * @param string $method |
||
262 | * @param array $options |
||
263 | * @return $this |
||
264 | */ |
||
265 | public function requestJson($url, $method = 'GET', $options = []) |
||
271 | |||
272 | /** |
||
273 | * Request the URL and return the response content. |
||
274 | * |
||
275 | * @param string $url |
||
276 | * @param string $method |
||
277 | * @param array $options |
||
278 | * @return string|null |
||
279 | */ |
||
280 | public function fetchContent($url, $method = 'GET', $options = []) |
||
284 | |||
285 | /** |
||
286 | * Request the URL and return the JSON decoded response content. |
||
287 | * |
||
288 | * @param string $url |
||
289 | * @param string $method |
||
290 | * @param array $options |
||
291 | * @param bool $assoc |
||
292 | * @return mixed |
||
293 | */ |
||
294 | public function fetchJson($url, $method = 'GET', $options = [], $assoc = true) |
||
298 | |||
299 | /** |
||
300 | * Any unhandled methods will be sent to $this->option() to set request option. |
||
301 | * |
||
302 | * @param string $name |
||
303 | * @param array $args |
||
304 | * @return $this |
||
305 | */ |
||
306 | public function __call($name, $args) |
||
310 | } |
||
311 |