Get live exchange rates in PHP

Posted by 6bytes at 8, November, 2010

Category: PHP

Tags: , ,

A very simple PHP script to get almost live currency exchange rates from Yahoo Finance. I’m saying “almost” as as far as I know Yahoo updates it with some minor delay.

$from = 'GBP';
$to = 'USD';
$url = 'http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s='.$from.$to.'=X';
$handle = fopen($url, 'r');

if ($handle) {
	$result = fgetcsv($handle);
	fclose($handle);
}

echo '1 '.$from.' is worth '.$result[0].' '.$to.' Based on data on '.$result[1].' '.$result[2];

On the day of writing this post, above script will display:

1 GBP is worth 1.6125 USD Based on data on 11/8/2010 10:26am

Short explanation

I think everything is clear except for the URL and its parameters. Lets have a closer look.
In our example

$url = 'http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s='.$from.$to.'=X';

translates to

http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s=GBPUSD=X

Pasting this into your browser returns a csv file with three columns. Current exchange rate, date and time. Where:

  • l1 – exchange rate,
  • d1 – date,
  • t1 – time

If exact time is not what you’re looking for and all you need is just the exchange rate your url could look like this:

http://finance.yahoo.com/d/quotes.csv?f=l1&s=GBPUSD=X

I couldn’t find full list of parameters on yahoo but this site seems to have it all covered.

Yahoo finance not only gives you access to exchange rates but also stock data.

  • Easy to use! Thanks for sharing!

  • Syrian Man

    Great work..
    thank very much Wojtek

  • I wonder how to get more than one rate a request? Please help!

  • Thanks for the code, very simple and easy to use.
    Good Work!

  • Multiple rate requests:
    The format of the URL is:
    http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s=GBPUSD=X

    Just add extra currency indicators at the end, in the form
    http://finance.yahoo.com/d/quotes.csv?f=sl1d1t1&s=GBPUSD=X+GBPCAD=X+GBPEUR=X
    In other words, just add +AAABBB=X for each entry at the end.

    To parse the CSV file, use multiples of 3:
    so in the above $result[0] would be GBP/USD; $result[3] is GBP/CAD; $result[6] is GBP/EUR, and so on.

    Hope this helps.

  • Matt

    Exactly what I was looking for, but unfortunately I get an error.

    “1 GBP is worth Missing Symbols List. USD Based on data on”

    The debug result is:
    Notice: Undefined offset: 1 in path/to/file.php on line 31
    Notice: Undefined offset: 2 in path/to/file.php on line 31

    Not sure if I did something wrong or Yahoo changed their API…

  • Mark

    Just what the doctor ordered! many thanks for sharing

  • bertrand

    For whatever reason, my rates do not update – they dont seem to be live – i have to refresh the page to get the latest quotes – maybe it is every 20 minutes or so – would you know when on average those quotes get updated? do i have to refresh the page or it is done automatically?

    many thanks

  • THANKS, GREAT WORK!

    Also to speed it a little bit you can also use:
    ========================================
    $from = ‘AUD’;
    $to = ‘USD’;
    $url = ‘http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s=’.$from.$to.’=X’;
    $handle = file_get_contents($url);

    if ($handle) {
    $hacurliverate = str_getcsv($handle);
    }

    echo ‘1 ‘.$from.’ is worth ‘.$hacurliverate[0].’ ‘.$to.’ Based on data on ‘.$hacurliverate[1].’ ‘.$hacurliverate[2];
    echo ”.$handle;
    ========================================
    Same will be done as 6bytes’s code but a little bit quickly. All credits goes to 6bytes as I the above code is done by me but based on 6bytes’s findings and technique.

    Have a great time. Enjoy!

  • JUstin

    How/Could you just have a dynamic PHP field that updates? ie: like with “echo date(“Y”);” but, have the exchange rate automatically update within the page?

    please help – I have no idea!!

  • We stumbled over here from a different web page and thought I might
    as well check things out. I like what I see so now i’m following you. Look forward to looking into your web page yet again.

  • Muhammad Usman

    results->rate->Rate);
    $rate = round($rate, 2);
    echo $rate;
    ? >

  • satheesh

    how to pass amounts

    • You need to be a bit more specific. What are you truing to achieve?

  • iamyojimbo

    Is there a limit to how many requests an application can make to this API?

  • MLS

    Thanks man! This helps me! God bless!

  • Mohammed

    Thank you man. That was great. And saved my time a lot

  • How to implement this currency conversion script on my html custom price option

  • I use geoIp json script also for IP detection and show price according to user IP location still I add custom currency rate like this one
    so let me know how to convert live currency rate suppose I want to convert $29.99 into euro

    $29.99

    €28.11

    £24.40

    • sixbytesunder

      Mind you, this post was written almost 7 years ago. Yahoo might have changed their service by now.
      Either way the above script would give you the conversion rate for the value of 1. For example:

      http://finance.yahoo.com/d/quotes.csv?f=l1&s=GBPUSD=X

      would give you the value of how many dollars you’ll get for 1 pound. If you want the other way around, just change the currency codes from GBPUSD to USDGBP so it becomes this:

      http://finance.yahoo.com/d/quotes.csv?f=l1&s=USDGBP=X

      Once you have the exchange rate you can multiply it by 29.99 and you’ll know how many pounds you’ll get for that many dollars.

  • Anthony Berglas

    Yahoo service seems broken as of Nov 2017,