【问题标题】:How to get info from YQL currency?如何从 YQL 货币中获取信息?
【发布时间】:2015-03-23 10:51:10
【问题描述】:

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

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

【问题讨论】:

  • 当你更换 ???使用实际汇率,一切都应该正常。如果没有,请描述正在发生的事情。

标签: php currency yql


【解决方案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

希望这会有所帮助。

【讨论】:

  • 没问题 :) 很高兴为您提供帮助。
【解决方案2】:

来自 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'
);

这会返回类似

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

【讨论】:

  • Warning: file_get_contents(http://www.google.com/ig/calculator?hl=en&amp;q=100EUR=?USD): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
猜你喜欢
  • 2017-04-09
  • 2012-02-01
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 2022-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多