How to get Location form IP Address in PHP« Back to Questions List

How to get Location from IP address using PHP
Posted by aliraza170@gmail.com
Asked on September 8, 2016 9:33 am
1

// Get the longitude and latitude data using IP Address of user.

$user_data = json_decode(file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']));
$latitude = $user_data->latitude;
$longitude = $user_data->longitude;

// Use the longitude and latitude from above to get the area name of user location.


$json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$latitude.','.$longitude.'&sensor=true');
$data = json_decode($json, true);

$area_name =  $data['results'][1]['address_components'][0]['long_name'];
echo $area_name;

Posted by aliraza170@gmail.com
Answered On September 8, 2016 9:35 am