Want To Be A Blogger  →  Start A Blog  →  Increase Traffic  →  Make Money Online  →  Recommended Things

Currency Converter Using PHP And Google API

Friday, Dec 15, 2017

Tags:- PHP Google API HTML


In this tutorial we will show you how to convert currency using PHP and Google API.User just have to enter amount and select amount currency and select currency he wish to convert on.You may also like get address longitude and latitude using google map api.


Currency Converter Using PHP And Google API


To Convert Currency It Takes Only Three Steps:-

  1. Make a HTML file and define markup
  2. Make a PHP file to convert currency
  3. Make a CSS file and define styling


Step 1.Make a HTML file and define markup

We make a HTML file and save it with a name currency.html

<html>
<head>
<link type="text/css" rel="stylesheet" href="currency_style.css"/>
</head>
<body>
<div id="wrapper">

<div id="convert_div">
<form method="post"action="convert_currency.php">
 <input type="text" name="amount" placeholder="Enter Amount">
 <select name="convert_from">
  <option  value="INR">Indian Rupee</option>
  <option  value="USD">US Dollar</option>
  <option  value="SGD">Singapore Dollar</option>
  <option  value="EUR">Euro</option>
  <option  value="AED">UAE Dirham</option>
 </select>
 <select name="convert_to">
  <option  value="INR">Indian Rupee</option>
  <option  value="USD">US Dollar</option>
  <option  value="SGD">Singapore Dollar</option>
  <option  value="EUR">Euro</option>
  <option  value="AED">UAE Dirham</option>
 </select>
 <br>
 <input type="submit" name="convert_currency" value="Convert Currency">
</form>
</div>

</div>
</body>
</html>

In this step we create a form and add a text field and two select option menu to enter amount and select before and after currency type and then submit form to 'convert_currency.php' file.You may also like login with google using PHP.



Step 2.Make a PHP file to convert currency

We make a PHP file and save it with a name convert_currency.php

<?php
function currency_converter($from,$to,$amount)
{
 $url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to"; 
 
 $request = curl_init(); 
 $timeOut = 0; 
 curl_setopt ($request, CURLOPT_URL, $url); 
 curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1); 
 
 curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
 curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut); 
 $response = curl_exec($request); 
 curl_close($request); 
 
 return $response;
} 

if(isset($_POST['convert_currency']))
{
 $amount=$_POST['amount'];
 $from=$_POST['convert_from'];
 $to=$_POST['convert_to'];
	
 $rawData = currency_converter($from,$to,$amount);
 $regex = '#\<span class=bld\>(.+?)\<\/span\>#s';
 preg_match($regex, $rawData, $converted);
 $result = $converted[0];
 echo $result;
}
?>

In this step we get all the values like amount, currency from and currency to and then call currency_converter() function to convert currency.In currency_converter() function we use Google API link and embed amount, currency from and currency to and by using curl function which is predifined by google we get converted currency in raw form.Then we use regular expression and preg_match() function to get final converted currency.You may also like HTML to PDF using PHP.



Step 3.Make a CSS file and define styling

We make a CSS file and save it with a name currency_style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 background-color:#F79F81;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#wrapper h1
{
 margin-top:50px;
 font-size:45px;
 color:#B43104;
}
#wrapper h1 p
{
 font-size:18px;
}
#convert_div input[type="text"]
{
 width:300px;
 height:55px;
 padding-left:10px;
 font-size:18px;
 margin-bottom:15px;
 color:#424242;
 font-weight:bold;
 border:none;
}
#convert_div select
{
 margin:0px;
 padding:0px;
 width:150px;
 height:55px;
 font-size:15px;
 margin-bottom:15px;
 color:#424242;
 font-weight:bold;
 border:none;
}
#convert_div input[type="submit"]
{
 width:330px;
 height:45px;
 font-size:16px;
 font-weight:bold;
 background-color:#B43104;
 color:white;
 border:none;
 box-shadow:0px 3px 0px 0px #8A2908;
 border-radius:3px;
}

That's all, this is how to convert currency using PHP and Google API.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Subscribe Our Newsletter And Get Tutorials And Offers Via Email

About Me


Hello, My Name is Mudit Jain. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of TalkersCode. I started this blog in february 2015 to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers.