-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequester.php
executable file
·522 lines (478 loc) · 14.1 KB
/
Requester.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<?php
/**
* Class for interact with Http Requests.
*
* @author Casiva Agustin
*/
class Requester {
const POST = 'POST';
const GET = 'GET';
const HEAD = 'HEAD';
const DELETE = 'DELETE';
const PUT = 'PUT';
const PROXY_AUTH_NTLM = 'NTLM';
const PROXY_AUTH_BASIC = 'BASIC';
const AUTH_BASIC = CURLAUTH_BASIC;
const AUTH_NTLM = CURLAUTH_NTLM;
const AUTH_DIGEST = CURLAUTH_DIGEST;
const AUTH_GSS = CURLAUTH_GSSNEGOTIATE;
const RESPONSE_RAW = 'raw';
const RESPONSE_ARRAY = 'array';
static protected $default_options = array(
CURLOPT_FRESH_CONNECT => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLINFO_HEADER_OUT => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_MAXREDIRS => 3,
CURLOPT_FAILONERROR => FALSE,
CURLOPT_HEADER => FALSE,
);
protected $url = '';
protected $method = self::GET;
protected $responseType = self::RESPONSE_RAW;
protected $options = array();
protected $lastHttpCode = 0;
/**
* Creates a Request Object
*
* @param $options
*
* @see resetOptions for more detail of the options
*/
public function __construct($options = array()) {
$this->resetOptions($options);
}
/**
* Executes the Request
*
* @param String $method : The HTTP method to use, by default use the internal Method.
* @param String $url : The url to hit
* @param Array $data : The Data to append in the body
* @param Array $params : The Parameters to append as a Query String
*
* @return String|Array|Boolean : The content or false on failure
*
* @throws Exception
*
*/
public function execute($method, $url, $data = null, $params = null) {
$this->setOptionUrl($url);
$this->setOptionMethod($method);
$this->setOptionData($data);
$this->setOptionParams($params);
$ch = curl_init();
curl_setopt_array($ch, $this->options);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
if (curl_errno($ch) > 0) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
if ($this->responseType === self::RESPONSE_ARRAY) {
$result = self::createArrayResponse($result, $info);
}
$this->lastHttpCode = $info['http_code'];
curl_close($ch);
return $result;
}
/**
* Executes a Get Request
*
* @param String $url
* @param Array|String $params
*
* @return String
*
* @throw Exception
*/
public function get($url, $params = null) {
return $this->execute(self::GET, $url, null, $params);
}
/**
* Executes a Post Request
*
* @param String $url
* @param Array|String $data : Data to add in the body of the Request
* @param Array|String $params : Parameters to include at the Url, ?arg1=1&arg2=2....
*
* @return String
*
* @throw Exception
*/
public function post($url, $data = null, $params = null) {
return $this->execute(self::POST, $url, $data , $params);
}
/**
* Executes a Put Request
*
* @param String $url
* @param Array|String $data : Data to add in the body of the Request
* @param Array|String $params : Parameters to include at the Url, ?arg1=1&arg2=2....
*
* @return String
*
* @throw Exception
*/
public function put($url, $data = null, $params = null) {
return $this->execute(self::PUT, $url, $data , $params);
}
/**
* Executes a Delete Request
*
* @param String $url
* @param Array|String $data : Data to add in the body of the Request
* @param Array|String $params : Parameters to include at the Url, ?arg1=1&arg2=2....
*
* @return String
*
* @throw Exception
*/
public function delete($url, $data = null, $params = null) {
return $this->execute(self::DELETE, $url, $data , $params);
}
/**
* Executes a Head Request
*
* @param String $url
* @param Array|String $data : Data to add in the body of the Request
* @param Array|String $params : Parameters to include at the Url, ?arg1=1&arg2=2....
*
* @return String
*
* @throw Exception
*/
public function head($url, $data = null, $params = null) {
return $this->execute(self::HEAD, $url, $data , $params);
}
/**
* Saves the Request in store path
*
* @param String $storePath : Full path to store the file
* @param String $url : The url to hit
* @param Array $data : The Data to append in the body
* @param Array $params : The Parameters to append as a Query String
* @param String $method : An HTTP Method, by default GET
* @return boolean : True on success False on fail
*/
public function save($storePath, $url, $data = null, $params = null, $method = self::GET) {
$fileContent = $this->execute($method, $url, $data, $params);
$fp = fopen($storePath,'w');
if ($fp !== FALSE) {
$writeStatus = fwrite($fp, $fileContent);
if ($writeStatus !== FALSE) {
fclose($fp);
return TRUE;
}
}
return FALSE;
}
/**
* Pings to the Url to check of works
*
* @param $url : Url to hit
* @return boolean : True on Success False on Fail
*/
public function ping($url) {
try {
if ($this->execute(self::HEAD, $url) !== FALSE) {
return TRUE;
}
} catch(Exception $e) {
return FALSE;
}
return FALSE;
}
/**
* Converts an array to a query string
*
* @param Array $params
*
* @return String
*/
public function buildQuery($params) {
if(is_array($params)) {
return http_build_query($params);
}
return $params;
}
/**
* Sets the Proxy Parameters
*
* @param Array $proxy : Array with Configurations
* array('url', //Proxy Url
* 'auth', //Proxy Auth credentials User:Pass, Optional
* 'auth_method'//Proxy Auth Method, BASIC / NTLM, Basic By Def
* )
* @return Requester
*/
public function setOptionProxy($proxy = FALSE) {
if ($proxy !== FALSE) {
$this->options[CURLOPT_PROXY] = $proxy['url'];
if (isset($proxy['auth'])) {
$this->options[CURLOPT_PROXYAUTH] = CURLAUTH_BASIC;
if (isset($proxy['auth_method']) && $proxy['auth_method'] === self::PROXY_AUTH_NTLM) {
$this->options[CURLOPT_PROXYAUTH] = CURLAUTH_NTLM;
}
$this->options[CURLOPT_PROXYUSERPWD] = $proxy['auth'];
}
}
return $this;
}
/**
* Sets the Timeout of the Request
*
* @param Integer $timeOut, by default 30
* @return Requester
*/
public function setOptionTimeOut($timeOut = 30) {
$this->options[CURLOPT_TIMEOUT] = $timeOut;
return $this;
}
/**
* Sets how many redirets will support
*
* @param Integer $max_redirects, By default 3
*
* @return Requester
*/
public function setOptionAllowRedirect($max_redirects = 3) {
if($max_redirects == FALSE || $max_redirects == 0) {
$this->options[CURLOPT_FOLLOWLOCATION] = FALSE;
$this->options[CURLOPT_MAXREDIRS] = 0;
return $this;
}
$this->options[CURLOPT_FOLLOWLOCATION] = TRUE;
$this->options[CURLOPT_MAXREDIRS] = $max_redirects;
return $this;
}
/**
* Sets the Certificate in order to Validate the Peer
*
* @param String $sslCa : Path to the CA Cert
*
* @return Requester
*/
public function setOptionSsl($sslCa) {
$this->options[CURLOPT_SSL_VERIFYPEER] = FALSE;
if ($sslCa !== '') {
$this->options[CURLOPT_SSL_VERIFYPEER] = TRUE;
$this->options[CURLOPT_SSL_VERIFYHOST] = 2;
$this->options[CURLOPT_CAINFO] = $sslCa;
}
return $this;
}
/**
* Sets auth for the Requests
*
* @param String $usernameAndPassword : username:password
* @param String $method : Any Curl Option valid for CURLOPT_HTTPAUTH, by def BASIC.
*
* @todo Test this method
*/
public function setOptionHttpAuth($usernameAndPassword, $method = self::AUTH_BASIC) {
$this->options[CURLOPT_HTTPAUTH] = $method;
$this->options[CURLOPT_USERPWD] = $usernameAndPassword;
}
/**
* Sets the Encoding
*
* @param String $encodig : "identity", "deflate", and "gzip"
*
* @return Requester
*
* @todo Test this Method
*/
protected function setOptionEncoding($encoding = '') {
$this->options[CURLOPT_ENCODING] = $encoding;
return $this;
}
/**
* Sets the Url to Hit
* @param String : $url
* @return Requester
*/
protected function setOptionUrl($url) {
$this->options[CURLOPT_URL] = $url;
return $this;
}
/**
* Sets params and Appends to the Url as Query string
*
* @param Array $params
*
* @return Requester
*/
protected function setOptionParams($params) {
if(!empty($params)) {
$query_params = $this->buildQuery($params);
$this->options[CURLOPT_URL] .= '?' . $query_params;
}
return $this;
}
/**
* Sets Payload for POST requests
*
* @param Mixed (Array or String) $data
* @return Requester
*/
protected function setOptionData($data) {
if (!empty($data)) {
if ($this->method === self::DELETE && is_array($data)) {
//DELETE needs the post data as string.
$data = $this->buildQuery($data);
}
$this->options[CURLOPT_POSTFIELDS] = $data;
}
return $this;
}
/**
* Sets the Request HTTP Method
*
* @param String $method : The Method, by default GET
*
* @return Requester
*/
protected function setOptionMethod($method = self::GET) {
$this->options[CURLOPT_NOBODY] = FALSE;
$this->method = $method;
switch ($this->method) {
case self::GET:
$this->options[CURLOPT_POST] = FALSE;
break;
case self::POST:
$this->options[CURLOPT_POST] = TRUE;
break;
case self::HEAD:
$this->options[CURLOPT_HEADER] = TRUE;
$this->options[CURLOPT_NOBODY] = TRUE;
break;
default:
$this->options[CURLOPT_CUSTOMREQUEST] = $this->method;
}
return $this;
}
/**
* This options allows set the response type. By default will return a string
* with the content. But if you need more info, you can set the response to an
* array and get a complete information of the request.
*
* @param String $type RESPONSE_RAW | RESPONSE_ARRAY
*
*/
public function setOptionResponseType($type = self::RESPONSE_RAW) {
$this->responseType = $type;
if($type === self::RESPONSE_ARRAY) {
$this->options[CURLOPT_HEADER] = TRUE;
} else {
$this->options[CURLOPT_HEADER] = FALSE;
}
return $this;
}
/**
* Set this option to throw an Exception if the response status code is
* grater or equal than 400. By dafault is false.
*
* @param boolean $fail
*/
public function setOptionFailOnError($fail = FALSE) {
$this->options[CURLOPT_FAILONERROR] = $fail;
return $this;
}
/**
* Resets Requester Options
*
* @param Array $options Posible entries
* timeout : Time in seconds to wait for the request, Default 30
* max_redirects : Numeric, default 3, 0 for don't allow redirects
* proxy : Array (url, auth, auth_method). Default None
* encoding : String, The encoding type to pass to curl, Default ''
* ssl_ca : Sets the Path to the CA for SSL
* response_type : RESPONSE_RAW or RESPONSE_ARRAY
* Sets the response type, the raw response or an array with details.
*
* @todo Test this method
*/
public function resetOptions($options = array()) {
$this->options = self::$default_options;
if (isset($options['timeout']) && is_numeric($options['timeout'])) {
$this->setOptionTimeOut($options['timeout']);
}
if (isset($options['max_redirects']) && is_numeric($options['max_redirects'])) {
$this->setOptionAllowRedirect((int) $options['max_redirects']);
}
if (isset($options['proxy']) && is_array($options['proxy'])) {
$proxy = $options['proxy'];
}
if(isset($options['ssl_ca'])) {
$this->setOptionSsl($options['ssl_ca']);
}
if(isset($options['proxy'])) {
$this->setOptionProxy($options['proxy']);
}
if (isset($options['encoding'])) {
$this->setOptionEncoding($options['encoding']);
}
if(isset($options['response_type']) && $options['response_type'] === self::RESPONSE_ARRAY) {
$this->setOptionResponseType(self::RESPONSE_ARRAY);
}
if (isset($options['fail_on_error'])) {
$this->setOptionFailOnError($options['fail_on_error']);
}
return $this;
}
/**
* Parses a raw HTTP Response Header and convert it to an array.
*
* @param String $headers
*
* @return Array : The Parsed Headers
*/
static function parseHttpHeader($headers) {
$headerdata = array();
if ($headers === FALSE){
return $headerdata;
}
$headers_lines = str_replace("\r","", $headers);
$headers_lines = explode("\n", $headers_lines);
foreach($headers_lines as $value){
$header = explode(": ", $value);
if(count($header) == 1){
$headerdata['status'] = $header[0];
} elseif(count($header) == 2){
$headerdata[$header[0]] = $header[1];
}
}
return $headerdata;
}
/**
* Creates a Response Array
*
* @param String $content : Raw Response (Headers and Body)
*
* @param Array $info : Curl Meta Info
*/
static function createArrayResponse($content, $info = array()) {
$response = array();
foreach($info as $key => $value) {
$response[$key] = $value;
}
$response['raw_header'] = trim(substr($content, 0, $info['header_size']));
$response['headers'] = self::parseHttpHeader($response['raw_header']);
$response['content'] = substr($content, $info['header_size']);
$response['allow'] = array();
if (isset($response['headers']['Allow'])) {
$response['allow'] = explode(', ', $response['headers']['Allow']);
}
return $response;
}
/**
* Returns the HTTP code of the last Request.
*
* If any request was made before, this method will return 0
*
* @return integer
*/
public function getLastHttpCode() {
return $this->lastHttpCode;
}
}