php 以编程方式访问货币汇率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/181990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically access currency exchange rates
提问by Adam Pierce
I'm setting up an online ordering system but I'm in Australia and for international customers I'd like to show prices in US dollars or Euros so they don't have to make the mental effort to convert from Australian dollars.
我正在建立一个在线订购系统,但我在澳大利亚,对于国际客户,我想以美元或欧元显示价格,这样他们就不必费心去兑换澳元。
Does anyone know if I can pull up to date exchange rates off the net somewhere in an easy-to-parse format I can access from my PHP script ?
有谁知道我是否可以通过我的 PHP 脚本访问的易于解析的格式从网上获取最新的汇率?
UPDATE:I have now written a PHP class which implements this. You can get the code from my website.
更新:我现在已经编写了一个实现这一点的 PHP 类。您可以从我的网站获取代码。
采纳答案by Greg
You can get currency conversions in a simple format from yahoo:
您可以从雅虎获得一种简单格式的货币换算:
For example, to convert from GBP to EUR:
http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv
例如,要从英镑转换为欧元:
http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv
回答by philoye
This answer is VERY late, but there is a key bit of info missing from the above answers.
这个答案很晚了,但是上面的答案中缺少一些关键信息。
If you want to show accurate prices to your customers it is important to understand how foreign exchange rates work.
如果您想向您的客户显示准确的价格,了解外汇汇率的工作原理很重要。
Most FX services only quote the spot rate (midway between the Bid and Ask). The spot is a kind of shorthand for the exchange rate, but no one gets the spot because you can only sell at the bid or buy at the ask. You're usually looking at least a 1% spread between them, so the spot rate is 0.5% off for your customers.
大多数外汇服务只报价即期汇率(买入价和卖出价之间的中间价)。现货是汇率的一种简写,但没有人得到现货,因为你只能以买价卖出或以要价买进。您通常会看到它们之间的差价至少为 1%,因此您的客户的即期汇率为 0.5%。
But it doesn't stop there, your customers almost certainly are using a credit card and Visa/Mastercard/Amex all charge foreign exchange fees. These are non-trivial in my experience, at LEAST 2.5%. For example, Citibank Australia charges 3.3%. These vary from card to card so there's no way for you to predict the final price that your customers will be billed.
但它并不止于此,您的客户几乎肯定使用信用卡,而 Visa/Mastercard/Amex 都收取外汇费用。根据我的经验,这些并不重要,至少为 2.5%。例如,澳大利亚花旗银行收取 3.3% 的费用。这些因卡而异,因此您无法预测向客户收取费用的最终价格。
If you want to quote an "accurate" price to your customers based on an exchange rate, you need to factor in the above and provide a buffer so that you don't end up charging more than what you quoted.
如果您想根据汇率向客户报价“准确”的价格,则需要考虑上述因素并提供缓冲,以免最终收取的费用超出报价。
FWIW, I've been adding 4% to what the F/X conversion would otherwise indicate.
FWIW,我已经将 F/X 转换所指示的值增加了 4%。
回答by Jacco
Might be nice to add
添加可能很好
http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml
http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml
to the list.
到列表中。
The official reference rates provides by the European Central Bankbased on the regular daily concertation procedure between central banks within and outside the European System of Central Banks.
官方参考利率由欧洲中央银行根据欧洲中央银行体系内外中央银行之间的日常协调程序提供。
The feed is in XML and some other formats.
Updating normally takes place at 2.15 p.m. (14:15) ECB time (= Frankfurt time).
提要采用 XML 和其他一些格式。
更新通常在欧洲央行时间(= 法兰克福时间)下午 2.15 (14:15) 进行。
回答by zeFree
another very great free and opensource link is this:
另一个非常棒的免费开源链接是这样的:
https://raw.github.com/currencybot/open-exchange-rates/master/latest.json
(I found about it here: http://josscrowcroft.github.com/open-exchange-rates/)
[Update]:
Open Exchange Rates project data has been moved away from GitHub.
It is available now at: http://openexchangerates.org/
Data in JSON format is available at: http://openexchangerates.org/latest.json
https://raw.github.com/currencybot/open-exchange-rates/master/latest.json
(我在这里找到了它:http: //josscrowcroft.github.com/open-exchange-rates/)
[更新] :
Open Exchange Rates 项目数据已从 GitHub 移走。
现在可以在:http://openexchangerates.org/
获得 JSON 格式的数据:http://openexchangerates.org/latest.json
No access fees, no rate limits, No ugly XML - just free, hourly updated exchange rates in JSON format.
This is not "entirely" free now. The new licensing states that up to 1000 hits per month is allowed, and then you need to pay. You also need to pay if you want to use the single currency converter (basic functionality).
没有访问费用,没有速率限制,没有丑陋的 XML - 只是免费的、每小时更新的 JSON 格式的汇率。
这现在不是“完全”免费的。新的许可规定每月最多允许点击 1000 次,然后您需要付费。如果您想使用单一货币转换器(基本功能),您还需要付费。
[ Note: You may want to look at thisanswer as well. ]
[注意:您可能也想看看这个答案。]
回答by Natsukane
I recently implemented the same thing, but using Google's API. The query URL looks like this:
我最近实现了同样的事情,但使用了谷歌的 API。查询 URL 如下所示:
http://www.google.com/ig/calculator?hl=en&q=1GBP=?USD
It takes 3 parameters. The first parameter is the amount, followed by the ISO 4217currency code you're converting from, an equals sign and a question mark, and the currency code you're converting to. You can find a list of codes that Google supports here. The response to the query will look like this:
它需要3个参数。第一个参数是金额,后跟要转换的ISO 4217货币代码、等号和问号,以及要转换为的货币代码。您可以在此处找到 Google 支持的代码列表。对查询的响应如下所示:
{lhs: "1 British pound",rhs: "1.6132 U.S. dollars",error: "",icc: true}
This is pretty self-explanatory, so I won't go into details here. This is how I handled the query response:
这是不言自明的,所以我不会在这里详细介绍。这就是我处理查询响应的方式:
function convert_currency($amount, $from_code, $to_code){
ini_set('max_execution_time', 60);
$temp = 'http://www.google.com/ig/calculator?hl=en&q=' . $amount . $from_code . '=?' . $to_code;
$response = file_get_contents($temp);
$result_string = explode('"', $response);
$final_result = $result_string['3'];
$float_result = preg_replace("/[^0-9\.]/", '', $full_result);
return $float_result;
}
I'm sure it's far from the most elegant way to do this, but I'm pretty new to PHP. Hope it helps!
我确信这远不是最优雅的方法,但我对 PHP 还很陌生。希望能帮助到你!
回答by mtelis
I added Open Data table to YQL, you can use it to retrieve exchange rate data from yahoo.finance.
我在 YQL 中添加了 Open Data 表,您可以使用它从 yahoo.finance 检索汇率数据。
Comma-separated format is preferrable over "where pair in ('EURUSD','GBPUSD')" but anyway, you can use both and even intermix them.
逗号分隔格式比“where pair in ('EURUSD','GBPUSD')”更可取,但无论如何,您可以同时使用两者,甚至混合使用它们。
回答by ólafur Waage
Here is a Soap service that offers exchange rate
这是提供汇率的肥皂服务
回答by David Wengier
This site has a currency converter service for free:
本站提供免费货币兑换服务:
回答by Kevin
回答by oodavid
iGoogle was retired on November 1, 2013. This API no longer works.
iGoogle 已于 2013 年 11 月 1 日停用。此 API 不再有效。
To get the exchange rate you can use something like this:
要获得汇率,您可以使用以下方法:
function get_exchange_rate($from, $to){
$data = file_get_contents("http://www.google.com/ig/calculator?hl=en&q=1{$from}=?{$to}");
preg_match('/rhs\:\s?"([0-9\.]+)/', $data, $m);
return $m[1];
}
You could add a DB cache in there to make sure you don't get throttled etc.
您可以在其中添加一个数据库缓存,以确保您不会受到限制等。
As has been noted on other posts / comments you'd then use this rate to calculate your currencies
正如在其他帖子/评论中所指出的那样,您将使用此汇率来计算您的货币

