Hello Friends, today we are going to see a script for currency convertor as per geo location in php. We are going to use Free plan of currencylayer api for currency rates and geoplugin api for geo location. currencylayer’s free version api only does not support currency conversion and we cannot change base currency other than USD. So, here we going to utilize free version for conversion. But we have to use it smartly as it gives only 250 request / month in free plan. You can request once, twice or maximum eight times in a day and store currency rates in database and use database value for conversion instead of directly sending request to currencylayer. In this way 250 request is enough for whole month. But here I am only going to explain how to use currencylayer api, how to convert, how to get geo location, rest you can customize according to your need. So let’s start coding
currency-convertor.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
<?php function ipInfo($ip = NULL, $purpose = "location", $deep_detect = TRUE) { $output = NULL; if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) { $ip = $_SERVER["REMOTE_ADDR"]; if ($deep_detect) { if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) $ip = $_SERVER['HTTP_CLIENT_IP']; } } $purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose))); $support = array("country", "countrycode", "state", "region", "city", "location", "address", "currencycode"); $continents = array( "AF" => "Africa", "AN" => "Antarctica", "AS" => "Asia", "EU" => "Europe", "OC" => "Australia (Oceania)", "NA" => "North America", "SA" => "South America" ); if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) { $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip)); if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) { switch ($purpose) { case "location": $output = array( "city" => @$ipdat->geoplugin_city, "state" => @$ipdat->geoplugin_regionName, "country" => @$ipdat->geoplugin_countryName, "country_code" => @$ipdat->geoplugin_countryCode, "continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)], "continent_code" => @$ipdat->geoplugin_continentCode ); break; case "address": $address = array($ipdat->geoplugin_countryName); if (@strlen($ipdat->geoplugin_regionName) >= 1) $address[] = $ipdat->geoplugin_regionName; if (@strlen($ipdat->geoplugin_city) >= 1) $address[] = $ipdat->geoplugin_city; $output = implode(", ", array_reverse($address)); break; case "city": $output = @$ipdat->geoplugin_city; break; case "state": $output = @$ipdat->geoplugin_regionName; break; case "region": $output = @$ipdat->geoplugin_regionName; break; case "country": $output = @$ipdat->geoplugin_countryName; break; case "countrycode": $output = @$ipdat->geoplugin_countryCode; break; case "currencycode": $output = @$ipdat->geoplugin_currencyCode; break; } } } return $output; } function convert($from, $to, $amount) { $access_key = '2bdbac13e326ecb38bc9fecae8ac483b';//your currencylayer access_key $currencies = $from . "," . $to; $output = @json_decode(file_get_contents("http://api.currencylayer.com/live?access_key=" . $access_key . "¤cies=" . $currencies . "&format=1"), true); $rate = $output['quotes']['USD' . strtoupper($to)] / $output['quotes']['USD' . strtoupper($from)]; return $rate * $amount; } //Usage echo "<br/>Fixed Conversion<br/>"; echo "1 USD = " . convert("USD", "INR", 1) . " INR<br/>"; echo "1 INR = " . convert("INR", "USD", 1) . " USD<br/>"; echo "55 INR = " . convert("INR", "USD", 55) . " USD<br/>"; echo "20 GBP = " . convert("GBP", "INR", 20) . " INR<br/>"; echo "99 INR = " . convert("INR", "GBP", 99) . " GBP<br/>"; echo "<br/>Geo Location Based Conversion<br/>"; $currency_code = ipInfo(NULL, "currencycode", TRUE); echo "199 USD = " . convert("USD", $currency_code, 199) . " " . $currency_code . "<br/>"; echo "199 INR = " . convert("INR", $currency_code, 199) . " " . $currency_code . "<br/>"; echo "199 GBP = " . convert("GBP", $currency_code, 199) . " " . $currency_code . "<br/>"; ?> |
Here in the above code you can see two function one is ipInfo() and another one is convert(). ipinfo converts user’s ip to location as well as gives currency code of the location and convert function converts given amount one currency to another currency.
Output
Download Script
Currency Convertor Script
Thanks friends
Please don’t forget to share if you find this helpful
It really helped me on one of my project