原文连接地址: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;
}

ip地址库 新浪,淘宝的更多相关文章

  1. WordPress使用淘宝IP地址库的API显示评论者的位置信息(二)

    1 淘宝IP地址库的接口说明 在上一篇文章<WordPress使用淘宝IP地址库的API显示评论者的位置信息(一)>中,vfhky使用了新浪工具提供的这个IP接口显示博客评论者的位置信息. ...

  2. 淘宝ip地址库接口会导致TTFB时间变长,网站打开速度变慢

    前一段时间闲来无事发现别人的网站上有显示当前用户城市的功能,就自己也整了一个 这是淘宝ip地址库调用方法 然后问题就出现了,网站打开速度慢的要死 用F12发现是TTFB太慢,然后百度了,发现了问题的原 ...

  3. 用淘宝ip地址库查ip

    这是一个通过调用淘宝ip地址库实现ip地址查询的功能类 using System; using System.Collections.Generic; using System.Linq; using ...

  4. Delphi使用JSON解析调用淘宝IP地址库REST API 示例

    淘宝IP地址库:http://ip.taobao.com,里面有REST API 说明. Delphi XE 调试通过,关键代码如下: var IdHTTP: TIdHTTP; RequestURL: ...

  5. 淘宝IP地址库采集器c#代码

    这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下. 最近做一个项目,功能类似于CNZZ站长统计功能,要求显示Ip所在的省份市区/提供商等信息.网上的Ip纯真数据库,下载下来一看 ...

  6. 淘宝IP地址库API接口(PHP)通过ip获取地址信息

    淘宝IP地址库网址:http://ip.taobao.com/ 提供的服务包括: 1. 根据用户提供的IP地址,快速查询出该IP地址所在的地理信息和地理相关的信息,包括国家.省.市和运营商. 2. 用 ...

  7. 淘宝IP地址库采集

    作者:阿宝 更新:2016-08-31 来源:彩色世界(https://blog.hz601.org/2016/08/31/taobao-ip-sniffer/index.html) 简述 当初选择做 ...

  8. 淘宝IP地址库API地址

    淘宝IP地址库:http://ip.taobao.com/instructions.php   接口说明 1. 请求接口(GET): http://ip.taobao.com/service/getI ...

  9. 淘宝IP地址库采集器c#

    个人原创.欢迎转载.转载请注明出处.http://www.cnblogs.com/zetee/articles/3482085.html 采集器概貌,如下: 最近做一个项目,功能类似于CNZZ站长统计 ...

随机推荐

  1. 安卓gridview 网格,多行多列实现

    主Activity() private int[] image = { R.drawable.camera, R.drawable.wifi, R.drawable.temperature, R.dr ...

  2. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  3. Eclipse CDT: Shortcut to switch between .h and .cpp

    ctrl+ tab  is the default shortcut.You can change it in Window → Preferences → General → Keys: Toggl ...

  4. Ettercap几种好玩的姿势

    0x00 简单的arp欺骗   命令 ettercap -i 网卡 -Tq -M arp:remote /受害者ip/ /网关ip/ 以下是我欺骗192.168.1.102主机并模拟http-get请 ...

  5. POJ 2391 Ombrophobic Bovines

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 4 ...

  6. 解决 node-gyp rebuild 卡住 的问题

    node-gyp在编译前会首先尝试下载node的headers文件,像这样: gyp http GET https://nodejs.org/download/release/v6.8.1/node- ...

  7. ajax的循环

    一.业务需求 在开发中,当一个列表页面加载完成后,我需要根据列表每一项的id去服务器端获取对应的数据然后再把获取的数据赋给当前id对应的标签. 例如如下表格: 我有一系列的商品编号,我需要根据商品编号 ...

  8. 【C#】析构函数

    MSDN paper 析构函数 析构函数(destructor) 与构造函数相反,当对象脱离其作用域时(例如对象所在的函数已调用完毕),系统自动执行析构函数. 析构函数往往用来做“清理善后” 的工作( ...

  9. android studio 不能创建或者安装模拟器

    解决方法: (1).对与这类问题实在不行卸载然后安装,看看自己的网络是否把android studio下载的东西给墙了,墙了记得换网. (2).我安装andriod studio 2.2.2.0 版本 ...

  10. GreenDao的使用

    1.生成代码文件