通过GeoIP获取ip所属地 (国家,城市,时区,邮编,经纬度等)
来源:互联网 发布:simulink算法模型 编辑:程序博客网 时间:2024/04/28 00:38
来源: 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.gz
- include("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 database
- geoip_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 ValuekeyYes<empty>API key provided with your free account.ipNoClient IPIP addressformatNorawraw, xml, jsoncallbackNo<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)
- 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(!emptyempty($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 (!emptyempty($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 (!emptyempty($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 FormatDB11.60123.123.123ISO country code, country nameCSVBINDB317.11123.123.123ISO country code, country name, state, cityCSVBINDB521.40123.123.123ISO country code, country name, state, city, latitude, longitudeCSVBINDB922.76123.123.123ISO country code, country name, state, city, latitude, longitude, ZIP codesCSVBINDB1123.28123.123.123ISO country code, country name, state, city, latitude, longitude, ZIP codes, time zoneCSVBIN
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版)
http://justcoding.iteye.com/blog/986355
http://blog.csdn.net/wangjia55/article/details/7843874
0 0
- 通过GeoIP获取ip所属地 (国家,城市,时区,邮编,经纬度等)
- 通过GeoIP获取ip所属地 (国家,城市,时区,邮编,经纬度等)
- 通过邮编获取城市
- geoip+php示例:通过ip,获取国家名称和代码
- 通过输入IP地址查询国家、城市、所有者等信息。
- geoip+php演示样例:通过ip,获取国家名称和代码
- Redis应用:查找IP所属城市以及国家
- 通过经纬度获取城市名
- php通过经纬度获取城市
- MaxMind GeoIP --- 通过IP地址获取地理信息
- Geoip geoip-api-php 库包使用 - 通过ip 找到国家
- 通过IP(城市、国家)检测地理位置
- 通过IP(城市、国家)检测地理位置
- 根据IP获取所在的国家城市
- 获取指定(访客)IP的所有信息,地址、邮政编码、国家、经纬度等的API
- 获取指定(访客)IP的所有信息,地址、邮政编码、国家、经纬度等的API
- 获取指定(访客)IP的所有信息,地址、邮政编码、国家、经纬度等的API
- 天气获取通过经纬度或城市信息
- iOS中常见错误
- 模板方法
- Java web.xml session-config 属性配置
- iOS上传错误 ERROR ITMS-90049
- AFNetworking 3.0迁移指南
- 通过GeoIP获取ip所属地 (国家,城市,时区,邮编,经纬度等)
- 一招辨认sql中的varchar和char
- bzoj-3585 mex
- iOS开发-文件管理(一)
- [工具设置]xp iis连接数破解
- 图像滤镜艺术---Photoshop实现Instagram之Nashville滤镜
- HDU 2612 Find a way题解
- [LeetCode-38] Count and Say
- postgresql mode 函数