-
Notifications
You must be signed in to change notification settings - Fork 0
/
Requests.php
113 lines (90 loc) · 2.81 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
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
<?php
declare(strict_types=1);
class RequestsCurlRequestFailedException extends Exception {}
class RequestsInvalidJSONException extends Exception {}
class Requests
{
public static function request(string $method, string $url, ?array $data = null, array $headers = [])
{
$curl = curl_init();
switch ($method)
{
case 'GET':
if (!is_null($data))
{
$url .= '?' . http_build_query($data);
}
break;
case 'POST':
{
curl_setopt($curl, CURLOPT_POST, 1);
if (!is_null($data))
{
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
}
break;
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array_merge($headers, [
'User-Agent: Lamia Assignment Thingy',
])
);
$response = curl_exec($curl);
if ($response === false)
{
throw new RequestsCurlRequestFailedException("cURL request failed: " . curl_error($curl));
}
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$effective_url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
// Extract headers formatted as array rows
$response_headers = trim(substr($response, 0, $header_size));
$response_headers = explode("\r\n\r\n", $response_headers);
foreach ($response_headers as &$headers)
{
$headers = explode("\r\n", $headers);
}
// Extract response content
$response_body = substr($response, $header_size);
curl_close($curl);
return [
'status_code' => $status_code,
'effective_url' => $effective_url,
'headers' => $response_headers,
'content' => $response_body,
];
}
public static function get(string $url, ?array $data = null, array $headers = [])
{
return Requests::request('GET', $url, $data, $headers);
}
public static function get_json(string $url, ?array $data = null, array $headers = [])
{
$response = Requests::get($url, $data, $headers);
$response['content'] = json_decode($response['content'], true);
if (is_null($response['content']))
{
throw new RequestsInvalidJSONException("Request response is invalid JSON, parsing failed.");
}
return $response;
}
public static function post(string $url, ?array $data = null, array $headers = [])
{
return Requests::request('POST', $url, $data, $headers);
}
public static function post_json(string $url, ?array $data = null, array $headers = [])
{
$response = Requests::post($url, $data, $headers);
$response['content'] = json_decode($response['content'], true);
if (is_null($response['content']))
{
throw new RequestsInvalidJSONException("Request response is invalid JSON, parsing failed.");
}
return $response;
}
}