Categories: Php

Currency Converter In PHP Using Google Api

Google is provides free of cost Currency Converter API to convert one currency into another currency type.Google Currency Converter API can be implement any programming language. I am suing PHP to create currency converter application to convert real time currency value from one currency type to other currency type.In this tutorials, I will learn how to implement currency conversion functionality using PHP with demo.

I will provide two script to convert currency using google API one is by using file_get_contents() method and other is using PHP curl.We will create a PHP function and pass three parameters like From currency, To currency and the amount to be converted, below php script are used to convert currency from any currency to another currency using Google Currency Converter API.

It’s Deprecated

Google Currency Converter Api Using file_get_contents() PHP method

<?php
function currencyConverter($from_currency, $to_currency, $amount) {
$from_Currency = urlencode($from_currency);
$to_Currency = urlencode($to_currency);
$encode_amount = urlencode($amount);
$get = file_get_contents("https://www.google.com/finance/converter?a=$encode_amount&from=$from_Currency&to=$to_Currency");
$get = explode("<span class=bld>",$get);
$get = explode("</span>",$get[1]);
$converted_currency = preg_replace("/[^0-9\.]/", null, $get[0]);
return $converted_currency;
}
?> 

Google Currency Converter Api Using PHP Curl

<?php
function currencyConverter($from_currency, $to_currency, $amount) {
$amount    = urlencode($amount);
$from    = urlencode($from_currency);
$to        = urlencode($to_currency);
$url    = "http://www.google.com/ig/calculator?hl=en&q=$amount$from=?$to";
$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);
}
?>

We have created a function currencyConverter($from_currency,$to_currency, $amount) with three parameters, where the parameters are,

Related Post
  • $from_currency: This parameters will use to which currency you want to convert.
  • $to_currency: This parameters will use to currency, in which it will convert.
  • $amount: How much amount will convert.

How to call Above Currency Converter Method

<?php
//amount
$amount =10;
 
//From Currency
$from_currency ="INR";
 
//To Currency
$to_currency ="USD";
 
$converted_currency = currencyConverter($from_currency, $to_currency, $amount);
 
echo $converted_currency;
?>

You can download source code and Demo from below link.

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

3 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories