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;
}
ip地址库 新浪,淘宝的更多相关文章
- WordPress使用淘宝IP地址库的API显示评论者的位置信息(二)
1 淘宝IP地址库的接口说明 在上一篇文章<WordPress使用淘宝IP地址库的API显示评论者的位置信息(一)>中,vfhky使用了新浪工具提供的这个IP接口显示博客评论者的位置信息. ...
- 淘宝ip地址库接口会导致TTFB时间变长,网站打开速度变慢
前一段时间闲来无事发现别人的网站上有显示当前用户城市的功能,就自己也整了一个 这是淘宝ip地址库调用方法 然后问题就出现了,网站打开速度慢的要死 用F12发现是TTFB太慢,然后百度了,发现了问题的原 ...
- 用淘宝ip地址库查ip
这是一个通过调用淘宝ip地址库实现ip地址查询的功能类 using System; using System.Collections.Generic; using System.Linq; using ...
- Delphi使用JSON解析调用淘宝IP地址库REST API 示例
淘宝IP地址库:http://ip.taobao.com,里面有REST API 说明. Delphi XE 调试通过,关键代码如下: var IdHTTP: TIdHTTP; RequestURL: ...
- 淘宝IP地址库采集器c#代码
这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下. 最近做一个项目,功能类似于CNZZ站长统计功能,要求显示Ip所在的省份市区/提供商等信息.网上的Ip纯真数据库,下载下来一看 ...
- 淘宝IP地址库API接口(PHP)通过ip获取地址信息
淘宝IP地址库网址:http://ip.taobao.com/ 提供的服务包括: 1. 根据用户提供的IP地址,快速查询出该IP地址所在的地理信息和地理相关的信息,包括国家.省.市和运营商. 2. 用 ...
- 淘宝IP地址库采集
作者:阿宝 更新:2016-08-31 来源:彩色世界(https://blog.hz601.org/2016/08/31/taobao-ip-sniffer/index.html) 简述 当初选择做 ...
- 淘宝IP地址库API地址
淘宝IP地址库:http://ip.taobao.com/instructions.php 接口说明 1. 请求接口(GET): http://ip.taobao.com/service/getI ...
- 淘宝IP地址库采集器c#
个人原创.欢迎转载.转载请注明出处.http://www.cnblogs.com/zetee/articles/3482085.html 采集器概貌,如下: 最近做一个项目,功能类似于CNZZ站长统计 ...
随机推荐
- 使用rsync同步目录
本文描述了linux下使用rsync单向同步两个机器目录的问题. 使用rsync同步后可以保持目录的一致性(含删除操作). 数据同步方式 从主机拉数据 备机上启动的流程 同步命令: rsync -av ...
- PHP中的数据库二、memcache
:first-child, ol li > :first-child, ul li ul:first-of-type, ol li ol:first-of-type, ul li ol:firs ...
- java中null 关键字
Java中,null是一个关键字,用来标识一个不确定的对象.null常见意义:一.null是代表不确定的对象 Java中,null是一个关键字,用来标识一个不确定的对象.因此可以将null赋给引用类 ...
- Linux系统下输出某进程内存占用信息的c程序实现
在实际工作中有时需要程序打印出某个进程的内存占用情况以作参考, 下面介绍一种通过Linux下的伪文件系统/proc 计算某进程内存占用的程序实现方法. 首先, 为什么会有所谓的 伪文件 呢. Linu ...
- [LeetCode] Design Snake Game 设计贪吃蛇游戏
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- Url重写——伪静态实现
简述: 在我们浏览网站的时候,很多都是以.html结尾的.难道这些都是静态网页么?其实不是的,它们很多是伪静态 那么什么是伪静态?顾名思义,就是假的静态页面.通过某种设置让你看成是静态的. Q:为何要 ...
- C与指针(结构体指针,函数指针,数组指针,指针数组)定义与使用
类型 普通指针 指针数组(非指针类型) 数组指针 结构体指针 函数指针 二重指针 定义方式 int *p; int *p[5]; int (*p)[5]; int a[3][5]; struct{.. ...
- [HTML5] FileReader对象
写在前面 前一篇文章介绍了HTML5中的Blob对象(详情戳这里),从中了解到Blob对象只是二进制数据的容器,本身并不能操作二进制,故本篇将对其操作对象FileReader进行介绍. FileRea ...
- 走进AngularJs 表单及表单验证
年底了越来越懒散,AngularJs的学习落了一段时间,博客最近也没更新.惭愧~前段时间有试了一下用yeoman构建Angular项目,感觉学的差不多了想做个项目练练手,谁知遇到了一系列问题.yeom ...
- Django初识
web框架 Web应用框架有助于减轻网页开发时共通性活动的工作负荷,例如许多框架提供数据库访问接口.标准样板以及会话管理等,可提升代码的可再用性.简单地说,就是你用别人搭建好的舞台来做表演,用别人做好 ...