-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
86 lines (69 loc) · 1.88 KB
/
common.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
declare(strict_types=1);
const STATUS_SUCCESS = 0;
const STATUS_FAILURE = 1;
const VERSIONS_FILE = 'versions.json';
function exit_cli(string $message = "", int $status = STATUS_FAILURE): void
{
$stream = STDOUT;
if ($status > STATUS_SUCCESS) {
$stream = STDERR;
}
fprintf($stream, $message);
exit($status);
}
function logError(string $format, mixed ...$values): void
{
fprintf(STDERR, "Error: %s\n", sprintf($format, ...$values));
}
function request(string $url, array $query = []): Response
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, sprintf("%s?%s", $url, http_build_query($query)));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$error = curl_error($curl);
$errno = curl_errno($curl);
curl_close($curl);
$body = null;
if (!empty($data) && json_validate($data)) {
$body = json_decode($data, true);
}
return new Response($errno, $error, $status, $body);
}
class Response
{
public function __construct(
private readonly int $curlErrno,
private readonly string $curlError,
private readonly int $status,
private readonly ?array $body = null,
) {}
public function getCurlErrno(): int
{
return $this->curlErrno;
}
public function getCurlError(): string
{
return $this->curlError;
}
public function getStatus(): int
{
return $this->status;
}
public function getBody(): ?array
{
return $this->body;
}
}
if (!function_exists('json_validate')) {
/**
* Polyfill for json_validate function.
*/
function json_validate(string $json, int $depth = 512, int $flags = 0): bool
{
json_decode($json, null, $depth, $flags);
return \JSON_ERROR_NONE === json_last_error();
}
}