PHP 在线IP地址库的Api


在线IP地址库的Api


正文

怎么知道一个ip的详细地址呢?访问IP地址库,网上可以搜到好多。

淘宝IP库,但速度慢
http://ip.taobao.com/service/getIpInfo.php?ip=202.5.121.34
返回:
{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u4e1c\u5317","area_id":"200000","region":"\u9ed1\u9f99\u6c5f\u7701","region_id":"230000","city":"\u54c8\u5c14\u6ee8\u5e02","city_id":"230100","county":"","county_id":"-1","isp":"\u8054\u901a","isp_id":"100026","ip":"218.7.221.49"}}

新浪的IP库,速度可以,但没运营商信息
http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=218.7.221.49&format=json
返回:
{"ret":1,"start":-1,"end":-1,"country":"\u4e2d\u56fd","province":"\u9ed1\u9f99\u6c5f","city":"\u54c8\u5c14\u6ee8","district":"","isp":"","type":"","desc":""}

//淘宝IP库,但好慢
// $ch = curl_init('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);
//新浪的IP库,速度不错,但没运营商信息
// $ch = curl_init('http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=218.7.221.49&format=json');
$ch = curl_init('http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip='.$ip.'&format=json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$chRel = curl_exec($ch);

if (curl_errno($ch)) {
    $ipArr = [];
} else {
    $ipArr = json_decode($chRel, 1);
}

//如果不为1,说明是内网
if ($ipArr['ret'] != 1) {
    $ipArr['country'] = '内网';
    $ipArr['province'] = '内网';
    $ipArr['city'] = '内网';
} 






参考资料


返回