Repository navigation
Expand file tree
/
Copy pathapi-proxy.php
More file actions
63 lines (54 loc) · 2.01 KB
/
Copy pathapi-proxy.php
File metadata and controls
63 lines (54 loc) · 2.01 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
<?php
// Proxy do API ERLC v2 - dodaje nagłówek server-key
// Użycie: api-proxy.php?endpoint=/players (GET) lub api-proxy.php?endpoint=/command (POST)
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
$endpoint = isset($_GET['endpoint']) ? $_GET['endpoint'] : '/players';
$api_domain = getenv('ERLC_API_DOMAIN') ?: 'https://api.erlc.gg';
$api_key = getenv('ERLC_API_KEY') ?: 'BcULYXDaOFOAqsyfuFiG-sfBaApJjJzPHZYBGxCrzPRgZtQskMryBTKxdJNtI';
// Dla /players pobieramy z v2 z Players=true, dla innych endpointów v1
if ($endpoint === '/players' || $endpoint === '') {
$url = "$api_domain/v2/server?Players=true";
} elseif ($endpoint === '/command') {
$url = "$api_domain/v2/server/command";
} else {
$url = "$api_domain/v1/server" . $endpoint;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Server-Key: $api_key",
"Accept: application/json"
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Obsługa POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
$post_data = file_get_contents('php://input');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Server-Key: $api_key",
"Content-Type: application/json",
"Accept: application/json"
));
}
$response = curl_exec($ch);
if ($response === false) {
$curl_error = curl_error($ch);
curl_close($ch);
http_response_code(502);
echo json_encode(["error" => "Proxy request failed", "details" => $curl_error]);
exit;
}
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
http_response_code($http_code);
echo $response;
?>