php专区

 首页 > php专区 > PHP应用 > php类库 > PHP货币换算程序代码 - php类库

PHP货币换算程序代码 - php类库

分享到:
【字体:
导读:
          ?php **File:CurrencyConverter php*Author:SimonJarvis*Copyright:2005SimonJarvis*Date:10 12 05*Link:http: www white-hat-web-design...

PHP货币换算程序代码

  1.   
  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. * 
  10. * This program is free software; you can redistribute it and/or 
  11. * modify it under the terms of the GNU General Public License 
  12. * as published by the Free Software Foundation; either version 2 
  13. * of the License, or (at your option) any later version. 
  14. * 
  15. * This program is distributed in the hope that it will be useful, 
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  18. * GNU General Public License for more details: 
  19. * http://www.gnu.org/licenses/gpl.html 
  20. * 
  21. */ 
  22.   
  23. class CurrencyConverter { 
  24.   
  25.    var $xml_file = "www.ecb.int/stats/eurofxref/eurofxref-daily.xml"
  26.    var $mysql_host$mysql_user$mysql_pass$mysql_db$mysql_table
  27.    var $exchange_rates = array(); 
  28.   
  29.    //Load Currency Rates 
  30.   
  31.    function CurrencyConverter($host,$user,$pass,$db,$tb) { 
  32.   
  33.       $this->mysql_host = $host
  34.       $this->mysql_user = $user
  35.       $this->mysql_pass = $pass
  36.       $this->mysql_db = $db
  37.       $this->mysql_table = $tb
  38.   
  39.       $this->checkLastUpdated(); 
  40.   
  41.       $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass); 
  42.   
  43.       $rs = mysql_select_db($this->mysql_db,$conn); 
  44.   
  45.       $sql = "SELECT * FROM ".$this->mysql_table; 
  46.   
  47.       $rs =  mysql_query($sql,$conn); 
  48.   
  49.       while($row = mysql_fetch_array($rs)) { 
  50.   
  51.          $this->exchange_rates[$row['currency']] = $row['rate']; 
  52.       } 
  53.   
  54.    } 
  55.   
  56.    /* Perform the actual conversion, defaults to £1.00 GBP to USD */ 
  57.    function convert($amount=1,$from="GBP",$to="USD",$decimals=2) { 
  58.   
  59.       return(number_format(($amount/$this->exchange_rates[$from])*$this->exchange_rates[$to],$decimals)); 
  60.    } 
  61.   
  62.    /* Check to see how long since the data was last updated */ 
  63.    function checkLastUpdated() { 
  64.       $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass); 
  65.   
  66.       $rs = mysql_select_db($this->mysql_db,$conn); 
  67.   
  68.       $sql = "SHOW TABLE STATUS FROM ".$this->mysql_db." LIKE '".$this->mysql_table."'"
  69.   
  70.       $rs =  mysql_query($sql,$conn); 
  71.   
  72.       if(mysql_num_rows($rs) == 0 ) { 
  73.   
  74.          $this->createTable(); 
  75.       } else { 
  76.          $row = mysql_fetch_array($rs); 
  77.          if(time() > (strtotime($row["Update_time"])+(12*60*60)) ) { 
  78.   
  79.             $this->downloadExchangeRates(); 
  80.          } 
  81.       } 
  82.    } 
  83.   
  84.    /* Download xml file, extract exchange rates and store values in database */ 
  85.   
  86.    function downloadExchangeRates() { 
  87.       $currency_domain = substr($this->xml_file,0,strpos($this->xml_file,"/")); 
  88.       $currency_file = substr($this->xml_file,strpos($this->xml_file,"/")); 
  89.       $fp = @fsockopen($currency_domain, 80, $errno$errstr, 10); 
  90.       if($fp) { 
  91.   
  92.          $out = "GET ".$currency_file." HTTP/1.1rn"
  93.          $out .= "Host: ".$currency_domain."rn"
  94.          $out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5rn"
  95.          $out .= "Connection: Closernrn"
  96.          fwrite($fp$out); 
  97.          while (!feof($fp)) { 
  98.   
  99.             $buffer .= fgets($fp, 128); 
  100.          } 
  101.          fclose($fp); 
  102.   
  103.          $pattern = "{}is"
  104.          preg_match_all($pattern,$buffer,$xml_rates); 
  105.          array_shift($xml_rates); 
  106.   
  107.          for($i=0;$i<count($xml_rates[0]);$i++) { 
  108.   
  109.             $exchange_rate[$xml_rates[0][$i]] = $xml_rates[1][$i]; 
  110.          } 
  111.   
  112.          $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass); 
  113.   
  114.          $rs = mysql_select_db($this->mysql_db,$conn); 
  115.   
  116.          foreach($exchange_rate as $currency=>$rate) { 
  117.   
  118.             if((is_numeric($rate)) && ($rate != 0)) { 
  119.   
  120.                $sql = "SELECT * FROM ".$this->mysql_table." WHERE currency='".$currency."'"
  121.                $rs =  mysql_query($sql,$connor die(mysql_error()); 
  122.                if(mysql_num_rows($rs) > 0) { 
  123.   
  124.                   $sql = "UPDATE ".$this->mysql_table." SET rate=".$rate." WHERE currency='".$currency."'"
  125.                } else { 
  126.   
  127.                   $sql = "INSERT INTO ".$this->mysql_table." VALUES('".$currency."',".$rate.")"
  128.                } 
  129.   
  130.                $rs =  mysql_query($sql,$connor die(mysql_error()); 
  131.             } 
  132.   
  133.          } 
  134.       } 
  135.    } 
  136.   
  137.    /* Create the currency exchange table */ 
  138.    function createTable() { 
  139.   
  140.       $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass); 
  141.   
  142.       $rs = mysql_select_db($this->mysql_db,$conn); 
  143.   
  144.       $sql = "CREATE TABLE ".$this->mysql_table." ( currency char(3) NOT NULL default '', rate float NOT NULL default '0', PRIMARY KEY(currency) ) ENGINE=MyISAM"
  145.   
  146.       $rs =  mysql_query($sql,$connor die(mysql_error()); 
  147.   
  148.       $sql = "INSERT INTO ".$this->mysql_table." VALUES('EUR',1)"
  149.   
  150.       $rs =  mysql_query($sql,$connor die(mysql_error()); 
  151.   
  152.       $this->downloadExchangeRates(); 
  153.    } 
  154.   
  155.   
  156. ?> 

 

上面的代码复制到一个新文件并将其保存为CurrencyConverter.php。当你需要转换包含类文件,称为“转换”功能。你需要输入自己的mysql数据库变量如登录详细信息。下面的例子将£2.50英镑转换成美元(美元)。

  1.    include('CurrencyConverter.php'); 
  2.    $x = new CurrencyConverter('your_host','your_username','your_password','your_database_name','your_table_name'); 
  3.    echo $x->convert(2.50,'GBP','USD'); 
  4. ?>
分享到:
php 递归json的类代码 - php类库
php 递归json的类代码  
php树形结构数据存取实例类 - php类库
php树形结构数据存取实例类
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……