feat: add apilayer as provider for fixer api (#127)
feat: add apilayer as provider for fixer api feat: update exchange rate when saving api key
This commit is contained in:
@@ -2,23 +2,28 @@
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
|
||||
$shouldUpdate = true;
|
||||
$query = "SELECT date FROM last_exchange_update";
|
||||
$result = $db->querySingle($query);
|
||||
|
||||
if ($result) {
|
||||
$lastUpdateDate = new DateTime($result);
|
||||
$currentDate = new DateTime();
|
||||
$lastUpdateDateString = $lastUpdateDate->format('Y-m-d');
|
||||
$currentDateString = $currentDate->format('Y-m-d');
|
||||
$shouldUpdate = $lastUpdateDateString < $currentDateString;
|
||||
if (isset($_GET['force']) && $_GET['force'] === "true") {
|
||||
$shouldUpdate = true;
|
||||
} else {
|
||||
$query = "SELECT date FROM last_exchange_update";
|
||||
$result = $db->querySingle($query);
|
||||
|
||||
if ($result) {
|
||||
$lastUpdateDate = new DateTime($result);
|
||||
$currentDate = new DateTime();
|
||||
$lastUpdateDateString = $lastUpdateDate->format('Y-m-d');
|
||||
$currentDateString = $currentDate->format('Y-m-d');
|
||||
$shouldUpdate = $lastUpdateDateString < $currentDateString;
|
||||
}
|
||||
|
||||
if (!$shouldUpdate) {
|
||||
echo "Rates are current, no need to update.";
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$shouldUpdate) {
|
||||
echo "Rates are current, no need to update.";
|
||||
exit;
|
||||
}
|
||||
|
||||
$query = "SELECT api_key FROM fixer";
|
||||
$query = "SELECT api_key, provider FROM fixer";
|
||||
$result = $db->query($query);
|
||||
|
||||
if ($result) {
|
||||
@@ -26,6 +31,7 @@ if ($result) {
|
||||
|
||||
if ($row) {
|
||||
$apiKey = $row['api_key'];
|
||||
$provider = $row['provider'];
|
||||
|
||||
$codes = "";
|
||||
$query = "SELECT id, name, symbol, code FROM currencies";
|
||||
@@ -41,8 +47,20 @@ if ($result) {
|
||||
$mainCurrencyCode = $row['code'];
|
||||
$mainCurrencyId = $row['main_currency'];
|
||||
|
||||
$api_url = "http://data.fixer.io/api/latest?access_key=". $apiKey . "&base=EUR&symbols=" . $codes;
|
||||
$response = file_get_contents($api_url);
|
||||
if ($provider === 1) {
|
||||
$api_url = "https://api.apilayer.com/fixer/latest?base=EUR&symbols=" . $codes;
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'GET',
|
||||
'header' => 'apikey: ' . $apiKey,
|
||||
]
|
||||
]);
|
||||
$response = file_get_contents($api_url, false, $context);
|
||||
} else {
|
||||
$api_url = "http://data.fixer.io/api/latest?access_key=". $apiKey . "&base=EUR&symbols=" . $codes;
|
||||
$response = file_get_contents($api_url);
|
||||
}
|
||||
|
||||
$apiData = json_decode($response, true);
|
||||
|
||||
$mainCurrencyToEUR = $apiData['rates'][$mainCurrencyCode];
|
||||
|
||||
Reference in New Issue
Block a user