PHP currency conversion method

Source: Internet
Author: User
This article mainly introduces the method of currency conversion in PHP, describes the implementation method of currency conversion in details in the form of examples, and illustrates the specific usage by taking the pound into the dollar as an example, for more information, see

This article mainly introduces the method of currency conversion in PHP, describes the implementation method of currency conversion in details in the form of examples, and illustrates the specific usage by taking the pound into the dollar as an example, for more information, see

This article describes how to use PHP to convert currencies. Share it with you for your reference.

The specific implementation code is as follows:

The Code is as follows:

<? Php
/*
* File: CurrencyConverter. php
* Author: Simon Jarvis
* Copyright: 2005 Simon Jarvis
* Date: 10/12/05
* Link:
*
* 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:
*
*
*/
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 ();
}
}
?>


Copy the above Code to a new file and save it as CurrencyConverter. php. When you need to convert a class file, it is called the "Conversion" function. You need to enter your mysql database variables such as logon details. The following example converts the £ 2.50 to $ ).

The Code is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.