Build Currency Converter in PHP with Google API

Today, We want to share with you google currency converter api php.In this post we will show you free currency converter api php, hear for live currency converter in php we will give you demo and example for implement.In this post, we will learn about All Coins And Cryptocurrency Data API PHP, Ruby, Python with an example.

google currency converter api php

here in this Example You learn All About Example of currency converter in php source code, google finance converter api, currency converter api android with demo and full source code.

Step 1: Creare Currency Convert HTML Form

<div class="container">
	<h2>Example: Build Currency Converter in PHP - www.pakainfo.com</h2>	
	<form method="post" id="money-convert-form">
		<div class="form-group row">
		 <div class="col-xs-4">
		<label for="from_curr">From:</label>
		<select name="money_from" class="form-control">
		<option value="INR">Indian Rupee</option>
		<option value="USD" selected="1">US Dollar</option>
		</select>		
		</div>		
		</div>
		<div class="form-group row">
		<div class="col-xs-4">
		<label for="pwd">To:</label>
		<select name="money_to" class="form-control">
		<option value="INR" selected="1">Indian Rupee</option>
		<option value="USD">US Dollar</option>
		</select>
		</div>
		<div class="col-xs-4">
		<div id="money_conversion_rate"></div>	
		<div id="converted_money_amount"></div>		
		</div>
		</div>
		<div class="form-group row">
		<div class="col-xs-4">
		<label>Amount :</label>
		<input type="number" placeholder="amount" name="amount" id="amount" class="form-control">
		</div>
		</div>
		<button type="submit" class="btn btn-info" name="convert_money" id="convert_money">Convert</button>		
	</form>	
</div>

Step 2: Handle Currency Form Submit with Ajax

$('document').ready(function() {	
	$("#money-convert-form").validate({
		rules: {
			amount: {
				required: true,
			},
		},
		messages: {
			amount:{
			  required: ""
			 },			
		},
		submitHandler: moneyConvertFormSubmit	
	});	
	function moneyConvertFormSubmit() {		
		var data = $("#money-convert-form").serialize();				
		$.ajax({				
			type : 'POST',
			url  : 'convert_money.php',
			dataType:'json',
			data : data,
			beforeSend: function(){					
			},
			success : function(response){				
			}
		});
		return false;
	}   
});

Step 3: Convert currency into another currency with Google Currency API

<?php
include_once("functions.php");
if(isset($_POST['convert_money'])) {
	$money_from = trim($_POST['money_from']);
	$money_to = trim($_POST['money_to']);
	$amount = trim($_POST['amount']);	
	if($money_from == $money_to) {
	 	$data = array('error' => '1');
		echo json_encode( $data );	
		exit;
	}
	$converted_money=convertCurrency($money_from, $money_to, $amount);
	echo $converted_money;
}
?>

Step 4: Create currency convert function

function convertCurrency($money_from,$money_to,$convert_amount) {	
	$money_from = urlencode($money_from);
	$money_to = urlencode($money_to);	
	$moneyUrl = "https://www.google.com/search?q=".$money_from."+to+".$money_to;
	$moneyDetails = file_get_contents($moneyUrl);
	$moneyData = preg_split('/\D\s(.*?)\s=\s/',$moneyDetails);
	$conversion_rate = (float) substr($moneyData[1],0,7);	
	$total_converted_money_amount = $convert_amount*$conversion_rate;
	$moneyJsonData = array( 'rate' => $conversion_rate, 'total_converted_money_amount' =>$total_converted_money_amount, 'money_from' => strtoupper($money_from), 'money_to' => strtoupper($money_to));
	echo json_encode( $moneyJsonData );	
}

I hope you get an idea about google currency converter api javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  How to Check If Array is Empty in Laravel Blade?

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *