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.
<?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; } ?>
<?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,
<?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.
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
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
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
Does your blog have a contact page? I'm having trouble locating it but, I'd like to shoot you an email.
I've got some recommendations for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it grow over time.
i will add contact page shortly, you can contact me on parvez1487(at)gmail(dot)com
can u check get variable have response?
live demo not working
The libs has been removed