-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebRequest.php
267 lines (228 loc) · 7.5 KB
/
WebRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* @author Abraham Toledo <abrahamtoledo90@gmail.com>
* @copyright 2019 Abraham Toledo
* @
*/
/**
* Represents an HTTP Request. A pool is used to allow several downloads in paralel.
*/
class WebRequest
{
public const DATA_URLENCODED = 0;
public const DATA_MULTIPART = 1;
public const DATA_JSON = 2;
public const DATA_RAW = 3;
/**
* The url to fetch
*
* @var string
*/
protected $url;
public function getUrl(){
return $this->url;
}
public function setUrl($val){
$this->url = $val;
}
/**
* HTTP Method of the request [POST, GET, PUT, DELETE, PATCH]
*
* @var string
*/
protected $method;
public function getMethod(){
return $this->method;
}
public function setMethod($val){
$this->method = $val;
}
/**
* A Dictionary with post data. "name" => "value"
*
* @var array
*/
protected $postData;
public function getPostData(){
return $this->postData;
}
public function setPostData($val){
$this->postData = $val;
}
/**
* An array with HTTP Headers to be sent. This will override any default Header set by CURL
*
* @var array
*/
protected $headers;
public function getHeaders(){
return $this->headers;
}
public function setHeaders($val){
$this->headers = $val;
}
/**
* The timeout for this request
*
* @var int
*/
protected $timeout;
public function getTimeout(){
return $this->timeout;
}
public function setTimeout($val){
$this->timeout = $val;
}
/**
* Specify if the remote certificate must be validated for secure connections
*
* @var bool
*/
protected $verifySsl;
public function getVerifySsl(){
return $this->verifySsl;
}
public function setVerifySsl($val){
$this->verifySsl = $val;
}
/**
* A file containing a CookieJar that will be used with this WebRequest
*
* @var string
*/
protected $cookieJar = NULL;
public function getCookieJar(){
return $this->cookieJar;
}
public function setCookieJar($val){
$this->cookieJar = $val;
}
/**
* The transfer info.
*
* @var array
*/
protected $transferInfo;
public function getTransferInfo(){
return $this->transferInfo;
}
/**
* The response content without headers
*
* @var string
*/
protected $response;
public function getResponse(){
return $this->response;
}
/**
* Sets the type of data to post. Valid values are DATA_URLENCODED, DATA_MULTIPART, DATA_JSON, DATA_RAW
*
* @var int
*/
protected $dataType = self::DATA_URLENCODED;
public function getDataType(){
return $this->dataType;
}
public function setDataType($val){
if (in_array($val, [
self::DATA_URLENCODED,
self::DATA_MULTIPART,
self::DATA_JSON,
self::DATA_RAW,
])){
$this->dataType = $val;
}
}
public function __construct($url = NULL, $method="GET", $postData=NULL, $headers=[], $verifySsl=true, $timeout=0){
$this->setUrl($url);
$this->setMethod($method);
$this->setPostData($postData);
$this->setHeaders($headers);
$this->setVerifySsl($verifySsl);
$this->setTimeout($timeout);
}
public static function requestAll($requests, $maxThreads = 20){
if ($requests instanceof WebRequest){
$requests = [$requests];
}
if (!is_array($requests)){
throw new InvalidArgumentException("\$request debe ser una instancia de "
. __CLASS__ . " o una lista de instancias", 1);
}
$mh = curl_multi_init();
$ch = array();
// Run Loop. Dinamically adds curl resources;
$c = count($requests);
$running = 0;
$i = 0;
do{
// While there are available threads:
// Add new handle to curl multiprocessor
while($i < $c && $running < $maxThreads){
$ch[$i] = curl_init($requests[$i]->url);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
$headers = $requests[$i]->headers;
if ($requests[$i]->dataType == self::DATA_JSON){
$headers[] = "Content-Type: application/json";
}
curl_setopt($ch[$i], CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch[$i], CURLOPT_HEADER, 0);
curl_setopt($ch[$i], CURLOPT_CUSTOMREQUEST, $requests[$i]->method);
if ($requests[$i]->postData){
$postData = $requests[$i]->postData;
switch ($requests[$i]->dataType) {
case self::DATA_URLENCODED:
$postData = join(
"&",
array_map(function($k, $v){
return urlencode($k) . "=" . urlencode($v);
}, array_keys($postData), $postData)
);
break;
case self::DATA_MULTIPART:
// DO nothing, the data is in the final format
break;
case self::DATA_JSON:
$postData = json_encode($postData);
break;
case self::DATA_RAW:
// Do nothing with the data
break;
}
curl_setopt($ch[$i], CURLOPT_POSTFIELDS, $postData);
}
curl_setopt($ch[$i], CURLOPT_TIMEOUT, $requests[$i]->timeout);
curl_setopt($ch[$i], CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch[$i], CURLOPT_MAXREDIRS, 15);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, $requests[$i]->verifySsl);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYHOST, $requests[$i]->verifySsl);
if ($requests[$i]->cookieJar && is_file($requests[$i]->cookieJar)){
curl_setopt($ch[$i], CURLOPT_COOKIEJAR, $requests[$i]->cookieJar);
curl_setopt($ch[$i], CURLOPT_COOKIEFILE, $requests[$i]->cookieJar);
}
curl_multi_add_handle($mh, $ch[$i]);
$i++; $running++;
}
// Advance the downloads progress
curl_multi_exec($mh, $running);
usleep(100);
} while($running > 0);
// Download is completed at this point
// Fetch results from curl
$contents = array();
$transfer_info = array();
for($i=0; $i < $c; $i++){
$cont = curl_multi_getcontent($ch[$i]);
// this is to copy the string rather than assign by reference
$requests[$i]->response = substr($cont, 0);
$requests[$i]->transferInfo = curl_getinfo($ch[$i]);
curl_multi_remove_handle($mh, $ch[$i]);
curl_close($ch[$i]);
}
return $requests;
}
public function request(){
return self::requestAll([$this]);
}
}