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