Currency Converter

A PHP Class

This is a class written in PHP to convert currencies.
It uses a free API (getexchangerates.com) that supports most currencies and updates them every $expires_in seconds.

Usage:
require('CurrencyExchange.php');
$cc = new CurrencyExchange();

// convert($amount, $from = null, $to = null)
$euro = $cc->convert(10.00, "USD", "EUR");
echo "USD $10.00 = EUR $euro";

Source:
/*************************************************************************
 A PHP class to convert currencies using the free exchange rate API
 at http://www.getexchangerates.com/

 Created by Keith Duvall (http://www.duvalltech.com/
 Copyright (C) Oct 2012

 You may freely use, modify, and share this however you like -- personal or
 commercial -- as long as you don't charge to distribute it.

 Please give credit where credit is due.
*************************************************************************/
class CurrencyExchange
{
	// API info
	protected $api_url = 'http://www.getexchangerates.com/api/latest.json';
	protected $feed_expires = true;
	protected $expires_in = 120; // Seconds
	protected $base_currency = 'USD'; // Currency the feed is based on

	// Defaults
	protected $from_currency = 'USD';
	protected $to_currency = 'EUR';

	// Don't edit these
	protected $last_update = null;
	protected $exchange_rates = array();

	public function __construct()
	{
		$this->refreshFeed();
	}

	protected function throwEx($msg = "Unknown")
	{
		throw new Exception($msg);
	}

	/**
	* Returns the time since the API feed was last updated.
	*
	* Valid arguments are:
	* minutes: Minutes since it was last updated.
	* seconds: seconds since it was last updated.
	* absolute: absolute update time in the form of a unix EPOCH timestamp
	*/
	public function lastUpdate($format = 'absolute')
	{
		switch ($format)
		{
			case "minutes":
				return (time() - $this->last_update) / 60;
				break;
			case "seconds":
				return time() - $this->last_update;
				break;
			default:
				return $this->last_update;
				break;
		}
	}

	/**
	* Tells you whether the feed has expired based on $this->expires_in
	*/
	public function feedExpired()
	{
		if ((time() - $this->last_update) >= $this->expires_in) { return true; }
		return false;
	}

	/**
	* Returns an array of all the conversion rates
	*/
	public function listRates()
	{
		return $this->exchange_rates;
	}

	/**
	* Converts $amount from $from currency to $to currency and returns the new amount as a float.
	* If $from or $to are null, it will use the class properties above.
	*/
	public function convert($amount, $from = null, $to = null)
	{
		$from = ($from == null) ? strtoupper($this->from_currency) : strtoupper($from);
		$to = ($to == null) ? strtoupper($this->to_currency) : strtoupper($to);

		if (!is_numeric($amount)) {
			$this->throwEx("The amount to convert must be numeric.");
		}

		// Make sure the exchange rates have been pulled and the feed is current
		if ($this->last_update == null) { $this->refreshFeed(); }
		if ($this->feed_expires && $this->feedExpired()) { $this->refreshFeed(); }

		// Make sure $from and $to is valid and supported by the API
		if (!array_key_exists($from, $this->exchange_rates)) {
			$this->throwEx("Invalid or unsupported currency: {$from}");
		}
		if (!array_key_exists($to, $this->exchange_rates)) {
			$this->throwEx("Invalid or unsupported currency: {$to}");
		}

		// Convert the amount given to the base currency
		$base_amount = ($from != strtoupper($this->base_currency)) ? $amount / $this->exchange_rates[$from] : $amount;

		// Convert to new currency
		$new_amount = $base_amount * $this->exchange_rates[$to];

		return $new_amount;
	}

	/**
	* Pulls the JSON data from the API and converts the rates
	* into a more usable array.
	*/
	public function refreshFeed()
	{
		if (!$raw_data = file_get_contents($this->api_url)) {
			$this->throwEx("There was an error pulling the feed from the API.");
		}

		if (!$data = json_decode($raw_data, true)) {
			$this->throwEx("The feed API returned invalid JSON data.");
		}

		$data = $data[0]; // Strip outer layer

		$this->last_update = $data['DateTime'];
		unset($data['DateTime']);

		$this->exchange_rates = $data;
		unset($data, $raw_data);
	}

}