-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexchange-rates.php
More file actions
46 lines (36 loc) · 1.29 KB
/
Copy pathexchange-rates.php
File metadata and controls
46 lines (36 loc) · 1.29 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
<?php
declare(strict_types=1);
require __DIR__ . '/app/bootstrap.php';
if (!requestMethod('GET')) {
jsonResponse(['error' => 'Method not allowed'], 405);
}
$base = strtoupper(trim($_GET['base'] ?? 'USD'));
if (!preg_match('/^[A-Z]{3}$/', $base)) {
jsonResponse(['error' => 'Invalid base currency'], 400);
}
$url = 'https://open.er-api.com/v6/latest/' . rawurlencode($base);
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 10,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => ['Accept: application/json'],
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($body === false || $status < 200 || $status >= 300) {
error_log('Exchange-rate provider error: ' . $error);
jsonResponse(['error' => 'Exchange-rate service unavailable'], 502);
}
$data = json_decode($body, true);
if (!is_array($data) || ($data['result'] ?? '') !== 'success' || !isset($data['rates'])) {
jsonResponse(['error' => 'Invalid exchange-rate response'], 502);
}
header('Cache-Control: public, max-age=300, stale-while-revalidate=60');
jsonResponse([
'base_code' => $data['base_code'] ?? $base,
'conversion_rates' => $data['rates'],
]);