How to Currency Converter Using Google API by PHP Code

Authors: CodeToday | PHP Code | Views: 164 | Posted: 04 AM: 10/07/2017

If you want to create a free tool for converting your web currency, you can use the free google API for currency conversion.

In this article I will guide you to use php code to create your own currency converter, data is updated directly from the google API.

https://finance.google.com/finance/converter
Image currency converter of google API
currency converter

Below is a list of currencies

AED:United Arab Emirates Dirham (AED)
AFN:Afghan Afghani (AFN)
ALL:Albanian Lek (ALL)
AMD:Armenian Dram (AMD)
ANG:Netherlands Antillean Guilder (ANG)
AOA:Angolan Kwanza (AOA)
ARS:Argentine Peso (ARS)
AUD:Australian Dollar (A$)
AWG:Aruban Florin (AWG)
AZN:Azerbaijani Manat (AZN)
BAM:Bosnia-Herzegovina Convertible Mark (BAM)
BBD:Barbadian Dollar (BBD)
BDT:Bangladeshi Taka (BDT)
BGN:Bulgarian Lev (BGN)
BHD:Bahraini Dinar (BHD)
BIF:Burundian Franc (BIF)
BMD:Bermudan Dollar (BMD)
BND:Brunei Dollar (BND)
BOB:Bolivian Boliviano (BOB)
BRL:Brazilian Real (R$)
BSD:Bahamian Dollar (BSD)
BTC:Bitcoin (฿)
BTN:Bhutanese Ngultrum (BTN)
BWP:Botswanan Pula (BWP)
BYN:Belarusian Ruble (BYN)
BYR:Belarusian Ruble (2000–2016) (BYR)
BZD:Belize Dollar (BZD)
CAD:Canadian Dollar (CA$)
CDF:Congolese Franc (CDF)
CHF:Swiss Franc (CHF)
CLF:Chilean Unit of Account (UF) (CLF)
CLP:Chilean Peso (CLP)
CNH:CNH (CNH)
CNY:Chinese Yuan (CN¥)
COP:Colombian Peso (COP)
CRC:Costa Rican Colón (CRC)
CUP:Cuban Peso (CUP)
CVE:Cape Verdean Escudo (CVE)
CZK:Czech Koruna (CZK)
DEM:German Mark (DEM)
DJF:Djiboutian Franc (DJF)
DKK:Danish Krone (DKK)
DOP:Dominican Peso (DOP)
DZD:Algerian Dinar (DZD)
EGP:Egyptian Pound (EGP)
ERN:Eritrean Nakfa (ERN)
ETB:Ethiopian Birr (ETB)
EUR:Euro (€)
FIM:Finnish Markka (FIM)
FJD:Fijian Dollar (FJD)
FKP:Falkland Islands Pound (FKP)
FRF:French Franc (FRF)
GBP:British Pound (£)
GEL:Georgian Lari (GEL)
GHS:Ghanaian Cedi (GHS)
GIP:Gibraltar Pound (GIP)
GMD:Gambian Dalasi (GMD)
GNF:Guinean Franc (GNF)
GTQ:Guatemalan Quetzal (GTQ)
GYD:Guyanaese Dollar (GYD)
HKD:Hong Kong Dollar (HK$)
HNL:Honduran Lempira (HNL)
HRK:Croatian Kuna (HRK)
HTG:Haitian Gourde (HTG)
HUF:Hungarian Forint (HUF)
IDR:Indonesian Rupiah (IDR)
IEP:Irish Pound (IEP)
ILS:Israeli New Shekel (₪)
INR:Indian Rupee (₹)
IQD:Iraqi Dinar (IQD)
IRR:Iranian Rial (IRR)
ISK:Icelandic Króna (ISK)
ITL:Italian Lira (ITL)
JMD:Jamaican Dollar (JMD)
JOD:Jordanian Dinar (JOD)
JPY:Japanese Yen (¥)
KES:Kenyan Shilling (KES)
KGS:Kyrgystani Som (KGS)
KHR:Cambodian Riel (KHR)
KMF:Comorian Franc (KMF)
KPW:North Korean Won (KPW)
KRW:South Korean Won (₩)
KWD:Kuwaiti Dinar (KWD)
KYD:Cayman Islands Dollar (KYD)
KZT:Kazakhstani Tenge (KZT)
LAK:Laotian Kip (LAK)
LBP:Lebanese Pound (LBP)
LKR:Sri Lankan Rupee (LKR)
LRD:Liberian Dollar (LRD)
LSL:Lesotho Loti (LSL)
LTL:Lithuanian Litas (LTL)
LVL:Latvian Lats (LVL)
LYD:Libyan Dinar (LYD)
MAD:Moroccan Dirham (MAD)
MDL:Moldovan Leu (MDL)
MGA:Malagasy Ariary (MGA)
MKD:Macedonian Denar (MKD)
MMK:Myanmar Kyat (MMK)
MNT:Mongolian Tugrik (MNT)
MOP:Macanese Pataca (MOP)
MRO:Mauritanian Ouguiya (MRO)
MUR:Mauritian Rupee (MUR)
MVR:Maldivian Rufiyaa (MVR)
MWK:Malawian Kwacha (MWK)
MXN:Mexican Peso (MX$)
MYR:Malaysian Ringgit (MYR)
MZN:Mozambican Metical (MZN)
NAD:Namibian Dollar (NAD)
NGN:Nigerian Naira (NGN)
NIO:Nicaraguan Córdoba (NIO)
NOK:Norwegian Krone (NOK)
NPR:Nepalese Rupee (NPR)
NZD:New Zealand Dollar (NZ$)
OMR:Omani Rial (OMR)
PAB:Panamanian Balboa (PAB)
PEN:Peruvian Sol (PEN)
PGK:Papua New Guinean Kina (PGK)
PHP:Philippine Peso (PHP)
PKG:PKG (PKG)
PKR:Pakistani Rupee (PKR)
PLN:Polish Zloty (PLN)
PYG:Paraguayan Guarani (PYG)
QAR:Qatari Rial (QAR)
RON:Romanian Leu (RON)
RSD:Serbian Dinar (RSD)
RUB:Russian Ruble (RUB)
RWF:Rwandan Franc (RWF)
SAR:Saudi Riyal (SAR)
SBD:Solomon Islands Dollar (SBD)
SCR:Seychellois Rupee (SCR)
SDG:Sudanese Pound (SDG)
SEK:Swedish Krona (SEK)
SGD:Singapore Dollar (SGD)
SHP:St. Helena Pound (SHP)
SKK:Slovak Koruna (SKK)
SLL:Sierra Leonean Leone (SLL)
SOS:Somali Shilling (SOS)
SRD:Surinamese Dollar (SRD)
STD:São Tomé & Príncipe Dobra (STD)
SVC:Salvadoran Colón (SVC)
SYP:Syrian Pound (SYP)
SZL:Swazi Lilangeni (SZL)
THB:Thai Baht (THB)
TJS:Tajikistani Somoni (TJS)
TMT:Turkmenistani Manat (TMT)
TND:Tunisian Dinar (TND)
TOP:Tongan Paʻanga (TOP)
TRY:Turkish Lira (TRY)
TTD:Trinidad & Tobago Dollar (TTD)
TWD:New Taiwan Dollar (NT$)
TZS:Tanzanian Shilling (TZS)
UAH:Ukrainian Hryvnia (UAH)
UGX:Ugandan Shilling (UGX)
USD:US Dollar ($)
UYU:Uruguayan Peso (UYU)
UZS:Uzbekistani Som (UZS)
VEF:Venezuelan Bolívar (VEF)
VND:Vietnamese Dong (₫)
VUV:Vanuatu Vatu (VUV)
WST:Samoan Tala (WST)
XAF:Central African CFA Franc (FCFA)
XCD:East Caribbean Dollar (EC$)
XDR:Special Drawing Rights (XDR)
XOF:West African CFA Franc (CFA)
XPF:CFP Franc (CFPF)
YER:Yemeni Rial (YER)
ZAR:South African Rand (ZAR)
ZMK:Zambian Kwacha (1968–2012) (ZMK)
ZMW:Zambian Kwacha (ZMW)
ZWL:Zimbabwean Dollar (2009) (ZWL)

HTML Form

<form action="" method="post" class="form-horizontal">
<div class="form-group">
<input type="text" name="amount"  value="<?=$_POST['amount']?>" size="80" class="form-control" placeholder = "Enter amount here ..." required/>
</div>
<div class="form-group">
<select name = "from" class="form-control">
<option  value="USD">US Dollar ($)</option>
<option  value="UYU">Uruguayan Peso (UYU)</option>
<option  value="UZS">Uzbekistani Som (UZS)</option>
<option  value="VEF">Venezuelan Bolívar (VEF)</option>
<option  value="VND">Vietnamese Dong (₫)</option>
<option  value="VUV">Vanuatu Vatu (VUV)</option>
<option  value="WST">Samoan Tala (WST)</option>
</select>
</div>
<div class="form-group">
<select name = "to" class="form-control">
<option  value="USD">US Dollar ($)</option>
<option  value="UYU">Uruguayan Peso (UYU)</option>
<option  value="UZS">Uzbekistani Som (UZS)</option>
<option  value="VEF">Venezuelan Bolívar (VEF)</option>
<option  value="VND">Vietnamese Dong (₫)</option>
<option  value="VUV">Vanuatu Vatu (VUV)</option>
<option  value="WST">Samoan Tala (WST)</option>
</select>
</div>
<div class="form-group">
<button class="btn btn-info" type="submit" name="submit"><i class="glyphicon glyphicon-search"></i> Currency Converter</button>
</div>
</form>

You can use the full list of currencies as listed above, here you just take the currency example of some countries.

PHP Full Source Code

<?
if(isset($_POST['submit'])) {

$amount = trim($_POST['amount']);
$from   = trim($_POST['from']);
$to     = trim($_POST['to']);

echo '<h3 class = "text-primary">Currency Converter '.$amount.' from '.$from.' to '.$to.'</h3>';

$url  = "https://finance.google.com/finance/converter?a=".$amount."&from=".$from."&to=".$to."";
$data = curlGet($url);
$regex = '#<span class=bld>(.+?)</span>#s';
preg_match($regex, $data, $result);
	$result = '<b>'.$result[0].'</b>';
 	echo $amount.' '.$from.' = '.$result;
}
function curlGet($url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	curl_setopt($ch, CURLOPT_HTTPGET, 1 );
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_URL, $url );
	curl_setopt($ch, CURLOPT_REFERER, $ref );
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);     
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}
?>

The result is as shown below when you perform the above steps:

How to Currency Converter Using Google API by PHP Code


If you have any questions, please leave a message below