/* * File: CurrencyConverter. php * Author: Simon Jarvis * Copyright: 2005 Simon Jarvis * Date: 10/12/05 * Link: http://www.white-hat-web-design.co.uk/articles/php-currency-conversion.php * * This program is free software; you can redistribute it and/or * Modify it under the terms of the GNU General Public License * As published by the Free Software Foundation; either version 2 * Of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * But without any warranty; without even the implied warranty * MERCHANTABILITY or fitness for a special PURPOSE. See * GNU General Public License for more details: * Http://www.gnu.org/licenses/gpl.html * */ Class CurrencyConverter { Var $ xml_file = "www.ecb.int/stats/eurofxref/eurofxref-daily.xml "; Var $ mysql_host, $ mysql_user, $ mysql_pass, $ mysql_db, $ mysql_table; Var $ exchange_rates = array (); // Load Currency Rates Function CurrencyConverter ($ host, $ user, $ pass, $ db, $ tb ){ $ This-> mysql_host = $ host; $ This-> mysql_user = $ user; $ This-> mysql_pass = $ pass; $ This-> mysql_db = $ db; $ This-> mysql_table = $ tb; $ This-> checkLastUpdated (); $ Conn = mysql_connect ($ this-> mysql_host, $ this-> mysql_user, $ this-> mysql_pass ); $ Rs = mysql_select_db ($ this-> mysql_db, $ conn ); $ SQL = "SELECT * FROM". $ this-> mysql_table; $ Rs = mysql_query ($ SQL, $ conn ); While ($ row = mysql_fetch_array ($ rs )){ $ This-> exchange_rates [$ row ['currency '] = $ row ['rate']; } } /* Perform the actual conversion, defaults to £1. 00 GBP to USD */ Function convert ($ amount = 1, $ from = "GBP", $ to = "USD", $ decimals = 2 ){ Return (number_format ($ amount/$ this-> exchange_rates [$ from]) * $ this-> exchange_rates [$ to], $ decimals )); } /* Check to see how long since the data was last updated */ Function checkLastUpdated (){ $ Conn = mysql_connect ($ this-> mysql_host, $ this-> mysql_user, $ this-> mysql_pass ); $ Rs = mysql_select_db ($ this-> mysql_db, $ conn ); $ SQL = "SHOW TABLE STATUS FROM". $ this-> mysql_db. "LIKE '". $ this-> mysql_table ."'"; $ Rs = mysql_query ($ SQL, $ conn ); If (mysql_num_rows ($ rs) = 0 ){ $ This-> createTable (); } Else { $ Row = mysql_fetch_array ($ rs ); If (time ()> (strtotime ($ row ["Update_time"]) + (12*60*60 ))){ $ This-> downloadExchangeRates (); } } } /* Download xml file, extract exchange rates and store values in database */ Function downloadExchangeRates (){ $ Currency_domain = substr ($ this-> xml_file, 0, strpos ($ this-> xml_file ,"/")); $ Currency_file = substr ($ this-> xml_file, strpos ($ this-> xml_file ,"/")); $ Fp = @ fsockopen ($ currency_domain, 80, $ errno, $ errstr, 10 ); If ($ fp ){ $ Out = "GET". $ currency_file. "HTTP/1.1rn "; $ Out. = "Host:". $ currency_domain. "rn "; $ Out. = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: 1.8) Gecko/20051111 Firefox/1.5rn "; $ Out. = "Connection: Closernrn "; Fwrite ($ fp, $ out ); While (! Feof ($ fp )){ $ Buffer. = fgets ($ fp, 128 ); } Fclose ($ fp ); $ Pattern = "{ } Is "; Preg_match_all ($ pattern, $ buffer, $ xml_rates ); Array_shift ($ xml_rates ); For ($ I = 0; $ I $ Exchange_rate [$ xml_rates [0] [$ I] = $ xml_rates [1] [$ I]; } $ Conn = mysql_connect ($ this-> mysql_host, $ this-> mysql_user, $ this-> mysql_pass ); $ Rs = mysql_select_db ($ this-> mysql_db, $ conn ); Foreach ($ exchange_rate as $ currency => $ rate ){ If (is_numeric ($ rate) & ($ rate! = 0 )){ $ SQL = "SELECT * FROM". $ this-> mysql_table. "WHERE currency = '". $ currency ."'"; $ Rs = mysql_query ($ SQL, $ conn) or die (mysql_error ()); If (mysql_num_rows ($ rs)> 0 ){ $ SQL = "UPDATE". $ this-> mysql_table. "SET rate =". $ rate. "WHERE currency = '". $ currency ."'"; } Else { $ SQL = "INSERT INTO". $ this-> mysql_table. "VALUES ('". $ currency. "',". $ rate .")"; } $ Rs = mysql_query ($ SQL, $ conn) or die (mysql_error ()); } } } } /* Create the currency exchange table */ Function createTable (){ $ Conn = mysql_connect ($ this-> mysql_host, $ this-> mysql_user, $ this-> mysql_pass ); $ Rs = mysql_select_db ($ this-> mysql_db, $ conn ); $ SQL = "CREATE TABLE ". $ this-> mysql_table. "(currency char (3) not null default'', rate float not null default '0', primary key (currency) ENGINE = MyISAM "; $ Rs = mysql_query ($ SQL, $ conn) or die (mysql_error ()); $ SQL = "INSERT INTO". $ this-> mysql_table. "VALUES ('eur', 1 )"; $ Rs = mysql_query ($ SQL, $ conn) or die (mysql_error ()); $ This-> downloadExchangeRates (); } } ?> |