如何从YQL货币获取信息?

时间:2015-03-23 10:51:10

标签: php currency yql

我尝试了很多来自互联网的脚本,但是有人为我工作,所以mabie你可以帮助我,我不知道如何创建PHP代码以获得EUR到DKK的费率。我需要这样的代码:

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

2 个答案:

答案 0 :(得分:1)

一种简单的方法是从雅虎财经下载最新的费率。例如:

<?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 :(得分:0)

来自PHP代码段

&#13;
&#13;
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);
}
&#13;
&#13;
&#13;

虽然你不一定需要cURL。如果启用了allow_furl_open,则file_get_contents也应该这样做:

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

这会返回类似

的内容

&#13;
&#13;
{lhs: "100 Euros",rhs: "129.18 U.S. dollars",error: "",icc: true}
&#13;
&#13;
&#13;