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
|
|
|
* Create a http client instance. |
38
|
|
|
* |
39
|
|
|
* @param array|string $config base_uri or any request options |
40
|
|
|
*/ |
41
|
|
|
public function __construct($config = null) |
42
|
|
|
{ |
43
|
|
|
if (is_string($config)) { |
44
|
|
|
$this->mergeOptions(['base_uri' => $config]); |
45
|
|
|
} else if (is_array($config)) { |
46
|
|
|
$this->mergeOptions($config); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->client = new Client($this->options); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the Guzzle client instance. |
54
|
|
|
* |
55
|
|
|
* @return \GuzzleHttp\Client |
56
|
|
|
*/ |
57
|
|
|
public function getClient() |
58
|
|
|
{ |
59
|
|
|
return $this->client; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the request options. |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function getOptions() |
68
|
|
|
{ |
69
|
|
|
return $this->options; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Merge request options. |
74
|
|
|
* |
75
|
|
|
* @param array $options |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function mergeOptions(array ...$options) |
79
|
|
|
{ |
80
|
|
|
$this->options = array_merge_recursive($this->options, ...$options); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
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) |
93
|
|
|
{ |
94
|
|
|
if ($key) { |
95
|
|
|
Arr::set($this->options, $key, $value); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
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) |
109
|
|
|
{ |
110
|
|
|
return $this->option('headers.'.$name, $value); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the request content type. |
115
|
|
|
* |
116
|
|
|
* @param string $type |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function contentType($type) |
120
|
|
|
{ |
121
|
|
|
return $this->header('Content-Type', $type); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set the request accept type. |
126
|
|
|
* |
127
|
|
|
* @param string $type |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function accept($type) |
131
|
|
|
{ |
132
|
|
|
return $this->header('Accept', $type); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set the request accept type to JSON. |
137
|
|
|
* |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function acceptJson() |
141
|
|
|
{ |
142
|
|
|
return $this->accept('application/json'); |
143
|
|
|
} |
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) |
153
|
|
|
{ |
154
|
|
|
return $this->option('sink', $value); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the Guzzle response instance. |
159
|
|
|
* |
160
|
|
|
* @return \GuzzleHttp\Psr7\Response|null |
161
|
|
|
*/ |
162
|
|
|
public function getResponse() |
163
|
|
|
{ |
164
|
|
|
return $this->response; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the status code of response. |
169
|
|
|
* |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
|
|
public function getStatusCode() |
173
|
|
|
{ |
174
|
|
|
if ($this->response) { |
175
|
|
|
return $this->response->getStatusCode(); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the response header value. |
181
|
|
|
* |
182
|
|
|
* @param string $name |
183
|
|
|
* @return mixed |
184
|
|
|
*/ |
185
|
|
|
public function getHeader($name) |
186
|
|
|
{ |
187
|
|
|
if ($this->response) { |
188
|
|
|
return $this->response->getHeaderLine($name); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get all response headers values. |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
|
public function getHeaders() |
198
|
|
|
{ |
199
|
|
|
return $this->response ? $this->response->getHeaders() : []; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get response body. |
204
|
|
|
* |
205
|
|
|
* @return \GuzzleHttp\Psr7\Stream|null |
206
|
|
|
*/ |
207
|
|
|
public function getBody() |
208
|
|
|
{ |
209
|
|
|
if ($this->response) { |
210
|
|
|
return $this->response->getBody(); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get response content. |
216
|
|
|
* |
217
|
|
|
* @return string|null |
218
|
|
|
*/ |
219
|
|
|
public function getContent() |
220
|
|
|
{ |
221
|
|
|
if ($body = $this->getBody()) { |
222
|
|
|
return (string) $body; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get JSON decoded response content. |
228
|
|
|
* |
229
|
|
|
* @param bool $assoc |
230
|
|
|
* @return mixed |
231
|
|
|
*/ |
232
|
|
|
public function getJson($assoc = true) |
233
|
|
|
{ |
234
|
|
|
if ($content = $this->getContent()) { |
235
|
|
|
return json_decode($content, $assoc); |
236
|
|
|
} |
237
|
|
|
} |
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 = []) |
248
|
|
|
{ |
249
|
|
|
try { |
250
|
|
|
$this->response = $this->client->request($method, $url, $options); |
251
|
|
|
} catch (Exception $e) { |
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $this; |
255
|
|
|
} |
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 = []) |
266
|
|
|
{ |
267
|
|
|
Arr::set($options, 'headers.Accept', 'application/json'); |
268
|
|
|
|
269
|
|
|
return $this->request($url, $method, $options); |
270
|
|
|
} |
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 = []) |
281
|
|
|
{ |
282
|
|
|
return $this->request($url, $method, $options)->getContent(); |
283
|
|
|
} |
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) |
295
|
|
|
{ |
296
|
|
|
return $this->requestJson($url, $method, $options)->getJson($assoc); |
297
|
|
|
} |
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) |
307
|
|
|
{ |
308
|
|
|
return $this->option(Str::snake($name), $args[0]); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|