Need API for currency converting [closed]

偶尔善良 提交于 2019-11-27 03:04:28

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}

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)

suhair

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

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

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=

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

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

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!