货币换算 Currency conversion using php and Google calculator api
?
Google is a wonderful ! While most of us use it for searching the web for information , few are aware that it has an inbuilt calculator which can also be used for converting a currency into another . Doing so is really simple , you just type ” AMOUNT CURRENCY_CONVERTING_FROM in CURRENCY_CONVERTING_TO ” and search , the inbuilt calculator will give you the result .For example , if one wants to convert 1 U.S Dollar into Euro , he shall type “1 USD in EUR” .
?
Now lets get to the topic of this post , Google also has a secret calculator API ( http://www.google.com/ig/calculator ) that is usually used for iGoogle gadgets , but since its free and open , anyone can use it .
?
So if you want to convert a currency into another using the API , your callback URL would look like :
?
?
?
www.google.com/ig/calculator?hl=en&q=AMOUNT-FROM_CURRENCY=?TO_CURRENCY
// 1 USD in EUR example
www.google.com/ig/calculator?hl=en&q=1USD=?EUR
?
?
?
In the above example , you get a string which looks like "{lhs: "1 U.S. dollar",rhs: "0.838574423 Euros",error: "",icc: true} " . You can easily explode() the string using php and extract the required data .
?
To make things easier for you , here is a simple php function that converts a currency into another using the Google calculator API . This example uses cURL , so you have to have it enabled for this code to work .
?
?
To make things easier for you , here is a simple php function that converts a currency into another using the Google calculator API . This example uses cURL , so you have to have it enabled for this code to work .
?
?
<?php
function currency($from_Currency,$to_Currency,$amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$data = explode('"', $rawdata);
$data = explode(' ', $data[3]);
$var = $data[0];
return round($var,3);
}
?>
?
?
?
The value returned by Google usually has 9 decimal places , you can round it off to a no. of digits after decimal of you choice using the round() function . By default the above function rounds the value to 3 digits after decimal .
?
Now here is how you can use the function in your scripts
?
?
?
<?php
//This will display current value of 1 USD in CNY ( China )
echo currency("USD","CNY",1);
?>
?
?
?
Hope this has helped you with converting currencies using php on your website
?
?
?
来源:?http://www.xe.com/ucc/full/?obsolete=yes
?
附:货币
?
Afghanistan, Afghani (AFN)
Albania, Lek (ALL)
Algeria, Dinar (DZD)
America (United States), Dollar (USD)
American Samoa, United States Dollar (USD)
Andorra, Euro (EUR)
Angola, Kwanza (AOA)
Anguilla, East Caribbean Dollar (XCD)
Antigua and Barbuda, East Caribbean Dollar (XCD)
Argentina, Peso (ARS)
Armenia, Dram (AMD)
Aruba, Guilder (AWG)
Ashmore and Cartier Islands, Australia Dollar (AUD)
Australia, Dollar (AUD)
Austria, Euro (EUR)
Austria, Schilling [obsolete] (ATS)
Azerbaijan, Manat [obsolete] (AZM)
Azerbaijan, New Manat (AZN)
Azores, Euro (EUR)
Bahamas, Dollar (BSD)
Bahrain, Dinar (BHD)
Bajan (Barbados), Dollar (BBD)
Balearic Islands, Euro (EUR)
Bangladesh, Taka (BDT)
Barbados, Dollar (BBD)
Belarus, Ruble (BYR)
Belgium, Euro (EUR)
Belgium, Franc [obsolete] (BEF)
Belize, Dollar (BZD)
Benin, CFA Franc BCEAO (XOF)
Bermuda, Dollar (BMD)
Bhutan, India Rupee (INR)
Bhutan, Ngultrum (BTN)
Bolivia, Boliviano (BOB)
Bonaire, Netherlands Antilles Guilder (ANG)
Bosnia and Herzegovina, Convertible Marka (BAM)
Botswana, Pula (BWP)
Brazil, Real (BRL)
Britain (United Kingdom), Pound (GBP)
British Indian Ocean Territory, United Kingdom Pound (GBP)
British Indian Ocean Territory, United States Dollar (USD)
British Virgin Islands, United States Dollar (USD)
Brunei, Dollar (BND)
Brunei, Singapore Dollar (SGD)
Bulgaria, Lev (BGN)
Burkina Faso, CFA Franc BCEAO (XOF)
Burma (Myanmar), Kyat (MMK)
Burundi, Franc (BIF)
Cambodia, Riel (KHR)
Cameroon, CFA Franc BEAC (XAF)
Canada, Dollar (CAD)
Canary Islands, Euro (EUR)
Cape Verde, Escudo (CVE)
Cayman Islands, Dollar (KYD)
Central African Republic, CFA Franc BEAC (XAF)
CFA Communauté Financière Africaine BEAC Franc (XAF)
CFA Communauté Financière Africaine BCEAO Franc (XOF)
Chad, CFA Franc BEAC (XAF)
Chile, Peso (CLP)
China, Yuan Renminbi (CNY)
Christmas Island, Australia Dollar (AUD)
Cocos (Keeling Islands, Australia Dollar (AUD)
Colombia, Peso (COP)
Communauté Financière Africaine BEAC Franc (XAF)
Communauté Financière Africaine BCEAO Franc (XOF)
Comoros, Franc (KMF)
Comptoirs Fran?ais du Pacifique Franc (XPF)
Congo/Brazzaville, CFA Franc BEAC (XAF)
Congo/Kinshasa, Franc (CDF)
Cook Islands, New Zealand Dollar (NZD)
Coral Sea Islands, Australia Dollar (AUD)
Costa Rica, Colon (CRC)
C?te d'Ivoire, CFA Franc BCEAO (XOF)
Croatia, Kuna (HRK)
Cuba, Convertible Peso (CUC)
Cuba, Peso (CUP)
Cura?o, Netherlands Antilles Guilder (ANG)
Cyprus, Euro (EUR)
Cyprus, Pound [obsolete] (CYP)
Czech Republic, Koruna (CZK)
Denmark, Krone (DKK)
Djibouti, Franc (DJF)
Dominica, East Caribbean Dollar (XCD)
Dominican Republic, Peso (DOP)
Dutch (Netherlands), Euro (EUR)
Dutch (Netherlands), Guilder [obsolete] (NLG)
East Caribbean Dollar (XCD)
East Timor, United States Dollar (USD)
Ecuador, United States Dollar (USD)
Egypt, Pound (EGP)
El Salvador, Colon (SVC)
El Salvador, United States Dollar (USD)
England (United Kingdom), Pound (GBP)
Equatorial Guinea, CFA Franc BEAC (XAF)
Eritrea, Nakfa (ERN)
Estonia, Euro (EUR)
Estonia, Kroon [obsolete] (EEK)
Ethiopia, Birr (ETB)
Euro (EUR)
Europa Island, Euro (EUR)
Falkland Islands, Pound (FKP)
Faroe Islands, Denmark Krone (DKK)
Fiji, Dollar (FJD)
Finland, Euro (EUR)
Finland, Markka [obsolete] (FIM)
France, Euro (EUR)
France, Franc [obsolete] (FRF)
French Guiana, Euro (EUR)
French Polynesia, Comptoirs Fran?ais du Pacifique Franc (XPF)
French Southern and Antarctic Lands, Euro (EUR)
Gabon, CFA Franc BEAC (XAF)
Gambia, Dalasi (GMD)
Gaza Strip, Israel New Shekel (ILS)
Georgia, Lari (GEL)
Germany, Deutsche Mark [obsolete] (DEM)
Germany, Euro (EUR)
Ghana, Cedi [obsolete] (GHC)
Ghana, Cedi (GHS)
Gibraltar, Pound (GIP)
Gold Ounce (XAU)
Great Britain (United Kingdom), Pound (GBP)
Greece, Euro (EUR)
Greece, Drachma [obsolete] (GRD)
Greenland, Denmark Krone (DKK)
Grenada, East Caribbean Dollar (XCD)
Guadeloupe, Euro (EUR)
Guam, United States Dollar (USD)
Guatemala, Quetzal (GTQ)
Guernsey, Pound (GGP)
Guinea, Franc (GNF)
Guinea-Bissau, CFA Franc BCEAO (XOF)
Guyana, Dollar (GYD)
Haiti, Gourde (HTG)
Holland (Netherlands), Euro (EUR)
Holland (Netherlands), Guilder [obsolete] (NLG)
Holy See (Vatican City), Euro (EUR)
Holy See (Vatican City), Lira [obsolete] (VAL)
Honduras, Lempira (HNL)
Hong Kong, Dollar (HKD)
Hungary, Forint (HUF)
Iceland, Krona (ISK)
India, Rupee (INR)
Indonesia, Rupiah (IDR)
International Monetary Fund Special Drawing Right (XDR)
Iran, Rial (IRR)
Iraq, Dinar (IQD)
Ireland, Euro (EUR)
Ireland, Pound [obsolete] (IEP)
Islas Malvinas (Falkland Islands), Pound (FKP)
Isle of Man, Pound (IMP)
Israel, New Shekel (ILS)
Italy, Euro (EUR)
Italy, Lira [obsolete] (ITL)
Ivory Coast (C?te d'Ivoire), CFA Franc BCEAO (XOF)
Jamaica, Dollar (JMD)
Japan, Yen (JPY)
Jersey, Pound (JEP)
Johnson, United States Dollar (USD)
Jordan, Dinar (JOD)
Juan de Nova, Euro (EUR)
Kazakhstan, Tenge (KZT)
Kenya, Shilling (KES)
Kiribati, Australia Dollar (AUD)
Kuwait, Dinar (KWD)
Kyrgyzstan, Som (KGS)
Laos, Kip (LAK)
Latvia, Lat (LVL)
Lebanon, Pound (LBP)
Lesotho, Loti (LSL)
Liberia, Dollar (LRD)
Libya, Dinar (LYD)
Liechtenstein, Switzerland Franc (CHF)
Lithuania, Litas (LTL)
Luxembourg, Euro (EUR)
Luxembourg, Franc [obsolete] (LUF)
Macau, Pataca (MOP)
Macedonia, Denar (MKD)
Madagascar, Ariary (MGA)
Madagascar, Franc [obsolete] (MGF)
Madeira Islands, Euro (EUR)
Malawi, Kwacha (MWK)
Malaysia, Ringgit (MYR)
Maldives, Rufiyaa (MVR)
Mali, CFA Franc BCEAO (XOF)
Malta, Euro (EUR)
Malta, Lira [obsolete] (MTL)
Malvinas (Falkland Islands), Pound (FKP)
Marshall Islands, United States Dollar (USD)
Martinique, Euro (EUR)
Mauritania, Ouguiya (MRO)
Mauritius, Rupee (MUR)
Mayotte, Euro (EUR)
Mexico, Peso (MXN)
Micronesia, United States Dollar (USD)
Midway Islands, United States Dollar (USD)
Moldova, Leu (MDL)
Monaco, Euro (EUR)
Mongolia, Tughrik (MNT)
Montenegro, Euro (EUR)
Montserrat, East Caribbean Dollar (XCD)
Morocco, Dirham (MAD)
Mozambique, Metical (MZN)
Mozambique, Metical [obsolete] (MZM)
Myanmar (Burma), Kyat (MMK)
Namibia, Dollar (NAD)
Nauru, Australia Dollar (AUD)
Navassa, Haiti Gourde (HTG)
Navassa, United States Dollar (USD)
Nepal, Rupee (NPR)
Netherlands Antilles, Guilder (ANG)
Netherlands, Euro (EUR)
Netherlands, Guilder [obsolete] (NLG)
New Caledonia, Comptoirs Fran?ais du Pacifique Franc (XPF)
New Zealand, Dollar (NZD)
Nicaragua, Cordoba (NIO)
Niger, CFA Franc BCEAO (XOF)
Nigeria, Naira (NGN)
Niue, New Zealand Dollar (NZD)
Norfolk Island, Australia Dollar (AUD)
North Korea, Won (KPW)
Northern Mariana Islands, United States Dollar (USD)
Norway, Krone (NOK)
Oman, Rial (OMR)
Pakistan, Rupee (PKR)
Palau, United States Dollar (USD)
Palladium Ounce (XPD)
Panama, Balboa (PAB)
Panama, United States Dollar (USD)
Papua New Guinea, Kina (PGK)
Paracel Islands, China Yuan Renminbi (CNY)
Paracel Islands, Vietnam Dong (VND)
Paraguay, Guarani (PYG)
Peru, Nuevo Sol (PEN)
Philippines, Peso (PHP)
Pitcairn, New Zealand Dollar (NZD)
Platinum Ounce (XPT)
Poland, Zloty (PLN)
Portugal, Euro (EUR)
Portugal, Escudo [obsolete] (PTE)
Puerto Rico, United States Dollar (USD)
Qatar, Riyal (QAR)
Réunion, Euro (EUR)
Romania, New Leu (RON)
Romania, Leu [obsolete] (ROL)
Russia, Ruble (RUB)
Rwanda, Franc (RWF)
Saba, Netherlands Antilles Guilder (ANG)
Saint Helena, Pound (SHP)
Saint Kitts and Nevis, East Caribbean Dollar (XCD)
Saint Lucia, East Caribbean Dollar (XCD)
Saint Pierre and Miquelon, Euro (EUR)
Saint Vincent and The Grenadines, East Caribbean Dollar (XCD)
Saint-Martin, Euro (EUR)
Samoa, Tala (WST)
San Marino, Euro (EUR)
S?o Tome and Principe, Dobra (STD)
Saudi Arabia, Riyal (SAR)
Scotland (United Kingdom), Pound (GBP)
Seborga, Luigino (SPL)
Senegal, CFA Franc BCEAO (XOF)
Serbia, Dinar (RSD)
Seychelles, Rupee (SCR)
Sierra Leone, Leone (SLL)
Silver Ounce (XAG)
Singapore, Dollar (SGD)
Sint Eustatius, Netherlands Antilles Guilder (ANG)
Sint Maarten, Netherlands Antilles Guilder (ANG)
Slovakia, Euro (EUR)
Slovakia, Koruna [obsolete] (SKK)
Slovenia, Euro (EUR)
Slovenia, Tolar [obsolete] (SIT)
Solomon Islands, Dollar (SBD)
Somalia, Shilling (SOS)
South Africa, Rand (ZAR)
South Georgia, United Kingdom Pound (GBP)
South Korea, Won (KRW)
South Sandwich Islands, United Kingdom Pound (GBP)
Spain, Euro (EUR)
Spain, Peseta [obsolete] (ESP)
Sri Lanka, Rupee (LKR)
Sudan, Dinar [obsolete] (SDD)
Sudan, Pound (SDG)
Suriname, Dollar (SRD)
Suriname, Guilder (SRG) [obsolete]
Svalbard and Jan Mayen, Norway Krone (NOK)
Swaziland, Lilangeni (SZL)
Swaziland, South Africa Rand (ZAR)
Sweden, Krona (SEK)
Switzerland, Franc (CHF)
Syria, Pound (SYP)
Taiwan, New Dollar (TWD)
Tajikistan, Somoni (TJS)
Tajikistan, Russia Ruble (RUB)
Tanzania, Shilling (TZS)
Thailand, Baht (THB)
Togo, CFA Franc BCEAO (XOF)
Tokelau, New Zealand Dollar (NZD)
Tonga, Pa'anga (TOP)
Transnistria, Moldova Leu (MDL)
Trinidad and Tobago, Dollar (TTD)
Tunisia, Dinar (TND)
Turkey, Lira (TRY)
Turkey, Lira (TRL) [obsolete]
Turkmenistan, New Manat (TMT)
Turkmenistan, Manat (TMM)
Turks and Caicos Islands, United States Dollar (USD)
Tuvalu, Australia Dollar (AUD)
Tuvalu, Dollar (TVD)
Uganda, Shilling (UGX)
Ukraine, Hryvna (UAH)
United Arab Emirates, Dirham (AED)
United Kingdom, Pound (GBP)
United States, Dollar (USD)
Uruguay, Peso (UYU)
Uzbekistan, Som (UZS)
Vanuatu, Vatu (VUV)
Vatican City, Euro (EUR)
Vatican City, Lira [obsolete] (VAL)
Venezuela, Bolivar Fuerte (VEF)
Venezuela, Bolivar (VEB) [obsolete]
Vietnam, Dong (VND)
Virgin Islands, United States Dollar (USD)
Wake Island, United States Dollar (USD)
Wallis and Futuna Islands, Comptoirs Fran?ais du Pacifique Franc (XPF)
West Bank, Israel New Shekel (ILS)
West Bank, Jordan Dinar (JOD)
Western Sahara, Morocco Dirham (MAD)
Samoa, Tala (WST)
Yemen, Rial (YER)
Zambia, Kwacha (ZMK)
Zimbabwe, Dollar (ZWD)
?
?
?
?
?
?
?
?