-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathHttpClient.php
More file actions
314 lines (261 loc) · 6.45 KB
/
HttpClient.php
File metadata and controls
314 lines (261 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
declare(strict_types=1);
namespace Bow\Http\Client;
use BadFunctionCallException;
use CurlHandle;
use Exception;
class HttpClient
{
/**
* The attached file collection
*
* @var array
*/
private array $attach = [];
/**
* Define the accept json header
*
* @var boolean
*/
private bool $accept_json = false;
/**
* The headers collection
*
* @var array
*/
private array $headers = [];
/**
* The curl instance
*
* @var ?CurlHandle
*/
private ?CurlHandle $ch = null;
/**
* The base url
*
* @var string|null
*/
private ?string $base_url = null;
/**
* HttpClient Constructor.
*
* @param string|null $base_url
*/
public function __construct(?string $base_url = null)
{
if (!function_exists('curl_init')) {
throw new BadFunctionCallException('cURL extension is required.');
}
if (!is_null($base_url)) {
$this->base_url = rtrim($base_url, "/");
}
}
/**
* Set the base url
*
* @param string $url
* @return void
*/
public function setBaseUrl(string $url): void
{
$this->base_url = rtrim($url, "/");
}
/**
* Make GET request
*
* @param string $url
* @param array $data
* @return Response
* @throws Exception
*/
public function get(string $url, array $data = []): Response
{
if (count($data) > 0) {
$url = $url . "?" . http_build_query($data);
}
$this->init($url);
$this->applyCommonOptions();
curl_setopt($this->ch, CURLOPT_HTTPGET, true);
$content = $this->execute();
return new Response($this->ch, $content);
}
/**
* Initialize connection with URL
*
* @param string $url
* @return void
*/
private function init(string $url): void
{
if (!is_null($this->base_url)) {
$url = $this->base_url . "/" . trim($url, "/");
}
$this->ch = curl_init(trim($url, "/"));
}
/**
* Apply common cURL options
*
* @return void
*/
private function applyCommonOptions(): void
{
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);
}
/**
* Execute request
*
* @return string
* @throws Exception
*/
private function execute(): string
{
if ($this->headers) {
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers);
}
$content = curl_exec($this->ch);
$errno = curl_errno($this->ch);
$this->close();
if ($content === false) {
throw new HttpClientException(
curl_strerror($errno),
$errno
);
}
return $content;
}
/**
* Close connection
*
* @return void
*/
private function close(): void
{
curl_close($this->ch);
}
/**
* Make POST request
*
* @param string $url
* @param array $data
* @return Response
* @throws Exception
*/
public function post(string $url, array $data = []): Response
{
$this->init($url);
if (!empty($this->attach)) {
curl_setopt($this->ch, CURLOPT_UPLOAD, true);
foreach ($this->attach as $key => $attach) {
$this->attach[$key] = '@' . ltrim('@', $attach);
}
$data = array_merge($this->attach, $data);
}
$this->addFields($data);
$this->applyCommonOptions();
curl_setopt($this->ch, CURLOPT_POST, true);
$content = $this->execute();
return new Response($this->ch, $content);
}
/**
* Add fields
*
* @param array $data
* @return void
*/
private function addFields(array $data): void
{
if (count($data) == 0) {
return;
}
if ($this->accept_json) {
$payload = json_encode($data);
} else {
$payload = http_build_query($data);
}
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $payload);
}
/**
* Make PUT request
*
* @param string $url
* @param array $data
* @return Response
* @throws Exception
*/
public function put(string $url, array $data = []): Response
{
$this->init($url);
$this->addFields($data);
$this->applyCommonOptions();
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "PUT");
$content = $this->execute();
return new Response($this->ch, $content);
}
/**
* Make DELETE request
*
* @param string $url
* @param array $data
* @return Response
* @throws Exception
*/
public function delete(string $url, array $data = []): Response
{
$this->init($url);
$this->addFields($data);
$this->applyCommonOptions();
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$content = $this->execute();
return new Response($this->ch, $content);
}
/**
* Attach file(s) to the request
*
* @param string|array $attach
* @return HttpClient
*/
public function addAttach(string|array $attach): HttpClient
{
$this->attach = (array)$attach;
return $this;
}
/**
* Set the User-Agent header
*
* @param string $user_agent
* @return HttpClient
*/
public function setUserAgent(string $user_agent): HttpClient
{
curl_setopt($this->ch, CURLOPT_USERAGENT, $user_agent);
return $this;
}
/**
* Configure client to accept and send JSON data
*
* @return HttpClient
*/
public function acceptJson(): HttpClient
{
$this->accept_json = true;
$this->addHeaders(["Content-Type" => "application/json"]);
return $this;
}
/**
* Add custom HTTP headers
*
* @param array $headers
* @return HttpClient
*/
public function addHeaders(array $headers): HttpClient
{
foreach ($headers as $key => $value) {
if (!in_array(strtolower($key . ': ' . $value), array_map('strtolower', $this->headers))) {
$this->headers[] = $key . ': ' . $value;
}
}
return $this;
}
}