-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.php
More file actions
337 lines (278 loc) · 7.71 KB
/
requests.php
File metadata and controls
337 lines (278 loc) · 7.71 KB
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
<?php
class requests
{
public $config = null;
public $defaultConfig = array(
'allow_redirects' => true,
'auth' => array(),
'base_headers' => '',
'base_url' => '',
'cookies' => false,
'max_redirects' => 30,
'timeout' => 0,
'verify' => false,
'safe_mode' => false,
);
public function __construct($url = null, $config = null)
{
if (!$config || !is_array($config)) {
$this->config = $this->defaultConfig;
} else {
$this->config = array_merge($this->defaultConfig, $config);
}
if ($url) {
$this->config['base_url'] = $url;
}
}
public function get($url, $config = null)
{
$this->configure($config);
return $this->request($url);
}
public function delete($url, $config = null)
{
$this->configure($config);
return $this->request($url, 'DELETE');
}
public function post($url, $data = null, $config = null)
{
$this->configure($config);
return $this->request($url, 'POST', $data);
}
public function put($url, $data = null, $config = null)
{
$this->configure($config);
return $this->request($url, 'PUT', $data);
}
private function request($url, $method = 'GET', $data = null)
{
$curl = curl_init();
$url = $this->config['base_url'] . $url;
if (!$url) {
throw new urlrequired('URL Required');
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $this->config['verify']);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->config['verify']);
if ($this->config['cookies']) {
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->config['cookies']);
curl_setopt($curl, CURLOPT_COOKIEJAR, $this->config['cookies']);
}
if ($this->config['allow_redirects']) {
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, $this->config['max_redirects']);
}
if ($this->config['timeout']) {
curl_setopt($curl, CURLOPT_TIMEOUT, $this->config['timeout']);
}
if ($this->config['auth']) {
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC) ;
curl_setopt($curl, CURLOPT_USERPWD, implode($this->config['auth'], ':'));
}
if ($data) {
if (is_array($data)) {
$data = http_build_query($data);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
if ($method === 'PUT' || $method === 'DELETE') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
if ($method === 'PUT') {
$length = 0;
if ($data) {
$length = strlen($data);
}
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: ' . $length));
}
}
if ($method === 'POST') {
curl_setopt($curl, CURLOPT_POST, true);
}
$raw = curl_exec($curl);
$info = curl_getinfo($curl);
$error = curl_error($curl);
$errno = curl_errno($curl);
if (!$this->config['safe_mode'] && $errno != CURLE_OK) {
switch ($errno) {
case CURLE_TOO_MANY_REDIRECTS:
throw new toomanyredirects('Too Many Redirects');
break;
case CURLE_COULDNT_RESOLVE_PROXY:
case CURLE_COULDNT_RESOLVE_HOST:
case CURLE_COULDNT_CONNECT:
throw new connectionerror('Connection Error');
break;
case CURLE_SSL_CONNECT_ERROR:
case CURLE_SSL_PEER_CERTIFICATE:
case CURLE_SSL_CERTPROBLEM:
case CURLE_SSL_CIPHER:
case CURLE_SSL_CACERT:
throw new sslerror('SSL Error');
break;
case CURLE_OPERATION_TIMEOUTED:
throw new timeout('Connection Timeout');
break;
CASE CURLE_HTTP_NOT_FOUND:
throw new httperror('HTTP Error');
break;
}
}
curl_close($curl);
$response = new response($this, $raw, $info, $error);
return $response;
}
private function configure($config)
{
if ($config && is_array($config)) {
$this->config = array_merge($this->defaultConfig, $config);
}
}
}
class response
{
public $content = null;
public $content_type = null;
public $cookies = null;
public $encoding = null;
public $error = null;
public $headers = null;
public $raw = null;
public $request = null;
public $status_code = null;
public $url = null;
public $info = null;
public function __construct($request, $raw, $info, $error)
{
$this->request = $request;
$this->raw = $raw;
$this->info = $info;
$this->error = $error;
$this->status_code = $info['http_code'];
$this->url = $info['url'];
$this->content = substr($this->raw, $this->info['header_size']);
$this->process_headers();
$this->process_cookies();
}
private function process_headers()
{
$header_length = $this->info['header_size'];
$headers = substr($this->raw, 0, $header_length);
$headers = str_replace("\r", "", $headers);
$headers = substr($headers, strrpos($headers, "\n\n", -3));
$headers = trim($headers);
$headers = explode("\n", $headers);
array_shift($headers);
foreach ($headers as $header) {
$header_array = explode(':', $header, 2);
if (isset($header_array[0]) && isset($header_array[1])) {
$header_array[0] = trim($header_array[0]);
$header_array[1] = trim($header_array[1]);
if (isset($this->headers[$header_array[0]])) {
if (is_array($this->headers[$header_array[0]])) {
$this->headers[$header_array[0]][] = $header_array[1];
} else {
$this->headers[$header_array[0]] = array(
$this->headers[$header_array[0]],
$header_array[1],
);
}
} else {
if (strtolower($header_array[0]) == 'content-type') {
$content_type_array = explode(';', $header_array[1]);
if (isset($content_type_array[0])) {
$this->content_type = $content_type_array[0];
}
if (isset($content_type_array[1])) {
$this->encoding = str_ireplace('charset=', '', $content_type_array[1]);
}
}
$this->headers[$header_array[0]] = $header_array[1];
}
}
}
}
public function header($label)
{
if (!$this->headers) {
return null;
}
foreach ($this->headers as $header_label => $header) {
if (strtolower($header_label) == strtolower($label)) {
return $header;
}
}
return null;
}
private function process_cookies()
{
$header_cookies = $this->header('set-cookie');
if (is_array($header_cookies)) {
$cookies = $header_cookies;
} else {
$cookies = array($header_cookies);
}
foreach ($cookies as $cookie) {
$cookie_array = explode(';', $cookie);
$cookie_data = array();
foreach ($cookie_array as $cookie_element) {
$cookie_info = explode('=', $cookie_element, 2);
if (count($cookie_info) == 2) {
$cookie_info[0] = trim($cookie_info[0]);
$cookie_info[1] = trim($cookie_info[1]);
if (in_array(strtolower($cookie_info[0]), array('domain', 'expires', 'path', 'secure', 'comment'))) {
$cookie_data[$cookie_info[0]] = $cookie_info[1];
} else {
$cookie_data['name'] = $cookie_info[0];
$cookie_data['value'] = $cookie_info[1];
}
}
}
if ($cookie_data) {
$this->cookies[] = $cookie_data;
}
}
if ($this->headers) {
foreach ($this->headers as $header_label => $header) {
if (strtolower($header_label) == 'set-cookie') {
unset($this->headers[$header_label]);
}
}
}
}
public function cookie($label)
{
if (!$this->cookies) {
return null;
}
foreach ($this->cookies as $cookie) {
if (strtolower($cookie['name']) == strtolower($label)) {
return $cookie;
}
}
return null;
}
}
class requestexception extends Exception
{
}
class httperror extends requestexception
{
}
class timeout extends requestexception
{
}
class toomanyredirects extends requestexception
{
}
class urlrequired extends requestexception
{
}
class sslerror extends requestexception
{
}
class connectionerror extends requestexception
{
}