直到昨天,我有一个完美的预算组织者网站/应用程序与iGoogle合作。
通过 PHP,使用以下小行
file_get_contents('http://www.google.com/ig/calculator?hl=en&q=1usd=?eur');
类似的,我能够得到我需要的一切。
截至今天,这不再有效。当我调查这个问题时,发生的事情是谷歌已经退休了iGoogle。无赖!
无论如何,我在其他地方寻找,但我找不到任何适合我需求的东西。我真的很想通过切换这行代码来修复它并让它再次运行(即使用其他一些可用的货币 API 的地址更改 Google 地址),但似乎没有。
rate-exchange.appspot.com 的API似乎可能是iGoogle的模拟,但是,唉,它永远不会起作用。我不断收到"超出配额"消息。
(这是一个最初的问题:有人知道一个简单,可靠的,iGoogle排序的API?
所以我想雅虎YQL功能很自然(至少我认为它是可靠的)。
雅虎的查询如下所示:
http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("USDEUR", "USDJPY", "USDBGN")&env=store://datatables.org/alltableswithkeys
我真的不知道如何解析这些数据。它输出一个 XML。
我曾经拥有的是这个:
function exchange($inputAmount,$inputCurrency,$outputCurrency) {
$exchange = file_get_contents('http://www.google.com/ig/calculator?hl=en&q='.$inputAmount.$inputCurrency.'=?'.$outputCurrency);
$exchange = explode('"', $exchange);
$exchange = explode('.', $exchange['3']);
$exchange[0] = str_replace(" ", "",preg_replace('/D/', '', $exchange[0]));
if(isset($exchange[1])){
$exchange[1] = str_replace(" ", "",preg_replace('/D/', '', $exchange[1]));
$exchange = $exchange[0].".".$exchange[1];
} else{
$exchange = $exchange[0];
}
return $exchange;
}
因此,用户能够从输入货币(如"USD")和输出货币(如"EUR")获取特定金额的汇率。正如我所说,直到昨天晚上,这一直在游泳。
有什么想法吗?
关系!解决了!
对于任何感兴趣的人,以下是我所做的工作,以使我的代码与Yahoo YQL一起工作(尽可能少的chnges):
// ** GET EXCHANGE INFO FROM YAHOO YQL ** //
$url = 'http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("USDEUR", "EURUSD")&env=store://datatables.org/alltableswithkeys'; //<-- Get the YQL info from Yahoo (here I'm only interested in converting from USD to EUR and vice-versa; you should add all conversion pairs you need).
$xml = simplexml_load_file($url) or die("Exchange feed not loading!"); //<-- Load the XML file into PHP variable.
$exchange = array(); //<-- Build an array to hold the data we need.
for($i=0; $i<2; $i++): //<-- For loop to get data specific to each exchange pair (you should change 2 to the actual amount of pairs you're querying for).
$name = (string)$xml->results->rate[$i]->Name; //<-- Get the name of the pair and turn it into a string (this looks like this: "USD to EUR").
$rate = (string)$xml->results->rate[$i]->Rate; //<-- Do the same for the actual rate resulting from the conversion.
$exchange[$name] = $rate; //<-- Put the data pairs into the array.
endfor; //<-- End for loop. :)
// ** WORK WITH EXCHANGE INFO ** //
$toeur = array( //<-- Create new array specific for conversion to one of the units needed.
'usd' => $exchange['USD to EUR'], //<-- Create an array key for each unit used. In this case, in order to get the conversion of USD to EUR I ask for it from my $exchange array with the pair Name.
'eur' => 1); //<-- The way I coded the app, I found it more practical to also create a conversion for the unit into itself and simply use a 1, which translates into "do not convert"
$tousd = array(
'eur' => $exchange['EUR to USD'],
'usd' => 1);
这基本上是获取所需所有交换信息所需的全部内容。之后,您可以像这样使用它:
amount*$toxxx['coin'];
所以,假设我现在想知道 100 美元是多少欧元:
100*$toeur['usd'];
小菜一碟!
仍然是 QuestionerNo27 的一个非常有用的解决方案。然而,自2015年初以来,雅虎YQL显然略微改变了其API的XML输出。"Name"现在不再转换为"USD to EUR"之类的字符串,而是转换为"USD/EUR",在上面的代码中应以这种方式引用:
$toeur = array(
'usd' => $exchange['USD/EUR']
而不是
$toeur = array(
'usd' => $exchange['USD to EUR']
以及其他货币转换的类似方式。
我创建了一个例程,根据您可以消费@QuestionerNo27 http://jamhubsoftware.com/geoip/currencyconvertor.php?fromcur=USD&tocur=EUR&amount=1 来转换货币
<?php
$fromcur = $_GET['fromcur'];
$tocur = $_GET['tocur'];
$amt = $_GET['amount'];
// ** GET EXCHANGE INFO FROM YAHOO YQL ** //
$url = 'http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("'.$fromcur.$tocur.'")&env=store://datatables.org/alltableswithkeys'; //<-- Get the YQL info from Yahoo (here I'm only interested in converting from USD to EUR and vice-versa; you should add all conversion pairs you need).
$xml = simplexml_load_file($url) or die("Exchange feed not loading!"); //<-- Load the XML file into PHP variable.
$exchange = array(); //<-- Build an array to hold the data we need.
for($i=0; $i<2; $i++): //<-- For loop to get data specific to each exchange pair (you should change 2 to the actual amount of pairs you're querying for).
$name = (string)$xml->results->rate[$i]->Name; //<-- Get the name of the pair and turn it into a string (this looks like this: "USD to EUR").
$rate = (string)$xml->results->rate[$i]->Rate; //<-- Do the same for the actual rate resulting from the conversion.
$exchange[$name] = $rate; //<-- Put the data pairs into the array.
endfor; //<-- End for loop. :)
// ** WORK WITH EXCHANGE INFO ** //
$conv = $fromcur . '/' . $tocur;
$toeur = array( //<-- Create new array specific for conversion to one of the units needed.
$tocur => $amt*$exchange[$conv], //<-- Create an array key for each unit used. In this case, in order to get the conversion of USD to EUR I ask for it from my $exchange array with the pair Name.
$fromcur => $amt,
"ex_amt" =>$amt*$exchange[$conv]); //<-- The way I coded the app, I found it more practical to also create a conversion for the unit into itself and simply use a 1, which translates into "do not convert"
echo json_encode($toeur);
?>