1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\UrlHarvester; |
4
|
|
|
|
5
|
|
|
use PiedWeb\Curl\Request as CurlRequest; |
6
|
|
|
use PiedWeb\Curl\Response; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Request a page and get it only if it's an html page. |
10
|
|
|
*/ |
11
|
|
|
class Request |
12
|
|
|
{ |
13
|
|
|
private string $url; |
14
|
|
|
|
15
|
|
|
private string $userAgent; |
16
|
|
|
|
17
|
|
|
private string $language; |
18
|
|
|
|
19
|
|
|
private ?string $proxy; |
20
|
|
|
|
21
|
|
|
public int $maxSize = 1000000; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param bool $tryHttps |
25
|
|
|
* |
26
|
|
|
* @return Response|int corresponding to the curl error |
27
|
|
|
*/ |
28
|
|
|
public static function make( |
29
|
|
|
string $url, |
30
|
|
|
string $userAgent, |
31
|
|
|
string $language = 'en,en-US;q=0.5', |
32
|
|
|
?string $proxy = null, |
33
|
|
|
int $documentMaxSize = 1000000 |
34
|
|
|
) { |
35
|
|
|
return self::makeFromRequest(null, $url, $userAgent, $language, $proxy, $documentMaxSize); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function makeFromRequest( |
39
|
|
|
?CurlRequest $curlRequest = null, |
40
|
|
|
string $url, |
41
|
|
|
string $userAgent, |
42
|
|
|
string $language = 'en,en-US;q=0.5', |
43
|
|
|
?string $proxy = null, |
44
|
|
|
int $documentMaxSize = 1000000 |
45
|
|
|
) { |
46
|
|
|
$request = new self($url); |
47
|
|
|
|
48
|
6 |
|
$request->userAgent = $userAgent; |
49
|
|
|
$request->language = $language; |
50
|
|
|
$request->proxy = $proxy; |
51
|
|
|
$request->maxSize = $documentMaxSize; |
52
|
|
|
|
53
|
|
|
return $request->request($curlRequest); |
54
|
6 |
|
} |
55
|
|
|
|
56
|
|
|
private function __construct($url) |
57
|
24 |
|
{ |
58
|
|
|
/* |
59
|
|
|
if (!filter_var($string, FILTER_VALIDATE_URL)) { |
60
|
|
|
throw new \Exception('URL invalid: '.$string); |
61
|
|
|
}**/ |
62
|
|
|
$this->url = $url; |
63
|
|
|
} |
64
|
24 |
|
|
65
|
|
|
/** |
66
|
24 |
|
* Prepare headers as a normal browser (same order, same content). |
67
|
24 |
|
*/ |
68
|
24 |
|
private function prepareHeadersForRequest(): array |
69
|
|
|
{ |
70
|
24 |
|
//$host = parse_url($this->url, PHP_URL_HOST); |
71
|
|
|
|
72
|
|
|
$headers = []; |
73
|
24 |
|
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'; |
74
|
|
|
$headers[] = 'Accept-Encoding: gzip, deflate'; |
75
|
|
|
$headers[] = 'Accept-Language: '.$this->language; |
76
|
|
|
$headers[] = 'Connection: keep-alive'; |
77
|
|
|
|
78
|
|
|
//if ($host) { |
79
|
24 |
|
//$headers[] = 'Host: '.$host; |
80
|
24 |
|
//} |
81
|
|
|
// Referer |
82
|
|
|
|
83
|
|
|
$headers[] = 'Upgrade-Insecure-Requests: 1'; |
84
|
|
|
$headers[] = 'User-Agent: '.$this->userAgent; |
85
|
|
|
|
86
|
|
|
return $headers; |
87
|
24 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Response|int corresponding to the curl error |
91
|
24 |
|
*/ |
92
|
24 |
|
private function request(?CurlRequest $request = null) |
93
|
24 |
|
{ |
94
|
24 |
|
$request = null !== $request ? $request : new CurlRequest(); |
95
|
24 |
|
$request |
96
|
|
|
->setUrl($this->url) |
97
|
|
|
->setReturnHeader() |
98
|
|
|
->setEncodingGzip() |
99
|
|
|
->setUserAgent($this->userAgent) |
100
|
|
|
->setDefaultSpeedOptions() |
101
|
|
|
->setOpt(\CURLOPT_SSL_VERIFYHOST, 0) |
102
|
24 |
|
->setOpt(\CURLOPT_SSL_VERIFYPEER, 0) |
103
|
24 |
|
->setOpt(\CURLOPT_MAXREDIRS, 0) |
104
|
|
|
->setOpt(\CURLOPT_FOLLOWLOCATION, false) |
105
|
24 |
|
->setOpt(\CURLOPT_COOKIE, false) |
106
|
|
|
->setOpt(\CURLOPT_CONNECTTIMEOUT, 20) |
107
|
|
|
->setOpt(\CURLOPT_TIMEOUT, 80) |
108
|
|
|
->setAbortIfTooBig($this->maxSize); // 2Mo |
109
|
|
|
|
110
|
|
|
if ($this->proxy) { |
111
|
24 |
|
$request->setProxy($this->proxy); |
112
|
|
|
} |
113
|
24 |
|
|
114
|
24 |
|
$request->setOpt(\CURLOPT_HTTPHEADER, $this->prepareHeadersForRequest()); |
115
|
24 |
|
|
116
|
24 |
|
$response = $request->exec(); |
117
|
24 |
|
//dd($this->request->exec()); |
118
|
24 |
|
|
119
|
24 |
|
return $response; |
120
|
24 |
|
} |
121
|
|
|
} |
122
|
|
|
|