PHP simple currency exchange rates

Many of my clients want their users to have the option to check currencies without getting to other sources, and they are right.

There are so many services out there that can give you result in no time, but you can never know who the hell is behind the service and if the currencies are updated on the nose.

For that, and for the simple reason i can not leave this unhandled, i am going to give you a simple, yet powerful, script, that give you the currency rates and values.

This script is using no other than Yahoo! services as a currency source, and by saying that, i can gurantee that you will have the exact currency rates at all times, without the need to go into another service.

The PHP class has one variable to run along with, and all the “big” effort is taking place in the Constructor of the class.

class ForeignExchange{
	private $fxRate;

	public function __construct($currencyBase, $currencyForeign){
		$url = ''.$currencyBase .$currencyForeign .'=X&f=l1';

		$c = curl_init($url);
		curl_setopt($c, CURLOPT_HEADER, 0);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
		$this->fxRate = doubleval(curl_exec($c));

	public function toBase($amount){
		if($this->fxRate == 0)
			return 0;
		return  $amount / $this->fxRate;

	public function toForeign($amount){
		if($this->fxRate == 0)
			return 0;

		return $amount * $this->fxRate;

Now, after taking a quick look at the code (That you can download directly from this page), lets go over the functions and explain.

As you can see, all the “brains” of the class is in the constructor body, where the constructor expects 2 parameters to get from the caller :

  1. currencyBase – The currency we want to get(AKA “to currency”).
  2. currencyForeign – The currency we want to convert(AKA “from currency”).

The constructor goes to the Yahoo! service and retrieve the exact currency rate.

So, if we do the following :

$calc = new ForeignExchange("EUR","USD");

This will populate the class’s variable with the rate for EUR to USD, so, if 1 USD equals to 1.5 EUR , the inner variable will be 1.5.

The constructor is getting the currency rate one time and one time only, so the exspensive CURL call will only occur once in a class’s lifetime, and will not take place again during this particular instantiation , making the class chipper and faster.

In order to calculate currency value, we simply calll the toBase function to retrive the rate for the amount we wish.

The toBase function expect one parameter, the amount to convert, so, if we want to know how much is 10 USD in EUR value, we simply do that :


This will return the EUR value for the 10 USD we sent to it.

The class has another function, which is the anti-base function, this function will return the foreign value for a given amount, so, if we to know how much 10 EUR in USD value, we simply do that :


This will return the USD value for the 10 EUR we sent to it.

It does not get any simple than that, is it?

81 thoughts on “PHP simple currency exchange rates

  1. foreign exchange

    Its like you learn my mind! You seem to know so much about this, like you wrote the guide in it or something. I think that you just could do with a few pics to drive the message residence a little bit, but other than that, that is magnificent blog. An excellent read. I will definitely be back.

  2. Pingback: My Homepage

  3. billigste mobilabonnement

    Having read this I thought it was very enlightening.
    I appreciate you finding the time and energy to put this short article
    together. I once again find myself spending a significant amount of time both reading and leaving comments.
    But so what, it was still worth it!

  4. a kassen

    It’s actually a great and helpful piece of information.
    I’m satisfied that you simply shared this useful information with us.
    Please keep us up to date like this. Thanks for sharing.

  5. best mobile games

    Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your webpage?

    My blog is in the very same niche as yours and my users would truly benefit
    from a lot of the information you present here. Please let me know
    if this ok with you. Appreciate it!

  6. Pingback: breast size increase

  7. Pingback:

  8. Pingback:

  9. Pingback: Analogue Adventures

  10. Pingback: analogue adventures

  11. mobile games

    Excellent goods from you, man. I have understand your stuff previous to and you are just extremely great.
    I really like what you have acquired here, certainly like what you’re saying
    and the way in which you say it. You make it entertaining and you still care for to keep it
    smart. I can not wait to read much more from you. This is really a wonderful

  12. Pingback: ondon event rentals facebook page

  13. Pingback: kyle leon fat loss

  14. Pingback:

  15. Pingback: traffic ticket lawyer

  16. Pingback: speeding ticket in NJ

  17. Brocula R.

    I’m extremely inspired together with your writing skills and also with the format on your blog. Is this a paid subject or did you customize it your self? Anyway keep up the nice high quality writing, it is uncommon to peer a nice blog like this one these days.

  18. mobile games

    Hi there! This post could not be written any better!

    Reading through this post reminds me of my previous
    roommate! He always kept talking about this. I will forward this
    information to him. Fairly certain he’ll have
    a very good read. I appreciate you for sharing!

  19. nulled scripts

    It is in point of fact a great and helpful piece of info. I’m glad that you shared this useful information with us. Please keep us up to date like this. Thank you for sharing.

  20. sammenlign mobilabonnement

    Hey There. I found your blog using msn. This is a very well
    written article. I’ll make sure to bookmark it
    and return to read more of your useful information.
    Thanks for the post. I will definitely return.

  21. Hipolito M. Wiseman

    I simply want to tell you that I am just newbie to blogs and certainly loved this web-site. Likely I’m planning to bookmark your blog . You absolutely have perfect well written articles. With thanks for revealing your blog.

  22. Issac Maez

    I just want to tell you that I am all new to blogging and actually savored you’re blog. Almost certainly I’m want to bookmark your blog . You definitely come with very good stories. Regards for sharing your website page.

  23. Pingback: fifa football

  24. Carroll B. Merriman

    I just want to mention I am new to weblog and certainly enjoyed this web page. Probably I’m going to bookmark your blog . You surely have beneficial posts. Regards for revealing your blog site.

  25. Pingback: plumber service

  26. Pingback: harper chair leather coaster furniture b00410fjw4 645

  27. Pingback: Кардиган Gas

  28. Pingback: Юбка Boss Orange

  29. Pingback: zubehor ipad

  30. Pingback: risunki sochi 2014

  31. Pingback: Big Truck Games

  32. Pingback: come see this

  33. home interior design

    Hello there, just became alert to your blog through Google, and found that it’s truly informative. I’m going to watch out for brussels. I’ll appreciate if you continue this in future. Many people will be benefited from your writing. Cheers!

  34. Pingback: plans to lose weight

  35. Pingback: Dream Chasing

  36. Auto approve list

    You are my inspiration, I possess few web logs and very sporadically run out from brand :). “Analyzing humor is like dissecting a frog. Few people are interested and the frog dies of it.” by E. B. White.

  37. sport pictures

    obviously like your web site however you need to check the spelling on several of your posts. Many of them are rife with spelling problems and I find it very troublesome to tell the reality on the other hand I¡¦ll definitely come again again.

  38. Jude Weninger

    I am not sure where you’re getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for fantastic info I was looking for this information for my mission.

  39. Russel Hanify

    I have to thank you for the efforts you’ve put in penning this blog. I really hope to check out the same high-grade blog posts by you in the future as well. In fact, your creative writing abilities has encouraged me to get my own, personal website now ;)|

  40. new technology

    Hiya, I’m really glad I’ve found this info. Nowadays bloggers publish just about gossips and internet and this is really irritating. A good site with interesting content, this is what I need. Thanks for keeping this site, I’ll be visiting it. Do you do newsletters? Cant find it.

  41. Mens Health

    Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a little bit, but other than that, this is great blog. An excellent read. I’ll certainly be back.

  42. Ross Rother

    My brother suggested I might like this blog. He was totally right. This post truly made my day. You cann’t imagine just how much time I had spent for this info! Thanks!

  43. indoor herb garden

    I do believe all the ideas you have presented for your post. They are really convincing and will certainly work. Nonetheless, the posts are very short for starters. May you please lengthen them a bit from next time? Thank you for the post.

  44. best insurance

    I have been absent for a while, but now I remember why I used to love this site. Thanks , I will try and check back more often. How frequently you update your site?

  45. cheap vintage snapback hats

    I like the valuable information you supply in your articles.
    I’ll bookmark your weblog and test again right here frequently.
    I am quite sure I’ll be informed many new stuff proper right here!
    Good luck for the next!

  46. auto accessories

    Fantastic goods from you, man. I have understand your stuff previous to and you’re just too wonderful. I actually like what you have acquired here, really like what you’re saying and the way in which you say it. You make it enjoyable and you still care for to keep it wise. I can’t wait to read much more from you. This is really a great site.

  47. online business books

    Thank you for every other informative site. Where else could I get that kind of information written in such an ideal way? I’ve a project that I am simply now working on, and I’ve been on the look out for such information.

  48. bally total fitness

    Thanks for some other excellent post. Where else may just anybody get that type of information in such a perfect way of writing? I’ve a presentation subsequent week, and I am at the look for such information.

  49. forex no deposit bonuses

    Thank you for sharing excellent informations. Your web-site is very cool. I am impressed by the details that you’ve on this web site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found just the info I already searched all over the place and simply couldn’t come across. What a great web site.

  50. Pingback: isaiah rashad type beat

  51. Pingback: is a great Search Engine

  52. Pingback: Yahoo is a great Search Engine

  53. Pingback: Bing is a gread Search Engine

Leave a Reply

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