1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Helper; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
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) |
49
|
|
|
{ |
50
|
|
|
if (is_string($config)) { |
51
|
|
|
$this->mergeOptions(['base_uri' => $config]); |
52
|
|
|
} else if (is_array($config)) { |
53
|
|
|
$this->mergeOptions($config); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->client = new Client($this->options); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the Guzzle client instance. |
61
|
|
|
* |
62
|
|
|
* @return \GuzzleHttp\Client |
63
|
|
|
*/ |
64
|
|
|
public function getClient() |
65
|
|
|
{ |
66
|
|
|
return $this->client; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Trun on/off Guzzle exceptions. |
71
|
|
|
* |
72
|
|
|
* @param bool $throws |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function withExceptions($throws) |
76
|
|
|
{ |
77
|
|
|
$this->withExceptions = !!$throws; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the request options. |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function getOptions() |
86
|
|
|
{ |
87
|
|
|
return $this->options; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Merge request options. |
92
|
|
|
* |
93
|
|
|
* @param array $options |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function mergeOptions(array ...$options) |
97
|
|
|
{ |
98
|
|
|
$this->options = array_merge_recursive($this->options, ...$options); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
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) |
111
|
|
|
{ |
112
|
|
|
if ($key) { |
113
|
|
|
Arr::set($this->options, $key, $value); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
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) |
127
|
|
|
{ |
128
|
|
|
return $this->option('headers.'.$name, $value); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the request content type. |
133
|
|
|
* |
134
|
|
|
* @param string $type |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function contentType($type) |
138
|
|
|
{ |
139
|
|
|
return $this->header('Content-Type', $type); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set the request accept type. |
144
|
|
|
* |
145
|
|
|
* @param string $type |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function accept($type) |
149
|
|
|
{ |
150
|
|
|
return $this->header('Accept', $type); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set the request accept type to JSON. |
155
|
|
|
* |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
|
|
public function acceptJson() |
159
|
|
|
{ |
160
|
|
|
return $this->accept('application/json'); |
161
|
|
|
} |
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) |
171
|
|
|
{ |
172
|
|
|
return $this->option('sink', $value); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get the Guzzle response instance. |
177
|
|
|
* |
178
|
|
|
* @return \GuzzleHttp\Psr7\Response|null |
179
|
|
|
*/ |
180
|
|
|
public function getResponse() |
181
|
|
|
{ |
182
|
|
|
return $this->response; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get the status code of response. |
187
|
|
|
* |
188
|
|
|
* @return int |
189
|
|
|
*/ |
190
|
|
|
public function getStatusCode() |
191
|
|
|
{ |
192
|
|
|
if ($this->response) { |
193
|
|
|
return $this->response->getStatusCode(); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the response header value. |
199
|
|
|
* |
200
|
|
|
* @param string $name |
201
|
|
|
* @return mixed |
202
|
|
|
*/ |
203
|
|
|
public function getHeader($name) |
204
|
|
|
{ |
205
|
|
|
if ($this->response) { |
206
|
|
|
return $this->response->getHeaderLine($name); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get all response headers values. |
212
|
|
|
* |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
public function getHeaders() |
216
|
|
|
{ |
217
|
|
|
return $this->response ? $this->response->getHeaders() : []; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Get response body. |
222
|
|
|
* |
223
|
|
|
* @return \GuzzleHttp\Psr7\Stream|null |
224
|
|
|
*/ |
225
|
|
|
public function getBody() |
226
|
|
|
{ |
227
|
|
|
if ($this->response) { |
228
|
|
|
return $this->response->getBody(); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get response content. |
234
|
|
|
* |
235
|
|
|
* @return string|null |
236
|
|
|
*/ |
237
|
|
|
public function getContent() |
238
|
|
|
{ |
239
|
|
|
if ($body = $this->getBody()) { |
240
|
|
|
return (string) $body; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get JSON decoded response content. |
246
|
|
|
* |
247
|
|
|
* @param bool $assoc |
248
|
|
|
* @return mixed |
249
|
|
|
*/ |
250
|
|
|
public function getJson($assoc = true) |
251
|
|
|
{ |
252
|
|
|
if ($content = $this->getContent()) { |
253
|
|
|
return json_decode($content, $assoc); |
254
|
|
|
} |
255
|
|
|
} |
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 = []) |
266
|
|
|
{ |
267
|
|
|
try { |
268
|
|
|
$this->response = $this->client->request($method, $url, $options); |
269
|
|
|
} catch (Exception $e) { |
270
|
|
|
if ($this->withExceptions) { |
271
|
|
|
throw $e; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $this; |
276
|
|
|
} |
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 = []) |
287
|
|
|
{ |
288
|
|
|
Arr::set($options, 'headers.Accept', 'application/json'); |
289
|
|
|
|
290
|
|
|
return $this->request($url, $method, $options); |
291
|
|
|
} |
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 = []) |
302
|
|
|
{ |
303
|
|
|
return $this->request($url, $method, $options)->getContent(); |
304
|
|
|
} |
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) |
316
|
|
|
{ |
317
|
|
|
return $this->requestJson($url, $method, $options)->getJson($assoc); |
318
|
|
|
} |
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) |
328
|
|
|
{ |
329
|
|
|
return $this->option(Str::snake($name), $args[0]); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|