如何获取YQL币的信息?

How to get info from YQL currency?

我尝试了很多来自互联网的脚本,但是有人为我工作,所以也许你可以帮助我,我不知道如何创建 PHP 代码来获得欧元对丹麦克朗的汇率。我需要这样的代码:

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

来自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。如果启用 allow_furl_open,file_get_contents 也应该这样做:

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

这会return类似于

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

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

<?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

希望对您有所帮助。