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