雅虎金融 api
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose.
我想像我一样,有些人需要一种获取货币汇率的方式,以便在Web项目中不时实施,因此我想我将分享为此目的开发的解决方案。
It turns out that Yahoo! Finance has a CSV API that makes it very easy to get currency exchange rates (and stock quotes) in CSV format by using a URL like this one (and it’s FREE!):
事实证明,雅虎! Finance有一个CSV API ,通过使用这样的URL(免费!),可以非常轻松地以CSV格式获取货币汇率(和股票报价):
http://finance.yahoo.com/d/quotes.csv?s=XCDUSD=X&f=sl1d1t1
This will tell us the last conversion rate for Eastern Caribbean Dollars (XCD) to United States Dollars (USD). At this point it would be useful to learn about currency codes.
这将告诉我们最后一次东加勒比元(XCD)转换为美元(USD)的汇率。 此时, 了解货币代码将很有用。
Breaking down the URL
分解URL
To begin, let’s try to understand the Yahoo! Finance URL.
首先,让我们尝试了解Yahoo! 财务网址。
URL Start
http://finance.yahoo.com/d/quotes.csv?s=
Lookup values (or stock symbols)
XCDUSD=X
The first three letters here indicate the currency you are converting from, the next three indicate the currency you are converting to, and ‘=X’ presumably says you are querying an exchange rate. That said, if you want to query multiple rates you could enter comma separated values like this:
此处的前三个字母表示要转换的货币,后三个字母表示要转换的货币,“ = X”大概表示您正在查询汇率。 也就是说,如果您要查询多个汇率,可以输入逗号分隔的值,如下所示:
XCDUSD=X,XCDGBP=X,XCDEUR=X
Result Format
&f=sl1d1t1
This part suggests the format or the fields that should be returned in the data. Let’s describe each part in turn.
这部分建议应在数据中返回的格式或字段。 让我们依次描述每个部分。
s = the Symbol (lookup value) queried.
s =查询的符号(查找值)。
l1 = the last exchange value
l1 =最后的交换值
d1 = the last exchange date
d1 =最后交易日期
t1 = the last exchange time
t1 =最后的交换时间
Up to this point you should be able to download a CSV file with the data you desire, and use it that way in some solutions. But here our objective is to demonstrate how to read that data using PHP and return it as an array or a standard class objects.
到目前为止,您应该能够下载包含所需数据的CSV文件,并在某些解决方案中以这种方式使用它。 但是这里我们的目的是演示如何使用PHP读取数据并将其作为数组或标准类对象返回。
Building the PHP Solution
构建PHP解决方案
Our objective is to develop a solution that would offer the following options:
我们的目标是开发一种解决方案,提供以下选项:
1. Lookup or query multiple currencies
1.查找或查询多种货币
2. Convert a single ‘base’ currency to multiple ‘t