iGoogle 消失后,从 Yahooapis 获取货币换算数据 [英] Getting currency conversion data from Yahooapis now that iGoogle is gone

查看:20
本文介绍了iGoogle 消失后,从 Yahooapis 获取货币换算数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到昨天,我有一个完美的预算管理器网站/应用程序与 iGoogle 合作.

Up until yesterday I had a perfectly working budget organizer site/app working with iGoogle.

通过PHP,使用下面的小行

Through PHP, using the following little line

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

类似的,我能够得到我需要的一切.

and similar I was able to get all I needed.

截至今天,这不再有效.当我调查这个问题时,发生的事情是 Google 已经停用了 iGoogle.无赖!

As of today, this is no longer working. When I looked into the issue, what has happened is that Google has retired iGoogle. Bummer!

无论如何,我在别处环顾四周,但找不到适合我需要的任何东西.我真的很想通过切换这一行代码来修复它并让它再次运行(即使用其他可用的货币 API 的地址更改 Google 地址),但似乎没有.

Anyway, I was looking around elsewhere but I can't find anything that fits my needs. I would REALLY love to just fix it and get it running again by just switching this one line of code (i.e. changing the Google address with the address of some other currency API available) but it seems like none does.

来自 rate-exchange.appspot.com 的 API 似乎可以是 iGoogle 模拟,但可惜,它永远无法工作.我不断收到超出配额"的消息.

The API from rate-exchange.appspot.com seems like it could be a iGoogle analog but, alas, it never works. I keep getting an "Over Quota" message.

(这里有一个初步的问题:有人知道一个简单、可靠的 iGoogle-sort API 吗?)

(Here comes an initial question: anybody out there know of a simple, reliable, iGoogle-sort API?)

所以我想自然是 Yahoo YQL 功能(至少我认为它是可靠的).

So I guess the natural thing would be to the Yahoo YQL feature (at least I suppose it is as reliable).

Yahoo 的查询如下所示:

Yahoo's queries look like this:

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.

What I really can't figure out is how to parse this data. It outputs an 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;
}

因此,用户能够从输入货币(例如美元")和输出货币(例如欧元")中获取特定金额的汇率.正如我所说,直到昨天晚上,这一切都在顺利进行.

So the user was able to get the exchange rate from an input currency such as "USD" and an output currency such as "EUR" on a specific amount of money. As I said, this was working swimmingly up until yesterday night.

有什么想法吗?

推荐答案

没关系!解决了!

对于任何感兴趣的人,以下是我为使我的代码与 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);

这基本上是您获取所需的所有交易所信息所需的全部内容.之后,您可以像这样使用它:

This is basically all you need to get all the exchange info you want. After that, you use it all something like this:

amount*$toxxx['coin'];

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

So, say I wanted to know how many Euro is 100 USD right now:

100*$toeur['usd'];

小菜一碟!

这篇关于iGoogle 消失后,从 Yahooapis 获取货币换算数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆