In this tutorial we are going to explain how to create currency converter in website. We found plenty of tutorials on internet related this, but they are not working or updated. Most of the recommend Google API for currency converter. Google has depreciate this API service and no longer use anymore.
We searched and found few paid API service who provide real time currency converter feature like xe.com, fixer.io. They have premium service and you have to take monthly subscription to get this API service.
In this tutorial, we are using free.currencyconverterapi.com API service to convert currency value from one currency type to other currency type. It is also paid API subscription, but they have Free version also.
This free version has some access limit. If you want to create Currency Converter for small scale website, than it would be good option for you.
Free Version API Limits:
Conversion Pairs per Request: 2
Number of Requests per Hour: 100
Date Range in History: 8 Days
Allowed Back in History: 1 Year(s)
PHP Currency Converter Script
In this script we are using currencyconverterapi free API by using file_get_contents() method. We have created a PHP ‘currencyConverter‘ function and send parameters like Amount, From currency, To currency.
$amount: Currancy amount you want to convert.
$from_currency: Use this parameters to input which currency you want to convert.
$to_currency: Use this parameters to pass currency value, in which it will convert.
Check below Currency Converter code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php function currencyConverter ($amount, $from_Currency, $to_Currency) { $from_Currency = urlencode(strtoupper($from_Currency)); $to_Currency = urlencode(strtoupper($to_Currency)); $url = file_get_contents('http://free.currencyconverterapi.com/api/v3/convert?q='.$from_Currency.'_'. $to_Currency.'&compact=ultra'); $json = json_decode($url, true); $ratevalue = $json[$from_Currency . '_' . $to_Currency]; $output = $amount * $ratevalue; return $output; } echo currencyConverter('300', 'INR', 'USD'); ?> |
This is the straight PHP Currency Converter example. You can implement on your website for live currancy conversion.
If you want more feature than you can go for Premium API version, which just start with $5 per month price.
Check Live Demo and Download Free Source code
Download Currency Converter Script