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!
Browse Timeline
Comments ( 17 )
dan added these pithy words on Oct 04 08 at 6:28 pmbert ako ni gamiton ha kay nangita pud ko ani hehehe thanks master kaayo ka bai
Sridhar Kuppalli added these pithy words on Oct 28 08 at 10:41 pmWonderfull dude, i was looking for this. Thanks a lot.
ashish added these pithy words on Nov 06 08 at 4:21 pmIt’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?
Wenbert added these pithy words on Nov 06 08 at 4:39 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.
ashish added these pithy words on Nov 10 08 at 3:13 pmThanks Wenbert for your help and suggestion.
bhavik added these pithy words on Nov 12 08 at 6:03 pmThanks dude its working
James added these pithy words on Nov 17 08 at 10:06 pmThanks a lot. I am searching for a long time
Ace Web Design added these pithy words on Dec 02 08 at 7:16 amBrilliant. Exactly what I was looking for
thesun added these pithy words on Jan 24 09 at 11:07 amMaraming salamat bro!!! been looking for this all over.
satish added these pithy words on Feb 12 09 at 8:01 pmThank u, Its help full lot, If u want to convert 3USD to INR, then where the logic is? Please its urgent
Wenbert added these pithy words on Feb 13 09 at 9:39 am@satish you simply multiply the output by 3? To be sure just try out the code ;)
sandeep added these pithy words on Feb 19 09 at 2:08 pmThank you its very helpful
chits added these pithy words on Mar 27 09 at 2:33 amHello 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
Wenbert added these pithy words on Mar 27 09 at 7:56 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.
Chits added these pithy words on Mar 27 09 at 11:46 pmThanks 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 ..
Chris W added these pithy words on Jun 18 09 at 3:40 pmThanks, perfect for those automated collection of Exchange rates!