First of all I would like to share with you guys that getting address details from an IP address is not much reliable because it does not give one's correct address. If you require exact address of a user, get latitude, longitude and fetch address from it instead of IP.
Please follow below PHP code to get address detail from IP address:
//$ip = $_SERVER['REMOTE_ADDR']; //Ig gives you current IP address
$ip = 103.103.59.180;
$result = json_decode(file_get_contents("http://ip-api.io/json/$ip"));
It gives below information and you can use it forming as per your requirement
stdClass Object
(
[ip] => 103.103.59.180
[country_code] => IN
[country_name] => India
[region_code] => DL
[region_name] => National Capital Territory of Delhi
[city] => New Delhi
[zip_code] => 110014
[time_zone] => Asia/Kolkata
[latitude] => 28.6
[longitude] => 77.2
[metro_code] => 0
[suspicious_factors] => stdClass Object
(
[is_proxy] =>
[is_tor_node] =>
[is_spam] =>
[is_suspicious] =>
)
)
You can see above printed object, it gives you information about proxy, spam or any suspicious source.
Now, JavaScript code to get address details from an IP address.
$(document).ready(function(){
$('#getIp').click(function(){
var IP = $('#inputIp').val();
if( IP == '')
{
alert("Please enter your IP address!");
return false;
}
$.getJSON("http://ip-api.io/json/" + IP, function(res){
alert('Country Name: ' + res.country_name);
alert('City Name: ' + res.city);
alert('Zip Code: ' + res.zip_code);
console.log(res); //See in console, it will show complete information object
});
});
});