货币换算 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 examplewww.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 .
?
?
<?phpfunction 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
?
附:货币
?
<select name="From" size="10" tabindex="2"><option selected="selected" value="AFN">Afghanistan, Afghani (AFN)</option><option value="ALL">Albania, Lek (ALL)</option><option value="DZD">Algeria, Dinar (DZD)</option><option value="USD">America (United States), Dollar (USD)</option><option value="USD">American Samoa, United States Dollar (USD)</option><option value="EUR">Andorra, Euro (EUR)</option><option value="AOA">Angola, Kwanza (AOA)</option><option value="XCD">Anguilla, East Caribbean Dollar (XCD)</option><option value="XCD">Antigua and Barbuda, East Caribbean Dollar (XCD)</option><option value="ARS">Argentina, Peso (ARS)</option><option value="AMD">Armenia, Dram (AMD)</option><option value="AWG">Aruba, Guilder (AWG)</option><option value="AUD">Ashmore and Cartier Islands, Australia Dollar (AUD)</option><option value="AUD">Australia, Dollar (AUD)</option><option value="EUR">Austria, Euro (EUR)</option><option value="ATS">Austria, Schilling [obsolete] (ATS)</option> <option value="AZM">Azerbaijan, Manat [obsolete] (AZM)</option><option value="AZN">Azerbaijan, New Manat (AZN)</option><option value="EUR">Azores, Euro (EUR)</option><option value="BSD">Bahamas, Dollar (BSD)</option><option value="BHD">Bahrain, Dinar (BHD)</option><option value="BBD">Bajan (Barbados), Dollar (BBD)</option><option value="EUR">Balearic Islands, Euro (EUR)</option><option value="BDT">Bangladesh, Taka (BDT)</option><option value="BBD">Barbados, Dollar (BBD)</option><option value="BYR">Belarus, Ruble (BYR)</option><option value="EUR">Belgium, Euro (EUR)</option><option value="BEF">Belgium, Franc [obsolete] (BEF)</option><option value="BZD">Belize, Dollar (BZD)</option><option value="XOF">Benin, CFA Franc BCEAO (XOF)</option><option value="BMD">Bermuda, Dollar (BMD)</option><option value="INR">Bhutan, India Rupee (INR)</option><option value="BTN">Bhutan, Ngultrum (BTN)</option><option value="BOB">Bolivia, Boliviano (BOB)</option><option value="ANG">Bonaire, Netherlands Antilles Guilder (ANG)</option><option value="BAM">Bosnia and Herzegovina, Convertible Marka (BAM)</option><option value="BWP">Botswana, Pula (BWP)</option><option value="BRL">Brazil, Real (BRL)</option><option value="GBP">Britain (United Kingdom), Pound (GBP)</option><option value="GBP">British Indian Ocean Territory, United Kingdom Pound (GBP)</option><option value="USD">British Indian Ocean Territory, United States Dollar (USD)</option><option value="USD">British Virgin Islands, United States Dollar (USD)</option><option value="BND">Brunei, Dollar (BND)</option><option value="SGD">Brunei, Singapore Dollar (SGD)</option><option value="BGN">Bulgaria, Lev (BGN)</option><option value="XOF">Burkina Faso, CFA Franc BCEAO (XOF)</option><option value="MMK">Burma (Myanmar), Kyat (MMK)</option><option value="BIF">Burundi, Franc (BIF)</option><option value="KHR">Cambodia, Riel (KHR)</option><option value="XAF">Cameroon, CFA Franc BEAC (XAF)</option><option value="CAD">Canada, Dollar (CAD)</option><option value="EUR">Canary Islands, Euro (EUR)</option><option value="CVE">Cape Verde, Escudo (CVE)</option><option value="KYD">Cayman Islands, Dollar (KYD)</option><option value="XAF">Central African Republic, CFA Franc BEAC (XAF)</option><option value="XAF">CFA Communauté Financière Africaine BEAC Franc (XAF)</option><option value="XOF">CFA Communauté Financière Africaine BCEAO Franc (XOF)</option><option value="XAF">Chad, CFA Franc BEAC (XAF)</option><option value="CLP">Chile, Peso (CLP)</option><option value="CNY">China, Yuan Renminbi (CNY)</option><option value="AUD">Christmas Island, Australia Dollar (AUD)</option><option value="AUD">Cocos (Keeling Islands, Australia Dollar (AUD)</option><option value="COP">Colombia, Peso (COP)</option><option value="XAF">Communauté Financière Africaine BEAC Franc (XAF)</option><option value="XOF">Communauté Financière Africaine BCEAO Franc (XOF)</option><option value="KMF">Comoros, Franc (KMF)</option><option value="XPF">Comptoirs Fran?ais du Pacifique Franc (XPF)</option><option value="XAF">Congo/Brazzaville, CFA Franc BEAC (XAF)</option><option value="CDF">Congo/Kinshasa, Franc (CDF)</option><option value="NZD">Cook Islands, New Zealand Dollar (NZD)</option><option value="AUD">Coral Sea Islands, Australia Dollar (AUD)</option><option value="CRC">Costa Rica, Colon (CRC)</option><option value="XOF">C?te d'Ivoire, CFA Franc BCEAO (XOF)</option><option value="HRK">Croatia, Kuna (HRK)</option><option value="CUC">Cuba, Convertible Peso (CUC)</option><option value="CUP">Cuba, Peso (CUP)</option><option value="ANG">Cura?o, Netherlands Antilles Guilder (ANG)</option><option value="EUR">Cyprus, Euro (EUR)</option><option value="CYP">Cyprus, Pound [obsolete] (CYP)</option><option value="CZK">Czech Republic, Koruna (CZK)</option><option value="DKK">Denmark, Krone (DKK)</option><option value="DJF">Djibouti, Franc (DJF)</option><option value="XCD">Dominica, East Caribbean Dollar (XCD)</option><option value="DOP">Dominican Republic, Peso (DOP)</option><option value="EUR">Dutch (Netherlands), Euro (EUR)</option><option value="NLG">Dutch (Netherlands), Guilder [obsolete] (NLG)</option><option value="XCD">East Caribbean Dollar (XCD)</option><option value="USD">East Timor, United States Dollar (USD)</option><option value="USD">Ecuador, United States Dollar (USD)</option><option value="EGP">Egypt, Pound (EGP)</option><option value="SVC">El Salvador, Colon (SVC)</option><option value="USD">El Salvador, United States Dollar (USD)</option><option value="GBP">England (United Kingdom), Pound (GBP)</option><option value="XAF">Equatorial Guinea, CFA Franc BEAC (XAF)</option><option value="ERN">Eritrea, Nakfa (ERN)</option><option value="EUR">Estonia, Euro (EUR)</option><option value="EEK">Estonia, Kroon [obsolete] (EEK)</option><option value="ETB">Ethiopia, Birr (ETB)</option><option value="EUR">Euro (EUR)</option><option value="EUR">Europa Island, Euro (EUR)</option><option value="FKP">Falkland Islands, Pound (FKP)</option><option value="DKK">Faroe Islands, Denmark Krone (DKK)</option><option value="FJD">Fiji, Dollar (FJD)</option><option value="EUR">Finland, Euro (EUR)</option><option value="FIM">Finland, Markka [obsolete] (FIM)</option><option value="EUR">France, Euro (EUR)</option><option value="FRF">France, Franc [obsolete] (FRF)</option><option value="EUR">French Guiana, Euro (EUR)</option><option value="XPF">French Polynesia, Comptoirs Fran?ais du Pacifique Franc (XPF)</option><option value="EUR">French Southern and Antarctic Lands, Euro (EUR)</option><option value="XAF">Gabon, CFA Franc BEAC (XAF)</option><option value="GMD">Gambia, Dalasi (GMD)</option><option value="ILS">Gaza Strip, Israel New Shekel (ILS)</option><option value="GEL">Georgia, Lari (GEL)</option><option value="DEM">Germany, Deutsche Mark [obsolete] (DEM)</option><option value="EUR">Germany, Euro (EUR)</option><option value="GHC">Ghana, Cedi [obsolete] (GHC)</option><option value="GHS">Ghana, Cedi (GHS)</option><option value="GIP">Gibraltar, Pound (GIP)</option><option value="XAU">Gold Ounce (XAU)</option><option value="GBP">Great Britain (United Kingdom), Pound (GBP)</option><option value="EUR">Greece, Euro (EUR)</option><option value="GRD">Greece, Drachma [obsolete] (GRD)</option> <option value="DKK">Greenland, Denmark Krone (DKK)</option><option value="XCD">Grenada, East Caribbean Dollar (XCD)</option><option value="EUR">Guadeloupe, Euro (EUR)</option><option value="USD">Guam, United States Dollar (USD)</option><option value="GTQ">Guatemala, Quetzal (GTQ)</option><option value="GGP">Guernsey, Pound (GGP)</option><option value="GNF">Guinea, Franc (GNF)</option><option value="XOF">Guinea-Bissau, CFA Franc BCEAO (XOF)</option><option value="GYD">Guyana, Dollar (GYD)</option><option value="HTG">Haiti, Gourde (HTG)</option><option value="EUR">Holland (Netherlands), Euro (EUR)</option><option value="NLG">Holland (Netherlands), Guilder [obsolete] (NLG)</option><option value="EUR">Holy See (Vatican City), Euro (EUR)</option><option value="VAL">Holy See (Vatican City), Lira [obsolete] (VAL)</option><option value="HNL">Honduras, Lempira (HNL)</option><option value="HKD">Hong Kong, Dollar (HKD)</option><option value="HUF">Hungary, Forint (HUF)</option><option value="ISK">Iceland, Krona (ISK)</option><option value="INR">India, Rupee (INR)</option><option value="IDR">Indonesia, Rupiah (IDR)</option><option value="XDR">International Monetary Fund Special Drawing Right (XDR)</option><option value="IRR">Iran, Rial (IRR)</option><option value="IQD">Iraq, Dinar (IQD)</option><option value="EUR">Ireland, Euro (EUR)</option><option value="IEP">Ireland, Pound [obsolete] (IEP)</option><option value="FKP">Islas Malvinas (Falkland Islands), Pound (FKP)</option><option value="IMP">Isle of Man, Pound (IMP)</option><option value="ILS">Israel, New Shekel (ILS)</option><option value="EUR">Italy, Euro (EUR)</option><option value="ITL">Italy, Lira [obsolete] (ITL)</option><option value="XOF">Ivory Coast (C?te d'Ivoire), CFA Franc BCEAO (XOF)</option><option value="JMD">Jamaica, Dollar (JMD)</option><option value="JPY">Japan, Yen (JPY)</option><option value="JEP">Jersey, Pound (JEP)</option><option value="USD">Johnson, United States Dollar (USD)</option><option value="JOD">Jordan, Dinar (JOD)</option><option value="EUR">Juan de Nova, Euro (EUR)</option><option value="KZT">Kazakhstan, Tenge (KZT)</option><option value="KES">Kenya, Shilling (KES)</option><option value="AUD">Kiribati, Australia Dollar (AUD)</option><option value="KWD">Kuwait, Dinar (KWD)</option><option value="KGS">Kyrgyzstan, Som (KGS)</option><option value="LAK">Laos, Kip (LAK)</option><option value="LVL">Latvia, Lat (LVL)</option><option value="LBP">Lebanon, Pound (LBP)</option><option value="LSL">Lesotho, Loti (LSL)</option><option value="LRD">Liberia, Dollar (LRD)</option><option value="LYD">Libya, Dinar (LYD)</option><option value="CHF">Liechtenstein, Switzerland Franc (CHF)</option><option value="LTL">Lithuania, Litas (LTL)</option><option value="EUR">Luxembourg, Euro (EUR)</option><option value="LUF">Luxembourg, Franc [obsolete] (LUF)</option><option value="MOP">Macau, Pataca (MOP)</option><option value="MKD">Macedonia, Denar (MKD)</option><option value="MGA">Madagascar, Ariary (MGA)</option><option value="MGF">Madagascar, Franc [obsolete] (MGF)</option><option value="EUR">Madeira Islands, Euro (EUR)</option><option value="MWK">Malawi, Kwacha (MWK)</option><option value="MYR">Malaysia, Ringgit (MYR)</option><option value="MVR">Maldives, Rufiyaa (MVR)</option><option value="XOF">Mali, CFA Franc BCEAO (XOF)</option><option value="EUR">Malta, Euro (EUR)</option><option value="MTL">Malta, Lira [obsolete] (MTL)</option><option value="FKP">Malvinas (Falkland Islands), Pound (FKP)</option><option value="USD">Marshall Islands, United States Dollar (USD)</option><option value="EUR">Martinique, Euro (EUR)</option><option value="MRO">Mauritania, Ouguiya (MRO)</option><option value="MUR">Mauritius, Rupee (MUR)</option><option value="EUR">Mayotte, Euro (EUR)</option><option value="MXN">Mexico, Peso (MXN)</option><option value="USD">Micronesia, United States Dollar (USD)</option><option value="USD">Midway Islands, United States Dollar (USD)</option><option value="MDL">Moldova, Leu (MDL)</option><option value="EUR">Monaco, Euro (EUR)</option><option value="MNT">Mongolia, Tughrik (MNT)</option><option value="EUR">Montenegro, Euro (EUR)</option><option value="XCD">Montserrat, East Caribbean Dollar (XCD)</option><option value="MAD">Morocco, Dirham (MAD)</option><option value="MZN">Mozambique, Metical (MZN)</option><option value="MZM">Mozambique, Metical [obsolete] (MZM)</option><option value="MMK">Myanmar (Burma), Kyat (MMK)</option><option value="NAD">Namibia, Dollar (NAD)</option><option value="AUD">Nauru, Australia Dollar (AUD)</option><option value="HTG">Navassa, Haiti Gourde (HTG)</option><option value="USD">Navassa, United States Dollar (USD)</option><option value="NPR">Nepal, Rupee (NPR)</option><option value="ANG">Netherlands Antilles, Guilder (ANG)</option><option value="EUR">Netherlands, Euro (EUR)</option><option value="NLG">Netherlands, Guilder [obsolete] (NLG)</option><option value="XPF">New Caledonia, Comptoirs Fran?ais du Pacifique Franc (XPF)</option><option value="NZD">New Zealand, Dollar (NZD)</option><option value="NIO">Nicaragua, Cordoba (NIO)</option><option value="XOF">Niger, CFA Franc BCEAO (XOF)</option><option value="NGN">Nigeria, Naira (NGN)</option><option value="NZD">Niue, New Zealand Dollar (NZD)</option><option value="AUD">Norfolk Island, Australia Dollar (AUD)</option><option value="KPW">North Korea, Won (KPW)</option><option value="USD">Northern Mariana Islands, United States Dollar (USD)</option><option value="NOK">Norway, Krone (NOK)</option><option value="OMR">Oman, Rial (OMR)</option><option value="PKR">Pakistan, Rupee (PKR)</option><option value="USD">Palau, United States Dollar (USD)</option><option value="XPD">Palladium Ounce (XPD)</option><option value="PAB">Panama, Balboa (PAB)</option><option value="USD">Panama, United States Dollar (USD)</option><option value="PGK">Papua New Guinea, Kina (PGK)</option><option value="CNY">Paracel Islands, China Yuan Renminbi (CNY)</option><option value="VND">Paracel Islands, Vietnam Dong (VND)</option><option value="PYG">Paraguay, Guarani (PYG)</option><option value="PEN">Peru, Nuevo Sol (PEN)</option><option value="PHP">Philippines, Peso (PHP)</option><option value="NZD">Pitcairn, New Zealand Dollar (NZD)</option><option value="XPT">Platinum Ounce (XPT)</option><option value="PLN">Poland, Zloty (PLN)</option><option value="EUR">Portugal, Euro (EUR)</option><option value="PTE">Portugal, Escudo [obsolete] (PTE)</option><option value="USD">Puerto Rico, United States Dollar (USD)</option><option value="QAR">Qatar, Riyal (QAR)</option><option value="EUR">Réunion, Euro (EUR)</option><option value="RON">Romania, New Leu (RON)</option><option value="ROL">Romania, Leu [obsolete] (ROL)</option><option value="RUB">Russia, Ruble (RUB)</option><option value="RWF">Rwanda, Franc (RWF)</option><option value="ANG">Saba, Netherlands Antilles Guilder (ANG)</option><option value="SHP">Saint Helena, Pound (SHP)</option><option value="XCD">Saint Kitts and Nevis, East Caribbean Dollar (XCD)</option><option value="XCD">Saint Lucia, East Caribbean Dollar (XCD)</option><option value="EUR">Saint Pierre and Miquelon, Euro (EUR)</option><option value="XCD">Saint Vincent and The Grenadines, East Caribbean Dollar (XCD)</option><option value="EUR">Saint-Martin, Euro (EUR)</option><option value="WST">Samoa, Tala (WST)</option><option value="EUR">San Marino, Euro (EUR)</option><option value="STD">S?o Tome and Principe, Dobra (STD)</option><option value="SAR">Saudi Arabia, Riyal (SAR)</option><option value="GBP">Scotland (United Kingdom), Pound (GBP)</option><option value="SPL">Seborga, Luigino (SPL)</option><option value="XOF">Senegal, CFA Franc BCEAO (XOF)</option><option value="RSD">Serbia, Dinar (RSD)</option><option value="SCR">Seychelles, Rupee (SCR)</option><option value="SLL">Sierra Leone, Leone (SLL)</option><option value="XAG">Silver Ounce (XAG)</option><option value="SGD">Singapore, Dollar (SGD)</option><option value="ANG">Sint Eustatius, Netherlands Antilles Guilder (ANG)</option><option value="ANG">Sint Maarten, Netherlands Antilles Guilder (ANG)</option><option value="EUR">Slovakia, Euro (EUR)</option><option value="SKK">Slovakia, Koruna [obsolete] (SKK)</option><option value="EUR">Slovenia, Euro (EUR)</option><option value="SIT">Slovenia, Tolar [obsolete] (SIT)</option><option value="SBD">Solomon Islands, Dollar (SBD)</option><option value="SOS">Somalia, Shilling (SOS)</option><option value="ZAR">South Africa, Rand (ZAR)</option><option value="GBP">South Georgia, United Kingdom Pound (GBP)</option><option value="KRW">South Korea, Won (KRW)</option><option value="GBP">South Sandwich Islands, United Kingdom Pound (GBP)</option><option value="EUR">Spain, Euro (EUR)</option><option value="ESP">Spain, Peseta [obsolete] (ESP)</option><option value="LKR">Sri Lanka, Rupee (LKR)</option><option value="SDD">Sudan, Dinar [obsolete] (SDD)</option><option value="SDG">Sudan, Pound (SDG)</option><option value="SRD">Suriname, Dollar (SRD)</option><option value="SRG">Suriname, Guilder (SRG) [obsolete]</option><option value="NOK">Svalbard and Jan Mayen, Norway Krone (NOK)</option><option value="SZL">Swaziland, Lilangeni (SZL)</option><option value="ZAR">Swaziland, South Africa Rand (ZAR)</option><option value="SEK">Sweden, Krona (SEK)</option><option value="CHF">Switzerland, Franc (CHF)</option><option value="SYP">Syria, Pound (SYP)</option><option value="TWD">Taiwan, New Dollar (TWD)</option><option value="TJS">Tajikistan, Somoni (TJS)</option><option value="RUB">Tajikistan, Russia Ruble (RUB)</option><option value="TZS">Tanzania, Shilling (TZS)</option><option value="THB">Thailand, Baht (THB)</option><option value="XOF">Togo, CFA Franc BCEAO (XOF)</option><option value="NZD">Tokelau, New Zealand Dollar (NZD)</option><option value="TOP">Tonga, Pa'anga (TOP)</option><option value="MDL">Transnistria, Moldova Leu (MDL)</option><option value="TTD">Trinidad and Tobago, Dollar (TTD)</option><option value="TND">Tunisia, Dinar (TND)</option><option value="TRY">Turkey, Lira (TRY)</option><option value="TRL">Turkey, Lira (TRL) [obsolete]</option><option value="TMT">Turkmenistan, New Manat (TMT)</option><option value="TMM">Turkmenistan, Manat (TMM)</option><option value="USD">Turks and Caicos Islands, United States Dollar (USD)</option><option value="AUD">Tuvalu, Australia Dollar (AUD)</option><option value="TVD">Tuvalu, Dollar (TVD)</option><option value="UGX">Uganda, Shilling (UGX)</option><option value="UAH">Ukraine, Hryvna (UAH)</option><option value="AED">United Arab Emirates, Dirham (AED)</option><option value="GBP">United Kingdom, Pound (GBP)</option><option value="USD">United States, Dollar (USD)</option><option value="UYU">Uruguay, Peso (UYU)</option><option value="UZS">Uzbekistan, Som (UZS)</option><option value="VUV">Vanuatu, Vatu (VUV)</option><option value="EUR">Vatican City, Euro (EUR)</option><option value="VAL">Vatican City, Lira [obsolete] (VAL)</option><option value="VEF">Venezuela, Bolivar Fuerte (VEF)</option><option value="VEB">Venezuela, Bolivar (VEB) [obsolete]</option><option value="VND">Vietnam, Dong (VND)</option><option value="USD">Virgin Islands, United States Dollar (USD)</option><option value="USD">Wake Island, United States Dollar (USD)</option><option value="XPF">Wallis and Futuna Islands, Comptoirs Fran?ais du Pacifique Franc (XPF)</option><option value="ILS">West Bank, Israel New Shekel (ILS)</option><option value="JOD">West Bank, Jordan Dinar (JOD)</option><option value="MAD">Western Sahara, Morocco Dirham (MAD)</option><option value="WST">Samoa, Tala (WST)</option><option value="YER">Yemen, Rial (YER)</option><option value="ZMK">Zambia, Kwacha (ZMK)</option><option value="ZWD">Zimbabwe, Dollar (ZWD)</option></select>?
?
?
?
?
?
?
?