A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Using Yahoo’s API to convert currencies with PHP

I have been looking around Yahoo Developer Network for this but found nothing relating to using the Currency Converter API.

You can make something similar to convert any currency to another currency using the Yahoo! database!

Here is what I came up with:

< ?php
$from   = 'USD'; //US Dollar
$to     = 'PHP'; //to Philippine Peso
 
$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);
}
 
$array = explode(',',$result);
 
var_dump($array);
/*
OUTPUTS: 
  array(4) {
  [0]=>
  string(10) ""USDPHP=X""
  [1]=>
  string(6) "47.125"
  [2]=>
  string(11) ""10/3/2008""
  [3]=>
  string(10) ""5:03pm"
"
}
 */

This is the simplest example I could give. And it works!

This entry was posted in General and tagged , , , , . Bookmark the permalink.

28 Responses to Using Yahoo’s API to convert currencies with PHP

  1. dan says:

    bert ako ni gamiton ha kay nangita pud ko ani hehehe thanks master kaayo ka bai

  2. Wonderfull dude, i was looking for this. Thanks a lot.

  3. Yep just what I needed – thanks!

  4. ashish says:

    It’s gr8 .. this is what I am looking for.

    Do you have any idea about the currency type it support? Also, can i use this frequently? I mean is yahoo allow me to request this URL thousand times a day?

  5. Wenbert says:

    @ashish You can go to their finance website see what types of currencies they support. But I am guessing they can convert any currency.

    Yahoo has unlimited resources ;) so you can use this as much as you want. But the smart thing to do is to “fetch” the data and store it in your database – you can use a cronjob to automatically fetch the currencies – autofetch the data every 30 minutes. Then, you query your database instead of querying Yahoo for every request. It will be safer this way.

  6. ashish says:

    Thanks Wenbert for your help and suggestion.

  7. bhavik says:

    Thanks dude its working

  8. James says:

    Thanks a lot. I am searching for a long time

  9. Brilliant. Exactly what I was looking for

  10. thesun says:

    Maraming salamat bro!!! been looking for this all over.

  11. satish says:

    Thank u, Its help full lot, If u want to convert 3USD to INR, then where the logic is? Please its urgent

  12. Wenbert says:

    @satish you simply multiply the output by 3? To be sure just try out the code ;)

  13. sandeep says:

    Thank you its very helpful

  14. chits says:

    Hello Friends,

    Is their a single URL bu which I can fetch the exchange rates of all the currency??
    I mean I send one URL and response should be base currency and all other currency in its respect…

    Thanks

  15. Wenbert says:

    @chits, I am not sure. But using the single URL, i would store the values in my database then update my database with a cronjob. It would also be faster on your side because you will be querying your own database instead of the URL.

  16. Chits says:

    Thanks Wenbert,

    Actually I was making a application(currency converter) which supports many currencies..now the suggestion is good but the only problem is I have to make multiple request in a loop (when doing currency update) and keep track of all of the request..in a way a bit more of work ..

  17. Chris W says:

    Thanks, perfect for those automated collection of Exchange rates!

  18. Thanks,

    I’m looking similar to this for long..

    Thanks

  19. saleem says:

    how can i send price value in the url? for example in the above example if i want to check 2USD in PHP then how can i send 2 in the url?

  20. Very useful.Just what I need.

  21. Maputing Pinoy says:

    Ang ganda ng code mo! Salamat!

  22. hm2k says:

    Thanks for your example, I used it in my ucc eggdrop tcl script.

  23. Oscar Arenas says:

    Excelente Recurso…

  24. mIke says:

    How to get currency symbol?

  25. Kamran says:

    thanks for this. I really need this.

  26. Pingback: Yahoo Finance – Converting Currencies « iDeveloper Notes

Leave a Reply to Wenbert Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>