需要API进行货币转换

发布于 2024-11-03 05:32:51 字数 1539 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

烟雨扶苏 2024-11-10 05:32:51

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}

free.currencyconverterapi.com returns results in JSON format.

The web service also supports JSONP. The API is very easy to use, and it lets you convert one currency to another.

Disclaimer, I'm the author of the website.

A sample conversion URL is: http://free.currencyconverterapi.com/api/v6/convert?q=USD_PHP&compact=ultra&apiKey=sample-api-key which will return a value in json format, e.g. {"USD_PHP":51.459999}

抚你发端 2024-11-10 05:32:51

正如评论中提到的,该服务已于 2013 年 11 月关闭。

Google 计算器 API 可以执行此操作;

请求:

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

响应:

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

(更多信息)

As mentioned in the comments this service was shut down in Nov 2013.

Googles calulator API can do this;

Request:

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

Response:

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

(More info)

濫情▎り 2024-11-10 05:32:51

雅虎不再工作。请参阅下面的评论

雅虎财经货币转换器

< Strike>此网址格式可用于获取不同格式的转化率。

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

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

编辑:添加示例网址格式

Yahoo is no longer working. See comment below

Yahoo Finance Currency Converter.

This url format could be used to fetch conversion rates in different formats.

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

Substitute quotes.csv with appropriate format and parameters with the required codes

EDIT: Added Example Url formats

此生挚爱伱 2024-11-10 05:32:51

现在 iGoogle 已经被淘汰了,Alex K 的解决方案不再有效。
在 php 中,这是一种替代方案,其工作方式相同并且同样有效:

$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]);

Now iGoogle has been killed off, Alex K's solution no longer works sadly.
In php, this is an alternative which works in the same way and is just as effective:

$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]);
飘然心甜 2024-11-10 05:32:51

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


使用yahoo api:

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=

它将返回json格式,例如:

{
  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"
      }
    }
  }
}

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

另外,有时汇率非常低,即使显示 4 个数字,您也看不到它:

汇率:0.0006

不要惊慌,只需进行反转查询、翻转货币并进行一些简单的数学计算即可。

例如,您得到的汇率是从韩元到欧元 0.0006,但实际汇率类似于 0.00000125,所以再次询问 API,只需翻转货币:欧元到美元的比率是多少。那么你会得到像 12500000.xxx 这样的巨大数字,所以进行数学运算以获得你需要的比率:1/12500000,你会得到比率 = 0.00000125

希望有帮助;)

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

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=

UPDATE: Yahoo API is not working anymore. Leaving this legacy answer just to provide information that this doesn't work anymore.


use yahoo api:

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=

it will return json format like:

{
  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"
      }
    }
  }
}

Check out in the URL there is USDLTL now, so just change to what you need.

Also sometime the rate is so low, that you don't see it even with 4 numbers it shows:

Rate: 0.0006

Do not panic just make a reversal query, flip your currencies and make some simple math.

e.g. you got that the rate is from KRW to EUR 0.0006 but the real rate is something like 0.00000125 so ask API again, just flip the currencies: what is the ratio from EUR to USD. then you will get huge number like 12500000.xxx so make math to get the ratio you need: 1/12500000 and you will get ratio = 0.00000125

Hope that helps ;)

P.S. decoded URL which is easier to read looks like this:

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=
呆橘 2024-11-10 05:32:51

我使用 php 类来转换货币汇率:

/**
 * 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;
            }
        }
    }
}

I use a php-class to convert currency rates:

/**
 * 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;
            }
        }
    }
}
给妤﹃绝世温柔 2024-11-10 05:32:51

这是 Felix Geenen 答案的一个简单改编,使用 curl 而不是 fopen,因为很多服务器默认关闭 fopen。

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

(还记得根据您将函数放入的范围更新重试自引用,例如 static:: 或 $this-> )

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;
}

Here is a simple adaptation of Felix Geenen's answer to use curl instead of fopen since a lot of servers have fopen turned off by default.

( I cleaned up some code and added a decrement value to retry. )

( Also remember to update the retry self reference depending on the scope you drop the function in to eg. static:: or $this-> )

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;
}
夜访吸血鬼 2024-11-10 05:32:51

我一直在使用 iGoogle,直到它崩溃了,这对我来说是对的。

感谢 Nerfair 在回应上面霍贝利的评论时的评论,这太棒了。我想我应该将其发布在这里,以便您可以充分了解它是如何工作的!

http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange 其中 ("USDCNY")&format=json&env=store 中的配对: //datatables.org/alltableswithkeys&callback=

这是编码的链接 url:http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28 %22USDCNY%22%29&format=json&env=store://datatables.org/alltableswithkeys&callback=

超级好,就这样更改货币对。谢谢内菲尔!

I was using iGoogle until it just went belly up, serves me right.

Thanks to Nerfair tho in his comment in response to hobailey's comment above, this works AWESOME. I thought I would post it here so you can fully see how it works!

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=

Here is the link url encoded: http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22USDCNY%22%29&format=json&env=store://datatables.org/alltableswithkeys&callback=

Super nice, just change the currency pair. Thanks Nerfair!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文