Get Live currency value using Yahoo API

Hi,

Here is a simple PHP code, using which you can get the live currency Value from yahoo finance ( http://finance.yahoo.com/ )

In this example i am getting the value of US Dollar in Indian Rupees.

<?php
$from   = 'USD'; /*change it to your required currencies */
$to     = 'INR';
$url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $from . $to .'=X';
$handle = @fopen($url, 'r');

if ($handle) {
    $result = fgets($handle, 4096);
    fclose($handle);
}
$allData = explode(',',$result); /* Get all the contents to an array */
$dollarValue = $allData[1];

echo 'Value of $1 in Indian Rupees is Rs. '.$dollarValue;
?>

Recently this code also got published here also

62 comments

  1. Thank you

    But notice that the url has changed from

    “http://finance.yahoo.com”

    to

    “http://download.finance.yahoo.com”

  2. hi , the code you have mentioned to convert currenct to Rupee but i want only two decimal points not more than that for that how to do that.. ?????

      1. If I want four digits after the decimal point., What i have to do?

  3. Great script, thanks!! But I want to know if these currency rates have a delay? how much time is it?

      1. which is not entirely correct, because there are currencies out there which have a greater difference in sum, as yahoo finance rounds it to 4 decimals, which will give a different outcome. BYR Belarus Ruble has such a big difference if queried by 1 or at a 1000 pieces, 0.003 if you query 1, 0.2555 if you query 1000. Didn’t figure out how to query another base sum though 😦

      2. and I did found the solution for that little problem, so I needed to share 🙂

        just reverse the query – instead of BYREUR=X use EURBYR=X and reverse your calculation if you reverse the query.

        thanks for this great little script btw. 😉

  4. Hey, Sridhar, an incredible job with that piece of code.

    I’m the person who asked you permission to adapt the code as a plugin for the DokuWiki (http://www.dokuwiki.org/) CMS. Things go OK so far but since I haven’t heard from you I thought my mail could have been filtered as SP*M and decided to let you know by this media.

    The adapted code is here: http://www.dokuwiki.org/plugin:gil with proper credits.

    Thanks again for a very interesting work.

  5. Thanks for this, works great.

    I’ve come up against Yahoo’s error 999 (too many requests). Does anyone know how often you’re allowed to query this service?

  6. Very nice work! Thanks a lot.

    Steve, setup a cron service and get silent the quotes every X minutes and then submit it to a db for not overcharge the Y! server.

  7. thank U for ur replay

    this code is vrey use ful for me
    in ecommners application

    thank U very much…………………

  8. Sir, can i implement the same using javascript. ? if so where can i get the code?
    thanks in advance.

  9. Hi
    i am not understand what is PHP kindly tell me how to use this code for live currency rate.

  10. Thanks a lot, this is exactly what we looking for! 2008-2012, yahoo still providing the nice service, hope it will not give up for at least another 50 years 😉

  11. Very useful script. But can you tell me where i put my database amount. I want my own amount?

    Please tell me as soon as possible.

    Thanks

  12. Thank you so much ! Works great. I was searching this script from a long time
    Thanks again for a very interesting work.

  13. This works brilliantly, I’m using it to convert GBP to USD for a feed for google products! Thanks!

  14. Get live Yahoo exchange rates in PHP

    [PHP]

    $from = ‘CAD’; /*change it to your required currencies */
    $to = ‘USD’;

    $url = simplexml_load_file(‘http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22′.$from.”.$to.’%22)&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys’);

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

    ?>[/PHP]

  15. We’ve been scraping Yahoo for a while but found that a) there rates are not very consistent compared to other providers and b) if you need to request rates more than every 10-minutes, the API starts blocking your requests after a while.

    I’d recommend going with a freemium provider instead. Two good ones I know are http://www.currencylayer.com and http://www.openexchangerates.org

  16. Thanks. It’s the script i’m looking for. Very simple and effective.

    It is possible add more 2 currencies for conversion in the url? I need to convert from USD to 3 different currencies.

Leave a comment