现在iGoogle已经消失,从Yahooapis获取货币转换数据

时间:2013-11-07 05:59:07

标签: php api currency yql igoogle

截至昨天,我有一个完美的预算组织者网站/应用程序与iGoogle合作。

通过PHP,使用以下小行

file_get_contents('http://www.google.com/ig/calculator?hl=en&q=1usd=?eur');

和类似的我能够得到我所需要的一切。

截至今天,这已不再适用。当我调查这个问题时,发生的事情是谷歌已经退休了iGoogle。无赖!

无论如何,我在其他地方环顾四周,但找不到符合我需求的东西。我真的很想通过改变这一行代码来修复它并再次运行它(即用一些其他货币API的地址改变谷歌地址),但似乎没有。但/ / p>

来自rate-exchange.appspot.com的API似乎可能是iGoogle模拟,但是,唉,它永远不会奏效。我一直收到“Over Quota”消息。

(这里有一个初步问题:那里有人知道一个简单,可靠的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”)获得汇率。就像我说的那样,直到昨天晚上才开始工作。

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

没关系!解决了!

对于任何有兴趣的人,以下是我使用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);

这基本上是您获得所需的所有交换信息所需的全部内容。在那之后,你可以使用这样的东西:

amount*$toxxx['coin'];

所以,我想知道现在有多少欧元是100美元:

100*$toeur['usd'];

一块蛋糕!

答案 1 :(得分:0)

QuestionerNo27仍是一个非常有用的解决方案。然而,自2015年初以来,雅虎YQL显然略微改变了他们的api的XML输出。 &#39;名称&#39;现在不再转换为类似“EUR”到“EUR&#”的字符串,而是转换为“USD / EUR&#39;并且应该在上面的代码中以这种方式引用:

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

而不是

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

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

答案 2 :(得分:0)

我根据@ 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);

?>