需要用于货币转换的API [关闭]

问题描述 投票:12回答:8

请建议用于货币转换的API,它返回JSON或小尺寸的html。我使用返回11 kb的HTML的http://www.google.com/finance/converter?a=1&from=RUB&to=USD。我在我的iOS应用程序中使用它。

提前致谢!

currency converter
8个回答
17
投票

free.currencyconverterapi.com以JSON格式返回结果。

Web服务还支持JSONP。 API非常易于使用,它允许您将一种货币转换为另一种货币。

免责声明,我是该网站的作者。

一个示例转换URL是:http://free.currencyconverterapi.com/api/v6/convert?q=USD_PHP&compact=ultra&apiKey=sample-api-key,它将以json格式返回一个值,例如: { “USD_PHP”:51.459999}


12
投票

正如评论中所述,该服务于2013年11月关闭。

谷歌计算器API可以做到这一点;

请求:

http://www.google.com/ig/calculator?hl=en&q=100EUR=?USD

响应:

{lhs: "100 Euros",rhs: "145.67 U.S. dollars",error: "",icc: true}

(Qazxswpoi)


11
投票

雅虎不再工作了。见下面的评论

More info

此网址格式可用于获取不同格式的转换率。

Yahoo Finance Currency Converter

用适当的格式和参数替换带有所需代码的quotes.csv

编辑:添加了示例Url格式


10
投票

现在iGoogle已经被淘汰,Alex K的解决方案不再令人遗憾。在PHP中,这是一种替代方法,它以同样的方式工作,同样有效:

http://download.finance.yahoo.com/d/quotes.csv?s=AUDUSD=X&f=nl1d1t1

8
投票

更新:Yahoo API不再起作用了。留下这个遗留的答案只是为了提供这不再起作用的信息。


使用雅虎api:

$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$get = file_get_contents("https://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency");
$get = explode("<span class=bld>",$get);
$get = explode("</span>",$get[1]);  
$converted_amount = preg_replace("/[^0-9\.]/", null, $get[0]);

它将返回json格式,如:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDLTL%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=

在URL中查看现在有USDLTL,所以只需更改为您需要的内容。

有时候速率很低,即使显示4个数字,你也看不到它:

比率:0.0006

不要惊慌只是做一个反转查询,翻转你的货币并做一些简单的数学运算。

例如你得到的利率是从KRW到0.0006欧元,但实际利率是0.00000125,所以再次询问API,只需翻转货币:从欧元到美元的比率是多少。然后你将得到像12500000.xxx这样的巨大数字,所以让数学得到你需要的比率:1/12500000,你会得到比率= 0.00000125

希望有所帮助;)

附:解码后的URL更容易阅读,如下所示:

{
  query: {
  count: 1,
  created: "2013-12-04T13:52:53Z",
  lang: "en-US",
  results: {
    rate: {
        id: "USDLTL",
        Name: "USD to LTL",
        Rate: "2.5485",
        Date: "12/4/2013",
        Time: "8:52am",
        Ask: "2.5486",
        Bid: "2.5485"
      }
    }
  }
}

5
投票

我使用php-class转换货币汇率:

http://query.yahooapis.com/v1/public/yql
?q=select * from yahoo.finance.xchange where pair in ("USDLTL")
&format=json
&env=store://datatables.org/alltableswithkeys
&callback=

5
投票

以下是Felix Geenen对使用curl而不是fopen的回答的简单改编,因为许多服务器默认关闭fopen。

(我清理了一些代码并添加了一个减量值来重试。)

(还记得更新重试自引用,具体取决于你将函数放入的范围,例如static ::或$ this->)

/**
 * Yahoo currency rate import class
 *
 * @author     Felix Geenen (http://www.geenen-it-systeme.de)
 * @version    1.0.3
 */
class Yahoofinance
{
    public static $_url = 'http://download.finance.yahoo.com/d/quotes.csv?s={{CURRENCY_FROM}}{{CURRENCY_TO}}=X&f=l1&e=.csv';
    public static $_messages = array();

    /*
     * converts currency rates
     *
     * use ISO-4217 currency-codes like EUR and USD (http://en.wikipedia.org/wiki/ISO_4217)
     *
     * @param currencyFrom String base-currency
     * @param currencyTo String currency that currencyFrom should be converted to
     * @param retry int change it to 1 if you dont want the method to retry receiving data on errors
     */
    public static function _convert($currencyFrom, $currencyTo, $retry=0)
    {
        $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::$_url);
        $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url);

        try {
            $handle = fopen($url, "r");

            if($handle !== false) {
                $exchange_rate = fread($handle, 2000);

                # there may be spaces or breaks
                $exchange_rate = trim($exchange_rate);
                $exchange_rate = (float) $exchange_rate;

                fclose($handle);

                if( !$exchange_rate ) {
                    echo 'Cannot retrieve rate from Yahoofinance';
                    return false;
                }
                return (float) $exchange_rate * 1.0; // change 1.0 to influence rate;
            }
        }
        catch (Exception $e) {
            if( $retry == 0 ) {
                # retry receiving data
                self::_convert($currencyFrom, $currencyTo, 1);
            } else {
                echo 'Cannot retrieve rate from Yahoofinance';
                return false;
            }
        }
    }
}

4
投票

我正在使用iGoogle,直到它刚刚开始,为我服务。

感谢Nerfair在回应上面的hobailey评论时的评论,这真是太棒了。我以为我会在这里发布,所以你可以完全看到它是如何工作的!

function convert($from, $to, $retry = 0) { $ch = curl_init("http://download.finance.yahoo.com/d/quotes.csv?s=$from$to=X&f=l1&e=.csv"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_NOBODY, false); $rate = curl_exec($ch); curl_close($ch); if ($rate) { return (float)$rate; } elseif ($retry > 0) { return convert($from, $to, --$retry); } return false; }

以下是链接网址编码:http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("USDCNY")&format=json&env=store://datatables.org/alltableswithkeys&callback= *%20from%20yahoo.finance.xchange%20其中%20pair%20in%20%28%22USDCNY%22%29&format = json&env = store://datatables.org/alltableswithkeys&callback=

超级好,只需更改货币对。谢谢Nerfair!

© www.soinside.com 2019 - 2024. All rights reserved.