-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-prices.php
43 lines (35 loc) · 1013 Bytes
/
convert-prices.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
<?php
// Get the currency code from the query string
$currency = $_GET['currency'];
// Set up the API endpoint and parameters
$endpoint = 'https://openexchangerates.org/api/latest.json';
$params = array(
'app_id' => 'APP_ID_HERE',
'base' => 'USD', // Set the base currency for conversion
);
// Send request
$url = $endpoint . '?' . http_build_query($params);
$response = file_get_contents($url);
// Parse response
if ($response !== false) {
$data = json_decode($response, true);
// Exchange rates are in the 'rates' field of the JSON data
$exchange_rates = $data['rates'];
} else {
// Handle an error
http_response_code(500);
echo 'Error fetching exchange rates';
exit;
}
// TODO: Convert prices to the requested currency using the exchange rate
// ...
// Return the converted prices as a JSON response
header('Content-Type: application/json');
echo json_encode($converted_prices);
// Should look like:
// {
// "plan1": "$400.77",
// "plan2": "$727.20",
// "plan3": "$968.28"
// }
?>