-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFivemServerStatus.php
56 lines (44 loc) · 1.53 KB
/
FivemServerStatus.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
<?php
namespace EpEren\FivemServerStatus;
class FivemServerStatus
{
private $baseUrl = "https://servers-frontend.fivem.net/api/servers/single";
private $httpContext;
public function __construct()
{
$this->httpContext = stream_context_create([
'http' => [
'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 OPR/101.0.0.0\r\n" .
"Referer: https://servers.fivem.net/\r\n" .
"Origin: https://servers.fivem.net\r\n"
]
]);
}
public function Get($Code)
{
$url = $this->baseUrl . "/" . $Code;
$content = file_get_contents($url, false, $this->httpContext);
if ($content === false) {
throw new \Exception("Can't fetch fivem API.");
}
$result = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception("JSON decoding error: " . json_last_error_msg());
}
return $result['Data'];
}
public function IsOnline($Code)
{
$server = $this->Get($Code);
$endpoint = $server['connectEndPoints'][0] ?? null;
if ($endpoint === null) {
return false;
}
$url = "http://" . $endpoint . "/info.json";
$content = file_get_contents($url, false, $this->httpContext);
if ($content === false) {
return false;
}
return true;
}
}