heraishere
Centipat
- Katılım
- 11 Eylül 2022
- Mesajlar
- 759
- Çözümler
- 7
Daha fazla
- Cinsiyet
- Erkek
Merhaba, API ile sitede https://xxx/admin/language/tr-tr/localisation/update_currencies.php uzantısına girince otomatik kur güncellemek için bir kod yazdım ancak veri tabanı bağlantısı ve API çalışmasına rağmen SQL'deki kuru güncellemiyor. Bu konu hakkında yardım eder misiniz?
Kod:
Kod:
PHP:
<?php
// Veritabanı bağlantısı
$db_host = 'localhost';
$db_user = 'test';
$db_pass = 'test';
$db_name = 'test';
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {
die("Veritabanı bağlantı hatası: " . $conn->connect_error);
}
$fromCurrency = 'TRY';
$toCurrency = 'USD';
function getExchangeRate($fromCurrency, $toCurrency) {
// Döviz kuru servisi API URL
$apiUrl = "https://api.exchangerate-api.com/v4/latest/" . $fromCurrency;
// API'den veriyi al
$response = file_get_contents($apiUrl);
if ($response) {
$data = json_decode($response, true);
// Hedef döviz kuru
$exchangeRate = $data['rates'][$toCurrency];
return $exchangeRate;
} else {
return false;
}
}
$exchangeRate = getExchangeRate($fromCurrency, $toCurrency);
if ($exchangeRate !== false) {
// Veritabanında döviz kurlarını güncelle
$usdSql = "UPDATE " . DB_PREFIX . "currency SET value = " . (float)$exchangeRate . " WHERE code = 'USD'";
$trySql = "UPDATE " . DB_PREFIX . "currency SET value = 1 WHERE code = 'TRY'";
$conn->query($usdSql);
$conn->query($trySql);
echo "Dolar kuru güncellendi: 1 USD = " . $exchangeRate . " TRY";
} else {
echo "Döviz kuru çekme hatası!";
}
$conn->close();
?>