ip地址库 新浪,淘宝

原文连接地址:http://www.9958.pw/post/city_ip

function getAddressFromIp($ip){
$urlTaobao = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
$urlSina = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip;
$json = file_get_contents($urlTaobao);
$jsonDecode = json_decode($json);
if($jsonDecode->code==){//如果取不到就去取新浪的
$data['country'] = $jsonDecode->data->country;
$data['province'] = $jsonDecode->data->region;
$data['city'] = $jsonDecode->data->city;
$data['isp'] = $jsonDecode->data->isp;
return $data;
}else{
$json = file_get_contents($urlSina);
$jsonDecode = json_decode($json);
$data['country'] = $jsonDecode->country;
$data['province'] = $jsonDecode->province;
$data['city'] = $jsonDecode->city;
$data['isp'] = $jsonDecode->isp;
$data['district'] = $jsonDecode->district;
return $data;
}
}

PS:在使用的时候发现,反馈的速度有时候会比较慢,影响了网站速度。

分开操作

/**
* 调用淘宝地址库
* */
function ip_taobao($ip){
$urlTaobao = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
$json = file_get_contents($urlTaobao);
$jsonDecode = json_decode($json);
$data['country'] = $jsonDecode->data->country;
$data['province'] = $jsonDecode->data->region;
$data['city'] = $jsonDecode->data->city;
$data['isp'] = $jsonDecode->data->isp;
return $data;
}
/**
* 调用新浪地址库
* */
function ip_sina($ip){
$data = '';
$urlSina = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip;
$json = file_get_contents($urlSina);
$jsonDecode = json_decode($json);
if($jsonDecode == '-2'){
$data['error'] = '未分配或者内网IP';
}else{
$data['country'] = $jsonDecode->country;
$data['province'] = $jsonDecode->province;
$data['city'] = $jsonDecode->city;
$data['isp'] = $jsonDecode->isp;
$data['district'] = $jsonDecode->district;
}
return $data;
}

 调用

/**
* 返回ip所在位置
* */
public function ip_address($ip=''){
if (empty($ip)) $ip = request()->ip();
$info = Model('site')->info();
if($info['ip_library'] ==){
$arr= ip_taobao($ip);
}else{
$arr= ip_sina($ip);
}
$address = implode(' ',$arr);
return $address;
}

新的用法

$ip = Request()->ip();
$ip_info = ip_sina($ip);
if($ip_info['country']!='未分配或者内网IP'){
$address = implode(' ',$ip_info);
}else{
$address = '未分配或者内网IP';
}
print_r($address);

最完美的地址库

/**
* 调用淘宝地址库
* */
function ip_taobao($ip){
//$ip = '113.87.131.159';
$opt = [
'http'=>[
'method'=>'GET',
'timeout'=>
]
];
$context = stream_context_create($opt);
$urlTaobao = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
$json = @file_get_contents($urlTaobao,false,$context);
$jsonDecode = json_decode($json);
if($jsonDecode){
$data['country'] = $jsonDecode->data->country;
$data['province'] = $jsonDecode->data->region;
$data['city'] = $jsonDecode->data->city;
$data['isp'] = $jsonDecode->data->isp;
}else{
$data['country'] = '无法连接网络';
}
return $data;
}
/**
* 调用新浪地址库
* */
function ip_sina($ip){
//$ip = '113.87.131.159';
$opt = [
'http'=>[
'method'=>'GET',
'timeout'=>
]
];
$context = stream_context_create($opt);
$data = '';
$urlSina = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip;
$json = @file_get_contents($urlSina,false,$context);
$jsonDecode = json_decode($json);
if($jsonDecode){
if($jsonDecode == '-2'){
$data['country'] = '未分配或者内网IP';
}else{
$data['country'] = $jsonDecode->country;
$data['province'] = $jsonDecode->province;
$data['city'] = $jsonDecode->city;
$data['isp'] = $jsonDecode->isp;
$data['district'] = $jsonDecode->district;
}
}else{
$data['country'] = '无法连接网络';
}
return $data;
}
上一篇:ECshop模板机制


下一篇:使用django开发博客过程记录4——Category分类视图