In this tutorial we will show you how to get user’s complete location using users IP address in PHP with the help of GeoPlugin and freegeoip web service.
Example 1
<?php
$ip='77.99.179.20';
$location = file_get_contents('http://freegeoip.net/json/'.$ip);
print_r($location);
?>
Output
{
"ip":"77.99.179.20",
"country_code":"GB",
"country_name":"United Kingdom",
"region_code":"ENG",
"region_name":"England",
"city":"Cheltenham",
"zip_code":"GL52",
"time_zone":"Europe/London",
"latitude":51.9222,
"longitude":-2.0509,
"metro_code":0
}
Example 2
<?php
$ip='77.99.179.20';
echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip)));
?>
Output
array (
'geoplugin_request' => '77.99.179.20',
'geoplugin_status' => 200,
'geoplugin_credit' => 'Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.', 'geoplugin_city' => 'Cheltenham',
'geoplugin_region' => 'Gloucestershire',
'geoplugin_areaCode' => '0',
'geoplugin_dmaCode' => '0',
'geoplugin_countryCode' => 'GB',
'geoplugin_countryName' => 'United Kingdom', 'geoplugin_continentCode' => 'EU',
'geoplugin_latitude' => '51.9222',
'geoplugin_longitude' => '-2.0509',
'geoplugin_regionCode' => 'E6',
'geoplugin_regionName' => 'Gloucestershire',
'geoplugin_currencyCode' => 'GBP',
'geoplugin_currencySymbol' => '£',
'geoplugin_currencySymbol_UTF8' => '£',
'geoplugin_currencyConverter' => '0.7077',
)