通过GeoIP获取ip所属地 (国家,城市,时区,邮编,经纬度等)
来源: http://www.phpandstuff.com/articles/geoip-country-lookup-with-php
GeoIP + PHP
<?php//计时开始function utime() {$time = explode( " ", microtime() );$usec = (double)$time[0];$sec = (double)$time[1];return $usec + $sec;}$startTimes = utime();// include the php script// wget -c http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz// gunzip GeoIP.dat.gzinclude("geoip.inc");// open the geoip database$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);// 获取国家代码$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);echo "Your country code is: <strong>$country_code</strong> <br />";// 获取国家名称$country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']);echo "Your country name is: <strong>$country_name</strong> <br />";// close the databasegeoip_close($gi);//运行结束时间$endTimes = utime();$runTimes = sprintf( '%0.4f', ( $endTimes - $startTimes ) );echo "Processed in " . $runTimes . "second.";
?>
注:在本地测试的话因 为$_SERVER['REMOTE_ADDR']和$_SERVER['REMOTE_ADDR']可能是127.0.0.1,所 以输出的内容为空。可以自己带入IP测试
或者 使用某网站的API
API 1.
1. 返回文字
http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true
2. 返回图片
<IMG SRC="http://api.hostip.info/flag.php?ip=12.215.42.19" ALT="IP Address Lookup">
API 2. (需要申请api key ,免费的,类似google)
城市:
http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&ip=74.125.45.100
国家(更快) :
http://api.ipinfodb.com/v3/ip-country/?key=<your_api_key>&ip=74.125.45.100
Parameter | Required | Default | Value |
---|---|---|---|
key | Yes | <empty> | API key provided with your free account. |
ip | No | Client IP | IP address |
format | No | raw | raw, xml, json |
callback | No | <empty> | Required when using json callback. |
Please use the appropriate API for your needs. You can help us keep the load low on our servers by making sure that :
- If you only need the country name, avoid using the city precision API.
- If you track your visitors, avoid querying our API for all your page views (you can store the geolocation in a cookie, see below for an example)
API | Precision | Timezone | Domains lookups |
---|---|---|---|
ip-city | City | Yes | Yes |
ip-country | Country | No | Yes |
Please use the appropriate API for your needs. You can help us keep the load low on our servers by making sure that :
- If you only need the country name, avoid using the city precision API.
- If you track your visitors, avoid querying our API for all your page views (you can store the geolocation in a cookie, see below for an example)
使用类:
<?php
final class ip2location_lite{protected $errors = array();protected $service = 'api.ipinfodb.com';protected $version = 'v3';protected $apiKey = '';public function __construct(){}public function __destruct(){}public function setKey($key){if(!empty($key)) $this->apiKey = $key;}public function getError(){return implode("\n", $this->errors);}public function getCountry($host){return $this->getResult($host, 'ip-country');}public function getCity($host){return $this->getResult($host, 'ip-city');}private function getResult($host, $name){$ip = @gethostbyname($host);if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');try{$response = @new SimpleXMLElement($xml);foreach($response as $field=>$value){$result[(string)$field] = (string)$value;}return $result;}catch(Exception $e){$this->errors[] = $e->getMessage();return;}}$this->errors[] = '"' . $host . '" is not a valid IP address or hostname.';return;}
}
?>
<?
include('ip2locationlite.class.php');//Load the class
$ipLite = new ip2location_lite;
$ipLite->setKey('<your_api_key>');//Get errors and locations
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
$errors = $ipLite->getError();//Getting the result
echo "<p>\n";
echo "<strong>First result</strong><br />\n";
if (!empty($locations) && is_array($locations)) {foreach ($locations as $field => $val) {echo $field . ' : ' . $val . "<br />\n";}
}
echo "</p>\n";//Show errors
echo "<p>\n";
echo "<strong>Dump of all errors</strong><br />\n";
if (!empty($errors) && is_array($errors)) {foreach ($errors as $error) {echo var_dump($error) . "<br /><br />\n";}
} else {echo "No errors" . "<br />\n";
}
echo "</p>\n";
数据库版:
IP geolocation databases download
Updated Mar 5 2011
Database | Uncompressed Size(MB) | IP Precision | Data Provided | Format |
---|---|---|---|---|
DB1 | 1.60 | 123.123.123 | ISO country code, country name | CSV BIN |
DB3 | 17.11 | 123.123.123 | ISO country code, country name, state, city | CSV BIN |
DB5 | 21.40 | 123.123.123 | ISO country code, country name, state, city, latitude, longitude | CSV BIN |
DB9 | 22.76 | 123.123.123 | ISO country code, country name, state, city, latitude, longitude, ZIP codes | CSV BIN |
DB11 | 23.28 | 123.123.123 | ISO country code, country name, state, city, latitude, longitude, ZIP codes, time zone | CSV BIN |
key: c9dcc88453e33a9e63ebad8d65f91583e87abd8185dd95f09fbeef6c62264f7d
其他参考
http://pecl.php.net/package/geoip
http://www.geoiptool.com/
http://www.hostip.info/use.html
http://phpweby.com/software/ip2country
http://www.ipinfodb.com/index.php
转帖注明出处:http://justcoding.iteye.com/blog/986355
本站链接:
php 显示ip所属地 (qq版)
通过GeoIP获取ip所属地 (国家,城市,时区,邮编,经纬度等)相关推荐
- Python通过GeoIP获取IP信息(国家、城市、经纬度等)
IP地址信息是非常重要的情报信息,通过IP可以定位到该IP所在的国家.城市.经纬度等. 获取IP信息的方式有很多,很多服务商都提供了相应的地址库或API接口服务. 如国内的ipip.net,国外的ip ...
- php使用淘宝IP库接口获取 IP所属地和运营商
php使用淘宝IP库接口获取 IP所属地和运营商 代码: ---- root@ops-docker-1:/tmp/IP_txt/get_random_line/contrast# cat taobao ...
- Java 获取ip地址、省份城市、城市ip
1.获取ip :使用request.getHeader("x-forwarded-for")获取真实IP,x-forwarded-for的值并不止一个,而是一串IP值,获取为空时, ...
- python - python 获取ip信息(国家、城市等)
Python代码: import geoip2.databasedef from_ip_get_info(sip):reader = geoip2.database.Reader('/opt/work ...
- 网站通过nginx设置黑/白名单IP限制、国家城市IP访问限制
一.黑/白名单IP限制访问配置 nginx配置黑白名单有好几种方式,这里只介绍常用的两种方法. 1.第一种方法:allow.deny deny和allow指令属于ngx_http_access_mod ...
- GeoIP获取IP的所在地区
1.下载文件 官网地址:https://dev.maxmind.com/?lang=en 最下方从Products→GeoLite2 Free Geolocation Data 进入 进入之后,按照登 ...
- Go 使用IP纯真库获取IP对应的国家、省、市
创作不易感谢支持. 一条主写Go和PHP的小菜鸟.平常有时间喜欢自己写点东西,如有不对的地方,欢迎大佬指点. 个人博客:太阳上的雨天 地址:blog.caixiaoxin.cn 善于分享,希望有助他人 ...
- php 显示ip所属地 (qq版)
本站关联: 通过GeoIP获取ip所属地 注:保存为.php的文件,纯真数据库 qqwry.dat 文件应该放在同一目录下,否则会报错! 代码: <?php //这些是核心部分,可以写到一个单独 ...
- ip地址转换数字函数 iton_DataLakeAnalytics: 解析IP地址对应的国家城市的函数
Data Lake Analytics 作为云上数据处理的枢纽,最近加入了通过IP地址查找对应的国家.省份.城市.ISP的函数, 今天带大家体验一下. 函数详细介绍 本次一共添加了下面这些函数: ip ...
最新文章
- 树莓派:人在太空,刚下火箭,诚招小于19岁的代码开发者
- mybatis mysql Druid_mybatis+Druid连接池的问题
- 管家服务器未显示数据库启动,服务器受到后缀为gupwp的**,10年数据库等大神解救...
- 快速排序C实现(阿里巴巴 2012年全国校招笔试题)
- 类和对象_月隐学python第15课
- 王者荣耀故事站小程序源码/含vue后台
- HTML(1)--- HTML5教程
- php-ews发送邮件,使用php-ews在特定日期之后收到电子邮件(Exchange Web服务)
- 问题:使用pandas中的DataFrame写入csv文件多出一行unnamed,如何解决呢??
- 假如 AI 时代少了软件可信,我们将面临什么?
- grub rescue修复
- MySQL-5.6.14-winx64的免安装配置方法
- datagrid 表格数据填充方式
- golang httpServer对于keepAlive长连接的处理方式
- 重磅!《中华人民共和国个人信息保护法》今日起施行!
- 计算机系统常见故障及处理,电脑常见故障以及解决方案都在这里!
- git的使用、ssh生成、github、Git分支操作
- SQL DUMB Series-3 4 通关过程分享(sql注入学习DAY2)
- VUE项目中CSS设置动态宽度的方法
- Lock wait timeout exceeded; try restarting transaction
热门文章
- nodejs ftp文件服务器,node.js自动上传ftp的脚本分享
- php+loaction+框架,【集锦】nginx【php,location,alias,504】
- InceptionNet V3整理总结
- h5键盘把页面顶走了_微信h5开发的坑
- c调用python代码找不到模块_构建 Python C 扩展模块
- python多线程编程_python多线程编程(1): python对多线程的支持
- yolo mark的使用(转)
- python画roc曲线_使用Python画ROC曲线以及AUC值
- linux udp 端口映射,Linux下的UDP/TCP端口映射(netcat and socat)
- poi为什么所有celltype都是string_不是所有向日葵都向阳,你知道为什么吗