本文实例讲述了PHP实现货币换算的方法。分享给大家供大家参考。
具体实现代码如下:
复制代码 代码如下:
/*
* 文件:CurrencyConverter.php
* 作者:西蒙·贾维斯
* 版权所有:2005 西蒙·贾维斯
* 日期:2005 年 10 月 12 日
* 链接:http://www.white-hat-web-design.co.uk/articles/php-currency-conversion.php
*
* 本程序是免费软件;您可以重新分发它和/或
* 根据 GNU 通用公共许可证的条款进行修改
* 由自由软件基金会发布;任一版本 2
* 许可证,或(由您选择)任何更高版本。
*
* 这个程序发布是为了希望它会有用,
* 但不提供任何保证;甚至没有
的默示保证
* 适销性或特定用途的适用性。参见
* GNU 通用公共许可证了解更多详细信息:
* http://www.gnu.org/licenses/gpl.html
*
*/
货币转换器类 {
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();
//加载货币汇率
函数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'];
}
}
/* 执行实际转换,默认为 £1.00 英镑兑换美元 */
函数转换($金额= 1,$ from =“英镑”,$ to =“美元”,$小数= 2){
return(number_format(($amount/$this->exchange_rates[$from])*$this->exchange_rates[$to],$decimals));
}
/* 检查数据上次更新以来的时间 */
函数 checkLastUpdated() {
$conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);
$rs = mysql_select_db($this->mysql_db,$conn);
$sql = "从 ".$this->mysql_db." 显示表状态。" LIKE '".$this->mysql_table."'";
$rs = mysql_query($sql,$conn);
if(mysql_num_rows($rs) == 0 ) {
$this->createTable();
} 其他 {
$row = mysql_fetch_array($rs);
if(time() > (strtotime($row["Update_time"]) (12*60*60)) ) {
$this->downloadExchangeRates();
}
}
}
/* 下载 xml 文件,提取汇率并将值存储在数据库中 */
函数 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 .= "主机:".$currency_domain."rn";
$out .= "用户代理: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5rn";
$out .= "连接:关闭";
fwrite($fp, $out);
while (!feof($fp)) {
$buffer .= fgets($fp, 128);
}
fclose($fp);
$pattern = "{
preg_match_all($pattern,$buffer,$xml_rates);
array_shift($xml_rates);
for($i=0;$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." WHEREcurrency='".$currency."'";
$rs = mysql_query($sql,$conn) 或 die(mysql_error());
if(mysql_num_rows($rs) > 0) {
$sql = "UPDATE ".$this->mysql_table." SET rates=".$rate." WHEREcurrency='".$currency."'";
} 其他 {
$sql = "INSERT INTO ".$this->mysql_table." VALUES('".$currency."',".$rate.")";
}
$rs = mysql_query($sql,$conn) 或 die(mysql_error());
}
}
}
}
/* 创建货币兑换表 */
函数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) 或 die(mysql_error());
$sql = "INSERT INTO ".$this->mysql_table." VALUES('EUR',1)";
$rs = mysql_query($sql,$conn) 或 die(mysql_error());
$this->downloadExchangeRates();
}
}
?>
复制代码代码如下:
包括('货币转换器.php');
$x = newCurrencyConverter('your_host','your_username','your_password','your_database_name','your_table_name');
echo $x->convert(2.50,'英镑','美元');
?>
希望本文对大家的php程序设计有所帮助。