-
Notifications
You must be signed in to change notification settings - Fork 6
/
requests.php
86 lines (65 loc) · 2.12 KB
/
requests.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
<?php
function request($method, $url, $params = [], $headers = [], $body = []) {
// placeholder for curl request response headers
$response_headers = [];
if(count($params)) {
$url = $url . '?' . http_build_query($params);
}
// get cURL resource
$ch = curl_init();
if(strpos($url, 'https://') === false) {
$url = DOMAIN_NAME . $url;
}
// set url
curl_setopt($ch, CURLOPT_URL, $url);
if( defined('SOCKS5') ) {
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
curl_setopt($ch, CURLOPT_PROXY, SOCKS5);
}
// set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HEADER, 1);
// this function is called by curl for each header received
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function($curl, $header) use (&$response_headers)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$response_headers[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
}
);
// return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// we should set the body to json for put/post requests
if(in_array($method, ["PUT", "POST"]) && count($body)) {
$body = json_encode($body);
// set body
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$headers[] = 'Content-Type: application/json';
}
// set request headers
if(count($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
// send the request and save response to $response
$response = curl_exec($ch);
// stop if fails
if (!$response) {
throw new Exception('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$response_body = substr($response, $header_size);
// close curl resource to free up system resources
curl_close($ch);
return [
'status' => $status_code,
'headers' => $response_headers,
'response' => json_decode($response_body, true),
'raw_response' => $response,
];
}