1

直到昨天,我还有一个与 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-sort 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”)获取特定金额的汇率。正如我所说,直到昨天晚上,这一切都在顺利进行。

有任何想法吗?

4

3 回答 3

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'];

小菜一碟!

于 2013-11-07T22:54:48.257 回答
0

QuestionerNo27 仍然是一个非常有用的解决方案。然而,自 2015 年初以来,Yahoo YQL 显然略微更改了其 api 的 XML 输出。'Name' 现在不再转换为像 'USD to EUR' 这样的字符串,而是转换为 'USD/EUR' 并且应该在上面的代码中以这种方式引用:

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

代替

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

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

于 2015-10-12T08:45:19.320 回答
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);

?>
于 2016-02-04T07:34:14.267 回答