PHP货币换算程序代码

  1. <?php
  2. /*
  3. * File: CurrencyConverter.php
  4. * Author: Simon Jarvis
  5. * Copyright: 2005 Simon Jarvis
  6. * Date: 10/12/05
  7. * Link: http://www.white-hat-web-design.co.uk/articles/php-currency-conversion.php
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details:
  18. * http://www.gnu.org/licenses/gpl.html
  19. *
  20. */
  21. class CurrencyConverter {
  22. var $xml_file = "www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
  23. var $mysql_host, $mysql_user, $mysql_pass, $mysql_db, $mysql_table;
  24. var $exchange_rates = array();
  25. //Load Currency Rates
  26. function CurrencyConverter($host,$user,$pass,$db,$tb) {
  27. $this->mysql_host = $host;
  28. $this->mysql_user = $user;
  29. $this->mysql_pass = $pass;
  30. $this->mysql_db = $db;
  31. $this->mysql_table = $tb;
  32. $this->checkLastUpdated();
  33. $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);
  34. $rs = mysql_select_db($this->mysql_db,$conn);
  35. $sql = "SELECT * FROM ".$this->mysql_table;
  36. $rs = mysql_query($sql,$conn);
  37. while($row = mysql_fetch_array($rs)) {
  38. $this->exchange_rates[$row['currency']] = $row['rate'];
  39. }
  40. }
  41. /* Perform the actual conversion, defaults to £1.00 GBP to USD */
  42. function convert($amount=1,$from="GBP",$to="USD",$decimals=2) {
  43. return(number_format(($amount/$this->exchange_rates[$from])*$this->exchange_rates[$to],$decimals));
  44. }
  45. /* Check to see how long since the data was last updated */
  46. function checkLastUpdated() {
  47. $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);
  48. $rs = mysql_select_db($this->mysql_db,$conn);
  49. $sql = "SHOW TABLE STATUS FROM ".$this->mysql_db." LIKE '".$this->mysql_table."'";
  50. $rs = mysql_query($sql,$conn);
  51. if(mysql_num_rows($rs) == 0 ) {
  52. $this->createTable();
  53. } else {
  54. $row = mysql_fetch_array($rs);
  55. if(time() > (strtotime($row["Update_time"])+(12*60*60)) ) {
  56. $this->downloadExchangeRates();
  57. }
  58. }
  59. }
  60. /* Download xml file, extract exchange rates and store values in database */
  61. function downloadExchangeRates() {
  62. $currency_domain = substr($this->xml_file,0,strpos($this->xml_file,"/"));
  63. $currency_file = substr($this->xml_file,strpos($this->xml_file,"/"));
  64. $fp = @fsockopen($currency_domain, 80, $errno, $errstr, 10);
  65. if($fp) {
  66. $out = "GET ".$currency_file." HTTP/1.1rn";
  67. $out .= "Host: ".$currency_domain."rn";
  68. $out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5rn";
  69. $out .= "Connection: Closernrn";
  70. fwrite($fp, $out);
  71. while (!feof($fp)) {
  72. $buffer .= fgets($fp, 128);
  73. }
  74. fclose($fp);
  75. $pattern = "{<Cubes*currency='(w*)'s*rate='([d.]*)'/>}is";
  76. preg_match_all($pattern,$buffer,$xml_rates);
  77. array_shift($xml_rates);
  78. for($i=0;$i<count($xml_rates[0]);$i++) {
  79. $exchange_rate[$xml_rates[0][$i]] = $xml_rates[1][$i];
  80. }
  81. $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);
  82. $rs = mysql_select_db($this->mysql_db,$conn);
  83. foreach($exchange_rate as $currency=>$rate) {
  84. if((is_numeric($rate)) && ($rate != 0)) {
  85. $sql = "SELECT * FROM ".$this->mysql_table." WHERE currency='".$currency."'";
  86. $rs = mysql_query($sql,$conn) or die(mysql_error());
  87. if(mysql_num_rows($rs) > 0) {
  88. $sql = "UPDATE ".$this->mysql_table." SET rate=".$rate." WHERE currency='".$currency."'";
  89. } else {
  90. $sql = "INSERT INTO ".$this->mysql_table." VALUES('".$currency."',".$rate.")";
  91. }
  92. $rs = mysql_query($sql,$conn) or die(mysql_error());
  93. }
  94. }
  95. }
  96. }
  97. /* Create the currency exchange table */
  98. function createTable() {
  99. $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);
  100. $rs = mysql_select_db($this->mysql_db,$conn);
  101. $sql = "CREATE TABLE ".$this->mysql_table." ( currency char(3) NOT NULL default '', rate float NOT NULL default '0', PRIMARY KEY(currency) ) ENGINE=MyISAM";
  102. $rs = mysql_query($sql,$conn) or die(mysql_error());
  103. $sql = "INSERT INTO ".$this->mysql_table." VALUES('EUR',1)";
  104. $rs = mysql_query($sql,$conn) or die(mysql_error());
  105. $this->downloadExchangeRates();
  106. }
  107. }
  108. ?>
上面的代码复制到一个新文件并将其保存为CurrencyConverter.php。当你需要转换包含类文件,称为“转换”功能。你需要输入自己的mysql数据库变量如登录详细信息。下面的例子将£2.50英镑转换成美元(美元)。
  1. <?php
  2. include('CurrencyConverter.php');
  3. $x = new CurrencyConverter('your_host','your_username','your_password','your_database_name','your_table_name');
  4. echo $x->convert(2.50,'GBP','USD');
  5. ?>