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

About these ads

58 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?

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

  3. 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.

  4. 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?

  5. 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.

  6. thank U for ur replay

    this code is vrey use ful for me
    in ecommners application

    thank U very much…………………

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

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

  9. 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 ;)

  10. 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

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

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

  13. 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]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s