1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\Curl; |
4
|
|
|
|
5
|
|
|
class Request |
6
|
|
|
{ |
7
|
|
|
use UserAgentTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Curl resource handle. |
11
|
|
|
* |
12
|
|
|
* @var resource |
13
|
|
|
*/ |
14
|
|
|
private $handle; |
15
|
|
|
|
16
|
|
|
/** @var string contains targeted URL */ |
17
|
|
|
private $url; |
18
|
|
|
|
19
|
|
|
/** @var int */ |
20
|
|
|
private $returnHeader = 0; |
21
|
|
|
|
22
|
|
|
/** @var mixed */ |
23
|
|
|
private $filter; |
24
|
|
|
|
25
|
|
|
/** @var bool */ |
26
|
|
|
private $optChangeDuringRequest = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
* |
31
|
|
|
* @param string $ur to request |
32
|
|
|
*/ |
33
|
18 |
|
public function __construct(?string $url = null) |
34
|
|
|
{ |
35
|
18 |
|
$this->handle = curl_init(); |
36
|
18 |
|
$this->setOpt(CURLOPT_RETURNTRANSFER, 1); |
37
|
|
|
|
38
|
18 |
|
if (null !== $url) { |
39
|
18 |
|
$this->setUrl($url); |
40
|
|
|
} |
41
|
18 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Change the URL to cURL. |
45
|
|
|
* |
46
|
|
|
* @param string $url to request |
47
|
|
|
* |
48
|
|
|
* @return self |
49
|
|
|
*/ |
50
|
18 |
|
public function setUrl(string $url) |
51
|
|
|
{ |
52
|
18 |
|
$this->url = $url; |
53
|
18 |
|
$this->setOpt(CURLOPT_URL, $url); |
54
|
|
|
|
55
|
18 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add a cURL's option. |
60
|
|
|
* |
61
|
|
|
* @param int $option cURL Predefined Constant |
62
|
|
|
* @param mixed $value |
63
|
|
|
* |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
18 |
|
public function setOpt($option, $value) |
67
|
|
|
{ |
68
|
18 |
|
curl_setopt($this->handle, $option, $value); |
69
|
|
|
|
70
|
18 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* A short way to set some classic options to cURL a web page. |
75
|
|
|
* |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
18 |
|
public function setDefaultGetOptions( |
79
|
|
|
$connectTimeOut = 5, |
80
|
|
|
$timeOut = 10, |
81
|
|
|
$dnsCacheTimeOut = 600, |
82
|
|
|
$followLocation = true, |
83
|
|
|
$maxRedirs = 5 |
84
|
|
|
) { |
85
|
|
|
$this |
86
|
18 |
|
->setOpt(CURLOPT_AUTOREFERER, 1) |
87
|
18 |
|
->setOpt(CURLOPT_FOLLOWLOCATION, $followLocation) |
88
|
18 |
|
->setOpt(CURLOPT_MAXREDIRS, $maxRedirs) |
89
|
18 |
|
->setOpt(CURLOPT_CONNECTTIMEOUT, $connectTimeOut) |
90
|
18 |
|
->setOpt(CURLOPT_DNS_CACHE_TIMEOUT, $dnsCacheTimeOut) |
91
|
18 |
|
->setOpt(CURLOPT_TIMEOUT, $timeOut) |
92
|
|
|
//->setOpt(CURLOPT_SSL_VERIFYPEER, 0); |
93
|
|
|
; |
94
|
|
|
|
95
|
18 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* A short way to set some classic options to cURL a web page quickly |
100
|
|
|
* (but loosing some data like header, cookie...). |
101
|
|
|
* |
102
|
|
|
* @return self |
103
|
|
|
*/ |
104
|
3 |
|
public function setDefaultSpeedOptions() |
105
|
|
|
{ |
106
|
3 |
|
$this->setOpt(CURLOPT_SSL_VERIFYHOST, 0); |
107
|
3 |
|
$this->setOpt(CURLOPT_SSL_VERIFYPEER, 0); |
108
|
|
|
|
109
|
3 |
|
if (!$this->returnHeader) { |
110
|
3 |
|
$this->setOpt(CURLOPT_HEADER, 0); |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
$this->setDefaultGetOptions(5, 10, 600, true, 1); |
114
|
3 |
|
$this->setEncodingGzip(); |
115
|
|
|
|
116
|
3 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Call it if you want header informations. |
121
|
|
|
* After self::execute(), you would have this informations with getHeader();. |
122
|
|
|
* |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
18 |
|
public function setReturnHeader($only = false) |
126
|
|
|
{ |
127
|
18 |
|
$this->setOpt(CURLOPT_HEADER, 1); |
128
|
18 |
|
$this->returnHeader = $only ? 2 : 1; |
129
|
|
|
|
130
|
18 |
|
if ($only) { |
131
|
|
|
$this->setOpt(CURLOPT_RETURNTRANSFER, 0); |
132
|
|
|
$this->setOpt(CURLOPT_NOBODY, 1); |
133
|
|
|
} |
134
|
|
|
|
135
|
18 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* An self::setOpt()'s alias to add a cookie to your request. |
140
|
|
|
* |
141
|
|
|
* @param string $cookie |
142
|
|
|
* |
143
|
|
|
* @return self |
144
|
|
|
*/ |
145
|
3 |
|
public function setCookie($cookie) |
146
|
|
|
{ |
147
|
3 |
|
$this->setOpt(CURLOPT_COOKIE, $cookie); |
148
|
|
|
|
149
|
3 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* An self::setOpt()'s alias to add a referer to your request. |
154
|
|
|
* |
155
|
|
|
* @param string $referer |
156
|
|
|
* |
157
|
|
|
* @return self |
158
|
|
|
*/ |
159
|
15 |
|
public function setReferer($referer) |
160
|
|
|
{ |
161
|
15 |
|
$this->setOpt(CURLOPT_REFERER, $referer); |
162
|
|
|
|
163
|
15 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* An self::setOpt()'s alias to add an user-agent to your request. |
168
|
|
|
* |
169
|
|
|
* @param string $ua |
170
|
|
|
* |
171
|
|
|
* @return self |
172
|
|
|
*/ |
173
|
18 |
|
public function setUserAgent(string $ua) |
174
|
|
|
{ |
175
|
18 |
|
$this->setOpt(CURLOPT_USERAGENT, $ua); |
176
|
|
|
|
177
|
18 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* A short way to set post's options to cURL a web page. |
182
|
|
|
* |
183
|
|
|
* @param mixed $post if it's an array, will be converted via http build query |
184
|
|
|
* |
185
|
|
|
* @return self |
186
|
|
|
*/ |
187
|
|
|
public function setPost($post) |
188
|
|
|
{ |
189
|
|
|
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'POST'); |
190
|
|
|
$this->setOpt(CURLOPT_POST, 1); |
191
|
|
|
$this->setOpt(CURLOPT_POSTFIELDS, is_array($post) ? http_build_query($post) : $post); |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* If you want to request the URL and hope get the result gzipped. |
198
|
|
|
* The output will be automatically uncompress with execute();. |
199
|
|
|
* |
200
|
|
|
* @return self |
201
|
|
|
*/ |
202
|
18 |
|
public function setEncodingGzip() |
203
|
|
|
{ |
204
|
18 |
|
$this->setOpt(CURLOPT_ENCODING, 'gzip, deflate'); |
205
|
|
|
|
206
|
18 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* If you want to request the URL with a (http|socks...) proxy (public or private). |
211
|
|
|
* |
212
|
|
|
* @param string $proxy [scheme]IP:PORT[:LOGIN:PASSWORD] |
213
|
|
|
* Eg. : socks5://98.023.023.02:1098:cUrlRequestProxId:SecretPassword |
214
|
|
|
* |
215
|
|
|
* @return self |
216
|
|
|
*/ |
217
|
|
|
public function setProxy($proxy) |
218
|
|
|
{ |
219
|
|
|
if (!empty($proxy)) { |
220
|
|
|
$scheme = Helper::getSchemeFrom($proxy); |
221
|
|
|
$proxy = explode(':', $proxy); |
222
|
|
|
$this->setOpt(CURLOPT_HTTPPROXYTUNNEL, 1); |
223
|
|
|
$this->setOpt(CURLOPT_PROXY, $scheme.$proxy[0].':'.$proxy[1]); |
224
|
|
|
if (isset($proxy[2])) { |
225
|
|
|
$this->setOpt(CURLOPT_PROXYUSERPWD, $proxy[2].':'.$proxy[3]); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param mixed $ContentType string or array |
234
|
|
|
* |
235
|
|
|
* @return self |
236
|
|
|
*/ |
237
|
15 |
|
public function setDownloadOnlyIf($ContentType = 'text/html') |
238
|
|
|
{ |
239
|
15 |
|
$this->setReturnHeader(); |
240
|
|
|
|
241
|
15 |
|
$this->filter = is_array($ContentType) ? $ContentType : [$ContentType]; |
242
|
15 |
|
$this->setOpt(CURLOPT_HEADERFUNCTION, [$this, 'checkHeaderContentType']); |
243
|
15 |
|
$this->setOpt(CURLOPT_NOBODY, 1); |
244
|
|
|
|
245
|
15 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
15 |
|
public function checkHeaderContentType($handle, $line) |
249
|
|
|
{ |
250
|
15 |
|
if (is_string($line)) { |
251
|
15 |
|
foreach ($this->filter as $filter) { |
252
|
15 |
|
if (Helper::checkContentType($line, $filter)) { |
253
|
12 |
|
$this->optChangeDuringRequest = true; |
254
|
15 |
|
$this->setOpt(CURLOPT_NOBODY, 0); //curl_setopt($handle, CURLOPT_NOBODY, 0); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
15 |
|
return strlen($line); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Execute the request. |
264
|
|
|
* |
265
|
|
|
* @return Response |
266
|
|
|
*/ |
267
|
18 |
|
public function exec($optChange = false) |
268
|
|
|
{ |
269
|
18 |
|
$return = Response::get($this->handle, $this->url, $this->returnHeader); |
270
|
|
|
|
271
|
|
|
// Permits to transform HEAD request in GET request |
272
|
18 |
|
if ($this->optChangeDuringRequest && false === $optChange) { |
273
|
12 |
|
$this->optChangeDuringRequest = true; |
274
|
|
|
|
275
|
12 |
|
return $this->exec(true); |
276
|
|
|
} |
277
|
|
|
|
278
|
18 |
|
if ($return instanceof Response) { |
279
|
15 |
|
$this->setReferer($return->getEffectiveUrl()); |
280
|
|
|
} |
281
|
|
|
|
282
|
18 |
|
return $return; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Return the last error number (curl_errno). |
287
|
|
|
* |
288
|
|
|
* @return int the error number or 0 (zero) if no error occurred |
289
|
|
|
*/ |
290
|
|
|
public function hasError() |
291
|
|
|
{ |
292
|
|
|
return curl_errno($this->handle); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Return a string containing the last error for the current session (curl_error). |
297
|
|
|
* |
298
|
|
|
* @return string the error message or '' (the empty string) if no error occurred |
299
|
|
|
*/ |
300
|
|
|
public function getError() |
301
|
|
|
{ |
302
|
|
|
return curl_error($this->handle); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get information regarding the request. |
307
|
|
|
* |
308
|
|
|
* @param int $opt This may be one of the following constants: |
309
|
|
|
* http://php.net/manual/en/function.curl-getinfo.php |
310
|
|
|
* |
311
|
|
|
* @return array|string|false |
312
|
|
|
*/ |
313
|
|
|
public function getInfo(?int $opt) |
314
|
|
|
{ |
315
|
|
|
return $opt ? curl_getinfo($this->handle, $opt) : curl_getinfo($this->handle); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Close the connexion |
320
|
|
|
* Call curl_reset function. |
321
|
|
|
*/ |
322
|
|
|
public function close() |
323
|
|
|
{ |
324
|
|
|
curl_reset($this->handle); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|