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