Need API for currency converting [closed]
Asked Answered
V

8

12

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!

Verwoerd answered 25/4, 2011 at 11:43 Comment(3)
possible duplicate of How do I get currency exchange rates via an API such as Google Finance?Aramaic
See 5 APIs Comparison in Free and Paid Currency Converter API ComparisonCrustaceous
fincharts.info/charts/exchange/…Homoeroticism
C
19

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}

Clari answered 3/2, 2014 at 5:44 Comment(4)
fincharts.info/charts/exchange/…Homoeroticism
Is there any way to send amounts in the /get? For example, if I want to get $46 converted to EURPicrate
@Picrate for now you can compute it on your side by multiplying it by 46.Clari
Paid service on stackoverflow?? Hope can get a reliable one on github.Lettered
E
12

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)

Eugenol answered 25/4, 2011 at 11:58 Comment(7)
I tried this one but it does not fully match with ISO currencies standard. For example Russian rubles are "RUB" by ISO but "RUR" for that system. So that makes it worthless.Verwoerd
Are there others? RUB & RUR seem interchangable on that system probably because RUR was the old ISO identifierEugenol
iGoogle is turned off, so now you can try yahoo bit.ly/1cPOH2F :)Burnett
@Clari hey, I need free api for conversion for all the currencies based on historical data! This looks that only has the latest currencies. can u suggest me something?Receiver
@ Nerfair This is perfect, dou know how to query based on date for the same query bit.ly/1cPOH2FReceiver
fincharts.info/charts/exchange/…Homoeroticism
google.com/ig/calculator and query.yahooapis.com/v1 both are not available.Wang
C
11

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

Chelsea answered 25/4, 2011 at 11:48 Comment(2)
Seems like it what I was looking for. Thank you!Verwoerd
This call and the https call appear to be shut down with response, "It has come to our attention that this service is being used in violation of the Yahoo Terms of Service. As such, the service is being discontinued. For all future markets and equities data research, please refer to finance.yahoo.com."Calm
J
10

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]);
Jangro answered 5/11, 2013 at 10:11 Comment(2)
Or use yahoo bit.ly/1cPOH2F :)Burnett
This is obviously a hacky solution, but one that works - so thank you :)Disposable
R
9

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=
Rosario answered 4/12, 2013 at 13:55 Comment(4)
Can you post a source query, not that horrific black magic?Pines
Added decoded-readable url, better now? ;)Rosario
Mark answer as useful if it is so ;)Rosario
Yahoo Finance API has been shut down now (From Nov '17)Jealous
A
6

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;
            }
        }
    }
}
Amedeo answered 7/2, 2014 at 12:17 Comment(1)
Thanks, I was using igoogle like many others and got screwed by the service going down. This answer is the only solution I could find so far that returns exactly what you want without the other unnecessary junk.Finbur
F
5

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;
}
Finbur answered 29/8, 2014 at 7:54 Comment(0)
I
4

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!

Inotropic answered 6/11, 2013 at 22:14 Comment(1)
This YQL query stopped working, it returns null every time.Sanbenito

© 2022 - 2024 — McMap. All rights reserved.