JavaScript, CSS and PHP Code samples

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

Comments on: "Get Live currency value using Yahoo API" (58)

  1. Thanks for script I was looking for it long time

  2. Nidhi said:

    Too good….after a long search I’ve got this…Thanx a lot!!!

  3. Kaiser Waseem said:

    Its realy great help.

    Thanks

  4. Thank you

    But notice that the url has changed from

    “http://finance.yahoo.com”

    to

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

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

  6. This is really helpfull and save my time.

    Thanks

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

  8. Awesome. You don’t know how hard it’s been finding a free/reliable web service for this. Cheers!

  9. saleem said:

    how can i send amount to covert in the url. for example amount unit more than 1.

    • Any how you are getting the unit price from the above code. Multiply with the whatever number you want to get the actual value.

      • 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 :(

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

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

  11. Thanx dude., i was looking for this., gr8 work

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

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

  14. thanks. it saves my job!

  15. raj kumar said:

    hey…..
    super yarr…..

  16. Taabeer said:

    That’s Nice!………. :-)

  17. Hey, nice work with this! But what worries me is when yahoo! decides to change their html or urls or whatever. After a lot of googling I found this site, http://www.exchangerate-api.com which looks professional and easy to use.

  18. Randika said:

    Thank u soo much u saved my day

  19. Great ! Can we get live stock quotes .. do u have a code in php ….

    Thanks in advance

  20. Thank you kindly! I’d been using a nasty data-scape till now.

  21. Shekhar said:

    Thanks very much i was searching this script from a long time ……….

    great

  22. thanks for the script. its very helpful. :)

  23. ashish gondaliya said:

    This rate is live ?????
    plz reply me as early as possible

  24. ashish gondaliya said:

    thank U for ur replay

    this code is vrey use ful for me
    in ecommners application

    thank U very much…………………

  25. Kunal Sagar said:

    Thanks :)

  26. really very nice code. this is very helpful for me to deliver my project. Best of luck and god bless you.

  27. Thanks! Will use it on client project.
    I tweaked the code, to make it ‘less hassle’

    http://pastie.org/1504759

  28. Hello,

    I want to convert $175 to IDR , but i dont know i must entered value (175) into code.

    Please Help me

    Thanks

  29. ANKITH M S said:

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

  30. Kandekar Ravi said:

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

  31. ariefsuseno said:

    thankz a lot

  32. jihchuan said:

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

  33. Thanks. This works!!

  34. Nauman said:

    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

  35. vickey said:

    i am very lucky i got this script on my first search

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

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

  38. suresh said:

    Thanks for your help

  39. ganesh said:

    but this url fetches rate 2 day back

  40. Thx man, you are the best! :)

  41. Pradeep said:

    Thanks a lot… friend

  42. Muhammad Usman said:

    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]

  43. Dhara said:

    Can I Implement This Code In My Site For Commercial Use ?

  44. thanks. this is just what i need.

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: