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