1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-library package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Library\Core\Network\CURL\Configuration; |
13
|
|
|
|
14
|
|
|
use WBW\Library\Core\Model\Attribute\BooleanDebugTrait; |
15
|
|
|
use WBW\Library\Core\Model\Attribute\BooleanVerboseTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* cURL configuration. |
19
|
|
|
* |
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
21
|
|
|
* @package WBW\Library\Core\Network\CURL\Configuration |
22
|
|
|
*/ |
23
|
|
|
class CurlConfiguration { |
24
|
|
|
|
25
|
|
|
use BooleanDebugTrait; |
26
|
|
|
use BooleanVerboseTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Allow encoding. |
30
|
|
|
* |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $allowEncoding; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Connect timeout. |
37
|
|
|
* |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
private $connectTimeout; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Debug file. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $debugFile; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Headers. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private $headers; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Host. |
58
|
|
|
* |
59
|
|
|
* @var string|null |
60
|
|
|
*/ |
61
|
|
|
private $host; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* HTTP basic password. |
65
|
|
|
* |
66
|
|
|
* @var string|null |
67
|
|
|
*/ |
68
|
|
|
private $httpPassword; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* HTTP basic username. |
72
|
|
|
* |
73
|
|
|
* @var string|null |
74
|
|
|
*/ |
75
|
|
|
private $httpUsername; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Proxy host. |
79
|
|
|
* |
80
|
|
|
* @var string|null |
81
|
|
|
*/ |
82
|
|
|
private $proxyHost; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Proxy password. |
86
|
|
|
* |
87
|
|
|
* @var string|null |
88
|
|
|
*/ |
89
|
|
|
private $proxyPassword; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Proxy port. |
93
|
|
|
* |
94
|
|
|
* @var int|null |
95
|
|
|
*/ |
96
|
|
|
private $proxyPort; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Proxy type. |
100
|
|
|
* |
101
|
|
|
* @var int|null |
102
|
|
|
*/ |
103
|
|
|
private $proxyType; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Proxy username. |
107
|
|
|
* |
108
|
|
|
* @var string|null |
109
|
|
|
*/ |
110
|
|
|
private $proxyUsername; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* HTTP request timeout. |
114
|
|
|
* |
115
|
|
|
* @var int |
116
|
|
|
*/ |
117
|
|
|
private $requestTimeout; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* SSL verification. |
121
|
|
|
* |
122
|
|
|
* @var bool |
123
|
|
|
*/ |
124
|
|
|
private $sslVerification; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* User agent. |
128
|
|
|
* |
129
|
|
|
* @var string |
130
|
|
|
*/ |
131
|
|
|
private $userAgent; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Constructor. |
135
|
|
|
*/ |
136
|
|
|
public function __construct() { |
137
|
|
|
$this->setAllowEncoding(false); |
138
|
|
|
$this->setConnectTimeout(0); |
139
|
|
|
$this->setDebug(false); |
140
|
|
|
$this->setDebugFile("php://output"); |
141
|
|
|
$this->setHeaders([]); |
142
|
|
|
$this->setRequestTimeout(0); |
143
|
|
|
$this->setSslVerification(true); |
144
|
|
|
$this->setUserAgent("webeweb/curl-library"); |
145
|
|
|
$this->setVerbose(false); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Add an header. |
150
|
|
|
* |
151
|
|
|
* @param string $name The header name. |
152
|
|
|
* @param string $value The header value. |
153
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
154
|
|
|
*/ |
155
|
|
|
public function addHeader(string $name, string $value): CurlConfiguration { |
156
|
|
|
$this->headers[$name] = $value; |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Clear the headers. |
162
|
|
|
* |
163
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
164
|
|
|
*/ |
165
|
|
|
public function clearHeaders(): CurlConfiguration { |
166
|
|
|
return $this->setHeaders([]); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get the allow encoding. |
171
|
|
|
* |
172
|
|
|
* @return bool Returns the allow encoding. |
173
|
|
|
*/ |
174
|
|
|
public function getAllowEncoding(): bool { |
175
|
|
|
return $this->allowEncoding; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the connect timeout. |
180
|
|
|
* |
181
|
|
|
* @return int Returns the connect timeout. |
182
|
|
|
*/ |
183
|
|
|
public function getConnectTimeout(): int { |
184
|
|
|
return $this->connectTimeout; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the debug file. |
189
|
|
|
* |
190
|
|
|
* @return string Returns the debug file. |
191
|
|
|
*/ |
192
|
|
|
public function getDebugFile(): string { |
193
|
|
|
return $this->debugFile; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get the headers. |
198
|
|
|
* |
199
|
|
|
* @return array Returns the headers. |
200
|
|
|
*/ |
201
|
|
|
public function getHeaders(): array { |
202
|
|
|
return $this->headers; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get the host. |
207
|
|
|
* |
208
|
|
|
* @return string|null Returns the host. |
209
|
|
|
*/ |
210
|
|
|
public function getHost(): ?string { |
211
|
|
|
return $this->host; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get the HTTP password. |
216
|
|
|
* |
217
|
|
|
* @return string|null Returns the HTTP password. |
218
|
|
|
*/ |
219
|
|
|
public function getHttpPassword(): ?string { |
220
|
|
|
return $this->httpPassword; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get the HTTP username. |
225
|
|
|
* |
226
|
|
|
* @return string|null Returns the HTTP username. |
227
|
|
|
*/ |
228
|
|
|
public function getHttpUsername(): ?string { |
229
|
|
|
return $this->httpUsername; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get the proxy host. |
234
|
|
|
* |
235
|
|
|
* @return string|null Returns the proxy host. |
236
|
|
|
*/ |
237
|
|
|
public function getProxyHost(): ?string { |
238
|
|
|
return $this->proxyHost; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get the proxy password. |
243
|
|
|
* |
244
|
|
|
* @return string|null Returns the proxy password. |
245
|
|
|
*/ |
246
|
|
|
public function getProxyPassword(): ?string { |
247
|
|
|
return $this->proxyPassword; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Get the proxy port. |
252
|
|
|
* |
253
|
|
|
* @return int|null Returns the proxy port. |
254
|
|
|
*/ |
255
|
|
|
public function getProxyPort(): ?int { |
256
|
|
|
return $this->proxyPort; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get the proxy type. |
261
|
|
|
* |
262
|
|
|
* @return int|null Returns the proxy type. |
263
|
|
|
*/ |
264
|
|
|
public function getProxyType(): ?int { |
265
|
|
|
return $this->proxyType; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Get the proxy username. |
270
|
|
|
* |
271
|
|
|
* @return string|null Returns the proxy username. |
272
|
|
|
*/ |
273
|
|
|
public function getProxyUsername(): ?string { |
274
|
|
|
return $this->proxyUsername; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Get the request timeout. |
279
|
|
|
* |
280
|
|
|
* @return int Returns the request timeout. |
281
|
|
|
*/ |
282
|
|
|
public function getRequestTimeout(): int { |
283
|
|
|
return $this->requestTimeout; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Get the SSL verification. |
288
|
|
|
* |
289
|
|
|
* @return bool Returns the SSL verification. |
290
|
|
|
*/ |
291
|
|
|
public function getSslVerification(): bool { |
292
|
|
|
return $this->sslVerification; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Get the user agent. |
297
|
|
|
* |
298
|
|
|
* @return string Returns the user agent. |
299
|
|
|
*/ |
300
|
|
|
public function getUserAgent(): string { |
301
|
|
|
return $this->userAgent; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Remove an header. |
306
|
|
|
* |
307
|
|
|
* @param string $name The header name. |
308
|
|
|
* @return void |
309
|
|
|
*/ |
310
|
|
|
public function removeHeader(string $name): void { |
311
|
|
|
if (true === array_key_exists($name, $this->headers)) { |
312
|
|
|
unset($this->headers[$name]); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Set the allow encoding. |
318
|
|
|
* |
319
|
|
|
* @param bool $allowEncoding The allow encoding. |
320
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
321
|
|
|
*/ |
322
|
|
|
public function setAllowEncoding(bool $allowEncoding): CurlConfiguration { |
323
|
|
|
$this->allowEncoding = $allowEncoding; |
324
|
|
|
return $this; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Set the connect timeout. |
329
|
|
|
* |
330
|
|
|
* @param int $connectTimeout The connect timeout. |
331
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
332
|
|
|
*/ |
333
|
|
|
public function setConnectTimeout(int $connectTimeout): CurlConfiguration { |
334
|
|
|
$this->connectTimeout = $connectTimeout; |
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Set the debug file. |
340
|
|
|
* |
341
|
|
|
* @param string $debugFile The debug file. |
342
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
343
|
|
|
*/ |
344
|
|
|
public function setDebugFile(string $debugFile): CurlConfiguration { |
345
|
|
|
$this->debugFile = $debugFile; |
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Set the headers. |
351
|
|
|
* |
352
|
|
|
* @param array $headers The headers |
353
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
354
|
|
|
*/ |
355
|
|
|
protected function setHeaders(array $headers): CurlConfiguration { |
356
|
|
|
$this->headers = $headers; |
357
|
|
|
return $this; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Set the host. |
362
|
|
|
* |
363
|
|
|
* @param string $host The host. |
364
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
365
|
|
|
*/ |
366
|
|
|
public function setHost(string $host): CurlConfiguration { |
367
|
|
|
$this->host = preg_replace("/\/$/", "", trim($host)); |
368
|
|
|
return $this; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Set the HTTP basic password. |
373
|
|
|
* |
374
|
|
|
* @param string|null $httpPassword The HTTP basic password. |
375
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
376
|
|
|
*/ |
377
|
|
|
public function setHttpPassword(?string $httpPassword): CurlConfiguration { |
378
|
|
|
$this->httpPassword = $httpPassword; |
379
|
|
|
return $this; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Set the HTTP basic username. |
384
|
|
|
* |
385
|
|
|
* @param string|null $httpUsername The HTTP basic username. |
386
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
387
|
|
|
*/ |
388
|
|
|
public function setHttpUsername(?string $httpUsername): CurlConfiguration { |
389
|
|
|
$this->httpUsername = $httpUsername; |
390
|
|
|
return $this; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Set the proxy host. |
395
|
|
|
* |
396
|
|
|
* @param string|null $proxyHost The proxy host. |
397
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
398
|
|
|
*/ |
399
|
|
|
public function setProxyHost(?string $proxyHost): CurlConfiguration { |
400
|
|
|
$this->proxyHost = $proxyHost; |
401
|
|
|
return $this; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Set the proxy password. |
406
|
|
|
* |
407
|
|
|
* @param string|null $proxyPassword The proxy password. |
408
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
409
|
|
|
*/ |
410
|
|
|
public function setProxyPassword(?string $proxyPassword): CurlConfiguration { |
411
|
|
|
$this->proxyPassword = $proxyPassword; |
412
|
|
|
return $this; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Set the proxy port. |
417
|
|
|
* |
418
|
|
|
* @param int|null $proxyPort The proxy port. |
419
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
420
|
|
|
*/ |
421
|
|
|
public function setProxyPort(?int $proxyPort): CurlConfiguration { |
422
|
|
|
$this->proxyPort = $proxyPort; |
423
|
|
|
return $this; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Set the proxy type. |
428
|
|
|
* |
429
|
|
|
* @param int|null $proxyType The proxy type. |
430
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
431
|
|
|
*/ |
432
|
|
|
public function setProxyType(?int $proxyType): CurlConfiguration { |
433
|
|
|
$this->proxyType = $proxyType; |
434
|
|
|
return $this; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Set the proxy username. |
439
|
|
|
* |
440
|
|
|
* @param string|null $proxyUsername The proxy username. |
441
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
442
|
|
|
*/ |
443
|
|
|
public function setProxyUsername(?string $proxyUsername): CurlConfiguration { |
444
|
|
|
$this->proxyUsername = $proxyUsername; |
445
|
|
|
return $this; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Set the request timeout. |
450
|
|
|
* |
451
|
|
|
* @param int $requestTimeout The request timeout. |
452
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
453
|
|
|
*/ |
454
|
|
|
public function setRequestTimeout(int $requestTimeout): CurlConfiguration { |
455
|
|
|
$this->requestTimeout = $requestTimeout; |
456
|
|
|
return $this; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Set the SSL verification. |
461
|
|
|
* |
462
|
|
|
* @param bool $sslVerification The SSL verification. |
463
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
464
|
|
|
*/ |
465
|
|
|
public function setSslVerification(bool $sslVerification): CurlConfiguration { |
466
|
|
|
$this->sslVerification = $sslVerification; |
467
|
|
|
return $this; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Set the user agent. |
472
|
|
|
* |
473
|
|
|
* @param string $userAgent The user agent. |
474
|
|
|
* @return CurlConfiguration Returns this cURL configuration. |
475
|
|
|
*/ |
476
|
|
|
public function setUserAgent(string $userAgent): CurlConfiguration { |
477
|
|
|
$this->userAgent = $userAgent; |
478
|
|
|
return $this; |
479
|
|
|
} |
480
|
|
|
} |
481
|
|
|
|