Как получить информацию из валюты YQL?

Я пробовал много сценариев из интернета, но у меня все работают, так что, может, вы мне поможете, я не знаю, как создать PHP-код, чтобы получить курс евро к датской кроне. Мне нужен код примерно так:

$dkk_rate = ???
$euros = 100;
$krones = $euros * $dkk_rate;

1

Решение

Один из простых подходов — загрузить последнюю версию с Yahoo Finance. Например:

<?php
$x = file_get_contents("http://download.finance.yahoo.com/d/quotes.csv?s=EURDKK=X&f=sl1d1t1ba&e=.json");
$x=explode(",",$x);
echo "FX rate of EURDKK is ".$x[1]." at ".$x[2];
?>

Вы можете заключить это в функцию следующим образом:

<?php
function convertCurrency($from,$to,$amount) {
$x = file_get_contents("http://download.finance.yahoo.com/d/quotes.csv?s=$from$to=X&f=sl1d1t1ba&e=.json");
$x=explode(",",$x);
echo "$amount of $from is equal to ".($amount*$x[1])." $to";
}

convertCurrency("EUR","DKK",100);
?>

который выведет: 100 of EUR is equal to 745.33 DKK

Надеюсь это поможет.

1

Другие решения

Из PHP-фрагментов

function currency($from_Currency,$to_Currency,$amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$data = explode('"', $rawdata);
$data = explode(' ', $data['3']);
$var = $data['0'];
return round($var,2);
}

Вы не обязательно нуждаетесь в CURL для этого все же. file_get_contents должен делать то же самое, если включен allow_furl_open:

$result = file_get_contents(
'http://www.google.com/ig/calculator?hl=en&q=100EUR=?USD'
);

Это вернуло бы что-то вроде

{lhs: "100 Euros",rhs: "129.18 U.S. dollars",error: "",icc: true}
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector