1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* HTTP\Driver |
5
|
|
|
* |
6
|
|
|
* Core\HTTP\Driver\CURL cURL HTTP Driver. |
7
|
|
|
* |
8
|
|
|
* @package core |
9
|
|
|
* @author [email protected] |
10
|
|
|
* @copyright Caffeina srl - 2015-2016 - http://caffeina.com |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace HTTP; |
14
|
|
|
|
15
|
|
|
class CURL implements Driver\Adapter { |
16
|
|
|
|
17
|
|
|
public function request(HTTP\Request $request){ |
18
|
|
|
$http_method = strtoupper($method); |
|
|
|
|
19
|
|
|
$ch = curl_init($url); |
|
|
|
|
20
|
|
|
$opt = [ |
21
|
|
|
CURLOPT_CUSTOMREQUEST => $http_method, |
22
|
|
|
CURLOPT_SSL_VERIFYHOST => false, |
23
|
|
|
CURLOPT_CONNECTTIMEOUT => 10, |
24
|
|
|
CURLOPT_RETURNTRANSFER => true, |
25
|
|
|
CURLOPT_USERAGENT => static::$UA, |
26
|
|
|
CURLOPT_HEADER => false, |
27
|
|
|
CURLOPT_MAXREDIRS => 10, |
28
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
29
|
|
|
CURLOPT_ENCODING => '', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
if($username && $password) { |
33
|
|
|
$opt[CURLOPT_USERPWD] = "$username:$password"; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$headers = array_merge($headers,static::$headers); |
|
|
|
|
37
|
|
|
|
38
|
|
View Code Duplication |
if($http_method == 'GET'){ |
39
|
|
|
if($data && is_array($data)){ |
40
|
|
|
$tmp = []; |
41
|
|
|
$queried_url = $url; |
42
|
|
|
foreach($data as $key=>$val) $tmp[] = $key.'='.$val; |
|
|
|
|
43
|
|
|
$queried_url .= (strpos($queried_url,'?') === false) ? '?' : '&'; |
44
|
|
|
$queried_url .= implode('&',$tmp); |
45
|
|
|
$opt[CURLOPT_URL] = $queried_url; |
46
|
|
|
$opt[CURLOPT_HTTPGET] = true; |
47
|
|
|
unset($opt[CURLOPT_CUSTOMREQUEST]); |
48
|
|
|
} |
49
|
|
|
} else { |
50
|
|
|
$opt[CURLOPT_CUSTOMREQUEST] = $http_method; |
51
|
|
|
if($data_as_json or is_object($data)){ |
|
|
|
|
52
|
|
|
$headers['Content-Type'] = 'application/json'; |
53
|
|
|
$opt[CURLOPT_POSTFIELDS] = json_encode($data); |
54
|
|
|
} else { |
55
|
|
|
$opt[CURLOPT_POSTFIELDS] = http_build_query($data); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
curl_setopt_array($ch,$opt); |
60
|
|
|
$_harr = []; |
61
|
|
|
foreach($headers as $key=>$val) $_harr[] = $key.': '.$val; |
62
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $_harr); |
63
|
|
|
$result = curl_exec($ch); |
64
|
|
|
$contentType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE)); |
65
|
|
|
static::$last_info = curl_getinfo($ch); |
66
|
|
|
if(false !== strpos($contentType,'json')) $result = json_decode($result); |
67
|
|
|
curl_close($ch); |
68
|
|
|
return $result; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function valid(){ |
72
|
|
|
return function_exists('curl_init'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.