1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* HTTP |
5
|
|
|
* |
6
|
|
|
* cURL proxy. |
7
|
|
|
* |
8
|
|
|
* @package core |
9
|
|
|
* @author [email protected] |
10
|
|
|
* @copyright Caffeina srl - 2016 - http://caffeina.com |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class HTTP { |
14
|
|
|
use Module, Events; |
15
|
|
|
|
16
|
|
|
protected static $UA = "Mozilla/4.0 (compatible; Core::HTTP; Windows NT 6.1)", |
|
|
|
|
17
|
|
|
$json_data = false, |
18
|
|
|
$headers = [], |
19
|
|
|
$last_info = null, |
20
|
|
|
$proxy = null; // host:port |
21
|
|
|
|
22
|
|
|
protected static function request($method, $url, $data=[], array $headers=[], $data_as_json=false, $username=null, $password = null){ |
23
|
|
|
$http_method = strtoupper($method); |
24
|
|
|
$ch = curl_init($url); |
25
|
|
|
$opt = [ |
26
|
|
|
CURLOPT_CUSTOMREQUEST => $http_method, |
27
|
|
|
CURLOPT_SSL_VERIFYHOST => false, |
28
|
|
|
CURLOPT_CONNECTTIMEOUT => 10, |
29
|
|
|
CURLOPT_RETURNTRANSFER => true, |
30
|
|
|
CURLOPT_USERAGENT => static::$UA, |
31
|
|
|
CURLOPT_HEADER => false, |
32
|
|
|
CURLOPT_MAXREDIRS => 10, |
33
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
34
|
|
|
CURLOPT_ENCODING => '', |
35
|
|
|
CURLOPT_PROXY => static::$proxy, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
if($username && $password) { |
39
|
|
|
$opt[CURLOPT_USERPWD] = "$username:$password"; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$headers = array_merge($headers,static::$headers); |
43
|
|
|
|
44
|
|
|
if($http_method == 'GET'){ |
45
|
|
|
if($data && is_array($data)){ |
46
|
|
|
$tmp = []; |
47
|
|
|
$queried_url = $url; |
48
|
|
|
foreach($data as $key=>$val) $tmp[] = $key.'='.$val; |
49
|
|
|
$queried_url .= (strpos($queried_url,'?') === false) ? '?' : '&'; |
50
|
|
|
$queried_url .= implode('&',$tmp); |
51
|
|
|
$opt[CURLOPT_URL] = $queried_url; |
52
|
|
|
$opt[CURLOPT_HTTPGET] = true; |
53
|
|
|
unset($opt[CURLOPT_CUSTOMREQUEST]); |
54
|
|
|
} |
55
|
|
|
} else { |
56
|
|
|
$opt[CURLOPT_CUSTOMREQUEST] = $http_method; |
57
|
|
|
if($data_as_json or is_object($data)){ |
|
|
|
|
58
|
|
|
$headers['Content-Type'] = 'application/json'; |
59
|
|
|
$opt[CURLOPT_POSTFIELDS] = json_encode($data); |
60
|
|
|
} else { |
61
|
|
|
$opt[CURLOPT_POSTFIELDS] = http_build_query($data); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
curl_setopt_array($ch,$opt); |
66
|
|
|
$_harr = []; |
67
|
|
|
foreach($headers as $key=>$val) $_harr[] = $key.': '.$val; |
68
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $_harr); |
69
|
|
|
$result = curl_exec($ch); |
70
|
|
|
$contentType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE)); |
71
|
|
|
static::$last_info = curl_getinfo($ch); |
72
|
|
|
if(false !== strpos($contentType,'json')) $result = json_decode($result); |
73
|
|
|
curl_close($ch); |
74
|
|
|
static::trigger("request", $result, static::$last_info); |
75
|
|
|
return $result; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function useJSON($value=null){ |
79
|
|
|
return $value===null ? static::$json_data : static::$json_data = $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function addHeader($name,$value){ |
83
|
|
|
static::$headers[$name] = $value; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public static function removeHeader($name){ |
87
|
|
|
unset(static::$headers[$name]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function headers($name=null){ |
91
|
|
|
// null === $name ?? static::$headers ?? static::$headers[$name] |
92
|
|
|
return null === $name |
93
|
|
|
? static::$headers |
94
|
|
|
: ( isset(static::$headers[$name]) ? static::$headers[$name] : '' ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public static function userAgent($value=null){ |
98
|
|
|
return $value===null ? static::$UA : static::$UA = $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public static function proxy($proxy=false){ |
|
|
|
|
102
|
|
|
return $value===false ? static::$proxy : static::$proxy = $value; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function get($url, $data=null, array $headers=[], $username = null, $password = null){ |
106
|
|
|
return static::request('get',$url,$data,$headers,false,$username,$password); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public static function post($url, $data=null, array $headers=[], $username = null, $password = null){ |
110
|
|
|
return static::request('post',$url,$data,$headers,static::$json_data,$username,$password); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function put($url, $data=null, array $headers=[], $username = null, $password = null){ |
114
|
|
|
return static::request('put',$url,$data,$headers,static::$json_data,$username,$password); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public static function delete($url, $data=null, array $headers=[], $username = null, $password = null){ |
118
|
|
|
return static::request('delete',$url,$data,$headers,static::$json_data,$username,$password); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public static function info($url = null){ |
122
|
|
|
if ($url){ |
123
|
|
|
curl_setopt_array($ch = curl_init($url), [ |
124
|
|
|
CURLOPT_SSL_VERIFYHOST => false, |
125
|
|
|
CURLOPT_CONNECTTIMEOUT => 10, |
126
|
|
|
CURLOPT_RETURNTRANSFER => true, |
127
|
|
|
CURLOPT_USERAGENT => static::$UA, |
128
|
|
|
CURLOPT_HEADER => false, |
129
|
|
|
CURLOPT_ENCODING => '', |
130
|
|
|
CURLOPT_FILETIME => true, |
131
|
|
|
CURLOPT_NOBODY => true, |
132
|
|
|
CURLOPT_PROXY => static::$proxy, |
133
|
|
|
]); |
134
|
|
|
curl_exec($ch); |
135
|
|
|
$info = curl_getinfo($ch); |
136
|
|
|
curl_close($ch); |
137
|
|
|
return $info; |
138
|
|
|
} else { |
139
|
|
|
return static::$last_info; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
class HTTP_Request { |
146
|
|
|
public $method = 'GET', |
|
|
|
|
147
|
|
|
$url = null, |
148
|
|
|
$headers = [], |
149
|
|
|
$body = ''; |
150
|
|
|
|
151
|
|
|
public function __construct($method, $url, $headers=[], $data=null){ |
|
|
|
|
152
|
|
|
$this->method = strtoupper($method); |
153
|
|
|
$this->url = new URL($this->url); |
154
|
|
|
$this->headers = (array)$headers; |
155
|
|
|
if ($data) { |
156
|
|
|
if (isset($this->headers["Content-Type"]) && $this->headers["Content-Type"]=='application/json') |
157
|
|
|
$this->body = json_encode($data); |
158
|
|
|
else |
159
|
|
|
$this->body = http_build_query($data); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function __toString(){ |
164
|
|
|
return "$this->method {$this->url->path}{$this->url->query} HTTP/1.1\r\n" |
|
|
|
|
165
|
|
|
."Host: {$this->url->host}\r\n" |
|
|
|
|
166
|
|
|
.($this->headers ? implode("\r\n",$this->headers) . "\r\n" : '') |
167
|
|
|
."\r\n{$this->body}"; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
class HTTP_Response { |
173
|
|
|
public $status = 200, |
|
|
|
|
174
|
|
|
$headers = [], |
175
|
|
|
$contents = ''; |
176
|
|
|
|
177
|
|
|
public function __construct($contents, $status, $headers){ |
178
|
|
|
$this->status = $status; |
179
|
|
|
$this->contents = $contents; |
180
|
|
|
$this->headers = (array)$headers; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function __toString(){ |
184
|
|
|
return $this->contents; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.