Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need API for currency converting [closed]

Please advice API for currency converting which returns JSON or small size html. I use http://www.google.com/finance/converter?a=1&from=RUB&to=USD that returns HTML of 11 kb. I use it in my iOS app.

Thanks in advance!

like image 554
kastet Avatar asked Apr 25 '11 11:04

kastet


People also ask

What is API currency exchange?

Currency exchange APIs gather currency updates from hundreds and thousands of data sources from various global banks, financial institutions, financial providers, and other commercial sources. It involves current, accurate data and historical data that may date back months or years.

Does Google have a currency converter API?

Google is providing a finance API to convert one currency to another currency in real-time. You will get real-time information on the currency conversion rate of any country. It's very easy and helpful to get currency conversion rate information.


2 Answers

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;
            }
        }
    }
}
like image 122
Felix Geenen Avatar answered Sep 22 '22 05:09

Felix Geenen


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}

like image 25
Manny Avatar answered Sep 21 '22 05:09

Manny