原文连接地址: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. JQuery plugin ---- simplePagination.js API

    CSS Themes "light-theme" "dark-theme" "compact-theme" How To Use Step ...

  2. Windows下SVN服务器的搭建步骤

    1.下载svn服务端和客户端 服务端VISUALSVN SERVER:https://www.visualsvn.com/ 客户端TortoiseSVN:https://tortoisesvn.net ...

  3. Spark SQL 之 DataFrame

    Spark SQL 之 DataFrame 转载请注明出处:http://www.cnblogs.com/BYRans/ 概述(Overview) Spark SQL是Spark的一个组件,用于结构化 ...

  4. PHP中的数据库一、MySQL优化策略综述

    前些天看到一篇文章说到PHP的瓶颈很多情况下不在PHP自身,而在于数据库.我们都知道,PHP开发中,数据的增删改查是核心.为了提升PHP的运行效率,程序员不光需要写出逻辑清晰,效率很高的代码,还要能对 ...

  5. Spark运行模式与Standalone模式部署

    上节中简单的介绍了Spark的一些概念还有Spark生态圈的一些情况,这里主要是介绍Spark运行模式与Spark Standalone模式的部署: Spark运行模式 在Spark中存在着多种运行模 ...

  6. Wiki安装(PHP +Sqlite+Cache)

    前期准备 PHP http://windows.php.net/download   WinCache Extension for PHP URL:http://sourceforge.net/pro ...

  7. svm心得体会(2)

    昨天和李老师讨论一会还是有所得的,虽然我发誓要早睡又泡汤了,又无原则晚睡了. 总结一下有这么几点心得认识: (1)MATLAB再带的svm工具箱得不到参数,必须在路径中添加libsvm工具箱,安装在M ...

  8. ubuntu 14.04安装pypcap

    直接sudo apt-get install python-pypcap即可 How to install python-pypcap on Ubuntu 12.04 (Precise Pangoli ...

  9. 用 ElementTree 在 Python 中解析 XML

    用 ElementTree 在 Python 中解析 XML 原文: http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python- ...

  10. jQuery中的100个技巧

      1.当document文档就绪时执行JavaScript代码. 我们为什么使用jQuery库呢?原因之一就在于我们可以使jQuery代码在各种不同的浏览器和存在bug的浏览器上完美运行. < ...