博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ip地址库 新浪,淘宝
阅读量:5903 次
发布时间:2019-06-19

本文共 4090 字,大约阅读时间需要 13 分钟。

原文连接地址: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==0){
//如果取不到就去取新浪的 $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'] ==1){            $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'=> 5        ]    ];    $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'=> 5        ]    ];    $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;}

 

转载地址:http://gqkpx.baihongyu.com/

你可能感兴趣的文章
Docker的系统资源限制及验证
查看>>
c++ ios_base register_callback方法使用
查看>>
Java中为什么需要Object类,Object类为什么是所有类的父类
查看>>
angularjs-paste-upload
查看>>
linux基础命令 head
查看>>
objective c:import和include的区别, ""和<>区别
查看>>
The Shared folder with you
查看>>
poj 2234 Matches Game
查看>>
sax方式解析XML学习笔记
查看>>
Springboot配置(上)
查看>>
java--Eclipse for mac 代码提示(代码助手,代码联想)快捷键修改
查看>>
Jdom的简单操作
查看>>
left join on/right join on/inner join on/full join on连接
查看>>
Codeforces 582B Once Again
查看>>
template.helper 多参数
查看>>
RadioButton布局图片+文字 实现tabhost效果
查看>>
access中设置不等于
查看>>
hdu 1221 Rectangle and Circle
查看>>
Android 四大组件之四(ContentProvider)
查看>>
Android 四大组件之一(Activity)
查看>>