v2.5.2
Giriş yap

DOM hatası! http://www.tcmb.gov.tr/kurlar/today.xml çalışmıyor.

4rival
1,251 defa görüntülendi

Helper Fonksiyonum aşşağıda ki gibidir.
Çözümü Bilen duyan var mı ?

<?php
   function USDtoTRY($cost){
    $oneYear = 60*60*24*365;
    $urls = array();
    $SiteMapURL = "http://www.tcmb.gov.tr/kurlar/today.xml";
    $DomDocument = new DOMDocument();
    $DomDocument->preserveWhiteSpace = false;
    $DomDocument->load($SiteMapURL);
    $DomNodeList = $DomDocument->getElementsByTagName('Isim');
    $DomNodeForexBuying = $DomDocument->getElementsByTagName('ForexBuying');
    $DomNodeForexSelling = $DomDocument->getElementsByTagName('ForexSelling');

    foreach($DomNodeList as $url) {
        $urls[] = $url->nodeValue;
    }

    foreach($DomNodeForexBuying as $buying) {
        $usd1[] = $buying->nodeValue;
    }

    foreach($DomNodeForexSelling as $selling) {
        $usd2[] = $selling->nodeValue;
    }
    $try = $usd2[0] * $cost;
    return $try;

}
?>

Hata Çıktısı ise

Cevap yaz
Cevaplar (4)
tayfunerbilen
1290 gün önce

şöyle çalıştırdım ben, belki işine yarar :)

function USDtoTRY($cost)
{
	$ch = curl_init('https://www.tcmb.gov.tr/kurlar/today.xml');
	curl_setopt_array($ch, [
		CURLOPT_RETURNTRANSFER => true
	]);
	$output = curl_exec($ch);
	curl_close($ch);

	$xml = new SimpleXMLElement($output);
	$usd = $xml->children()[0]->ForexSelling;
	return number_format($cost / $usd, 2) . '$';
}

echo USDtoTRY(100);
4rival
1290 gün önce

Sunucu ile alakalı bir problemmiş sanırım. Network hatası falan dediler. Sanırım tüm kodlar doğru...

4rival
1290 gün önce
 $result = curl_exec($ch);

bu kısım false dönüyor

frdgk
1290 gün önce

Senin fonksiyonunla alakası yok ama tcmb bildiğim kadarıyla bir gün öncesinin kurlarını gösteriyor. Aşağıda ki örnekte anlık olarak alabilirsin, böylesinin senin için daha verimli olacağını düşündüm.


function USDtoTRY($cost){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://kur.doviz.com/api/v5/converterItems');
  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER ['HTTP_USER_AGENT']);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  $json = json_decode($result, true);

  foreach($json[0] as $row){
    if($row['code'] == 'USD'){
      return $row['buying'] * $cost;
    }
  }
}

$TL = USDtoTRY(10);