Php 从YahooAPI获取货币兑换数据,现在iGoogle已经消失了

Php 从YahooAPI获取货币兑换数据,现在iGoogle已经消失了,php,api,currency,yql,igoogle,Php,Api,Currency,Yql,Igoogle,直到昨天,我还有一个完美的预算组织者网站/应用程序与iGoogle合作 通过PHP,使用以下几行代码 file_get_contents('http://www.google.com/ig/calculator?hl=en&q=1usd=?eur'); 同样,我也能得到我所需要的一切 到今天为止,这已经不起作用了。当我调查这个问题时,所发生的是谷歌已经退出了iGoogle。真倒霉 不管怎么说,我在别处到处找,但找不到任何适合我需要的东西。我真的很想通过切换这一行代码来修复它并让

直到昨天,我还有一个完美的预算组织者网站/应用程序与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?)

因此,我想Yahoo 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
amount*$toxxx['coin'];
我真正搞不懂的是如何解析这些数据。它输出一个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上运行(尽可能少的改动)所做的工作:

// ** 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);
比如说,我想知道现在100美元等于多少欧元:

100*$toeur['usd'];

小菜一碟

问题仍然是一个非常有用的解决方案27。然而,自2015年初以来,雅虎YQL显然略微改变了api的XML输出“名称”现在不再转换为“USD to EUR”之类的字符串,而是转换为“USD/EUR”,在上面的代码中应以这种方式引用:

$toeur = array( 
     'usd' => $exchange['USD/EUR']
而不是

$toeur = array( 
     'usd' => $exchange['USD to EUR']

以类似的方式进行其他货币转换。

我创建了一个例程,根据@QuestionO27转换货币,您可以使用它

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

?>
results->rate[$i]->Name//结果->比率[$i]->比率//