Using Yahoo’s API to convert currencies with PHP

Posted on: Oct 04, 2008 by wenbert

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!


Subscribe to comments Comment | Trackback |
Post Tags: , , , ,

Browse Timeline


Comments ( 24 )

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

dan added these pithy words on Oct 04 08 at 6:28 PM

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

Sridhar Kuppalli added these pithy words on Oct 28 08 at 10:41 PM

Yep just what I needed – thanks!

Bill Trikojus added these pithy words on Oct 31 08 at 6:33 PM

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?

ashish added these pithy words on Nov 06 08 at 4:21 PM

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

Wenbert added these pithy words on Nov 06 08 at 4:39 PM

Thanks Wenbert for your help and suggestion.

ashish added these pithy words on Nov 10 08 at 3:13 PM

Thanks dude its working

bhavik added these pithy words on Nov 12 08 at 6:03 PM

Thanks a lot. I am searching for a long time

James added these pithy words on Nov 17 08 at 10:06 PM

Brilliant. Exactly what I was looking for

Ace Web Design added these pithy words on Dec 02 08 at 7:16 AM

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

thesun added these pithy words on Jan 24 09 at 11:07 AM

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

satish added these pithy words on Feb 12 09 at 8:01 PM

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

Wenbert added these pithy words on Feb 13 09 at 9:39 AM

Thank you its very helpful

sandeep added these pithy words on Feb 19 09 at 2:08 PM

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

chits added these pithy words on Mar 27 09 at 2:33 AM

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

Wenbert added these pithy words on Mar 27 09 at 7:56 AM

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

Chits added these pithy words on Mar 27 09 at 11:46 PM

Thanks, perfect for those automated collection of Exchange rates!

Chris W added these pithy words on Jun 18 09 at 3:40 PM

Thanks,

I’m looking similar to this for long..

Thanks

Synergy Informatics added these pithy words on Jul 07 09 at 7:26 PM

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?

saleem added these pithy words on Jul 20 09 at 8:51 PM

Thanks.. :)

jinson added these pithy words on Oct 09 09 at 4:36 PM

Very useful.Just what I need.

IT Support London added these pithy words on Nov 24 09 at 9:14 PM

Ang ganda ng code mo! Salamat!

Maputing Pinoy added these pithy words on Feb 03 10 at 12:54 AM

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

hm2k added these pithy words on Feb 25 10 at 10:45 PM

Good.

pog added these pithy words on Jul 09 10 at 1:14 AM

Add a Comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">


© Copyright 2007 eKini Web Developer Blog . Thanks for visiting!