这个是基于PHP的:
这个只能得到城市
<?php
$ip = '163.179.32.93';
$str = file_get_contents("http://ip.fengtalk.com/ip/?ip=".$ip);
$str = substr($str,strrpos($str,":")+2);
$str = substr($str,0,strpos($str,'"'));
echo $str;
?>
直接访问它的API的效果截图(上面的代码会只提取出城市信息,而不是下面的效果):
由于它只能得到城市信息,所以请看下面。
这个才是有用的。它能得到更详细的信息
调用的是太平洋(pconline)的API
<?php
$ip = '163.179.32.93';
$location = file_get_contents("https://whois.pconline.com.cn/jsLabel.jsp?ip=".$ip);
$location = mb_convert_encoding($location, 'utf-8','GB2312');
$location = substr($location,strrpos($location,"='")+2);
$location = substr($location,0,strpos($location,"'"));
echo $location;
?>
请直接调用此函数:
传入IP地址,返回归属地信息,包括省->市->区(可能更详细)还包括该IP是移动的,联通的还是电信的信息。
该函数调用的API跟上面那个一样,也调用的是太平洋(pconline)的API。
//得到某IP的归属地,返回归属地字符串
function GetAddress($ip)
{
//得到IP归属地
$location = file_get_contents("https://whois.pconline.com.cn/jsLabel.jsp?ip=".$ip);
//转换字符集,UTF-8 -> GBK
//如果出现乱码就删掉这句
$location = mb_convert_encoding($location, 'utf-8','GB2312');
//删去无用信息,只保留归属地信息
$location = substr($location,strrpos($location,"='")+2);
$location = substr($location,0,strpos($location,"'"));
//返回
return $location;
}
太平洋的这个,效果如下(直接访问API的效果,含有杂项):
使用GetAddres函数效果(去除杂项):
完整示例:
<html>
<head>
<title>得到IP归属地</title>
</head>
<body>
<?php
//得到某IP的归属地,返回归属地字符串
function GetAddress($ip)
{
//得到IP归属地
$location = file_get_contents("https://whois.pconline.com.cn/jsLabel.jsp?ip=".$ip);
//转换字符集,UTF-8 -> GBK
//$location = mb_convert_encoding($location, 'utf-8','GB2312');
//删去无用信息,只保留归属地信息
$location = substr($location,strrpos($location,"='")+2);
$location = substr($location,0,strpos($location,"'"));
//返回
return $location;
}
$Address = GetAddress("123.123.123.123");
echo $Address;
?>
</body>
</html>