如何在PHP中使用IP地址获取国家和城市


How get country and city using IP address in PHP

I am trying  to get country and city using ip address in php.My code is given below.

我得到了输出。任何机构都为这些问题提供任何解决方案?我像这样放了IP地址:- 127.0.0.1 城市:- 未定义 国家:- 未定义

 <?php
 /*Get user ip address*/
 $ip_address=$_SERVER['REMOTE_ADDR'];
/*Get user ip address details with geoplugin.net*/
$geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip_address;
$addrDetailsArr = unserialize(file_get_contents($geopluginURL)); 
/*Get City name by return array*/
$city = $addrDetailsArr['geoplugin_city']; 
/*Get Country name by return array*/
$country = $addrDetailsArr['geoplugin_countryName'];
/*Comment out these line to see all the posible details*/
/*echo '<pre>';
print_r($addrDetailsArr);
die();*/
if(!$city){
$city='Not Define';
}if(!$country){
$country='Not Define';
 }
echo '<strong>IP Address</strong>:- '.$ip_address.'<br/>';
echo '<strong>City</strong>:- '.$city.'<br/>';
echo '<strong>Country</strong>:- '.$country.'<br/>';
?>

您需要使请求来自互联网上的IP地址。从本地主机环回网络接口发送它不会为服务器提供在任何数据库中都有位置的 IP 地址。