获取IP地址与PHP的国家

理想情况下,我正在尝试将可从任何Web浏览器查询的PHP脚本放在一起,并返回访问PHP脚本的IP地址的国家/地区。

这是可能的还是有更好的解决scheme?

我使用hostip.info API 。 远程请求很慢所以要小心:)

有免费的,简单的API可以使用,如:

哪一个看起来最值得信赖是由你决定的:)

否则,在您的服务器上有基于本地数据库的脚本。 但是,数据库数据需要定期更新。 看看这个:

HTH!

编辑:当然,根据您的项目,你可能想看看HTML5的位置function。 你不能在Internet Explorer上使用它们(IE9将支持它,还有很长的路要走),但是如果你的用户主要是在移动设备上,或者使用Safari / Firefox,那绝对值得一看!

获得坐标后,您可以将其转换为国家/地区代码。 再次有这样的API:

  • 例如: http : //ws.geonames.org/countryCode?lat=47.03&lng=10.2
  • 更多的API: http : //www.geonames.org/export/ws-overview.html

更新,2013年4月
今天,我会推荐使用Geocoder ,一个PHP库,这使得它非常容易地理编码的IP地址以及邮政地址数据。

***更新,2016年9月
由于Google的隐私政策发生了变化,如果您的服务器没有HTPPS证书或者用户不允许您检查其位置,则无法使用HTML5地理定位API。 因此,现在您可以使用用户的IP并在PHP中签入或获得HTTPS证书。

有各种各样的networkingAPI将为您做这个。 这是一个使用我的服务的例子, http : //ipinfo.io :

 $ip = $_SERVER['REMOTE_ADDR']; $details = json_decode(file_get_contents("http://ipinfo.io/{$ip}")); echo $details->country; // -> "US" 

Web API是一个非常简单快速的解决scheme,但是如果你需要做大量的查询,那么在你自己的机器上使用IP-> country数据库是一个更好的解决scheme。 MaxMind提供了一个免费的数据库 ,您可以使用各种PHP库,包括GeoIP 。

你可以从MaxMind下载ip-tables:

http://www.maxmind.com/app/geolite

将CSV导入数据库(确保为ip_long_from + ip_long_to创build索引)。 那么你可以简单地做:

 $iplong = ip2long($_SERVER['REMOTE_ADDR']); // should use mysqli with prepared statements etc, but just an example mysql_query(" SELECT country FROM ip_table WHERE $iplong BETWEEN ip_long_from AND ip_long_to LIMIT 1 "); 

这是一个使用http://www.geoplugin.net/json.gp的例子;

 $ip = $_SERVER['REMOTE_ADDR']; $details = json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip={$ip}")); echo $details; 

这是一个API这个API

  echo file_get_contents('https://ipapi.co/8.8.8.8/country/'); 

响应

  US 

这是一个小提琴 。 当您查询某个特定字段(如country时,响应是text 。 没有解码需要,只需将其插入到您的代码。

PS如果你想要所有的领域,例如https://ipapi.co/8.8.8.8/json/ ,响应是JSON

 $country_code = trim(file_get_contents("http://ipinfo.io/{$ip_address}/country")) 

会得到你的国家代码。

查看“其他基本扩展”下的GeoIPfunction。 http://php.net/manual/en/book.geoip.php

我看到有很多答案,但没有一个提到https://freegeoip.net/

15,000 queries per hour最多允许15,000 queries per hour 。 我认为在大多数情况下这已经足够了。 否则,您应该考虑使用MaxMind GeoIP的本地数据库。

Freegeoip是开源的 ,你可以在github上find源代码,在本地部署,摆脱查询限制。

此外,服务提供了纬度/经度/时区和其他有用的东西。

我发现这个博客文章非常有帮助。 它拯救了我的一天。 http://www.techsirius.com/2014/05/simple-geo-tracking-system.html

基本上你需要安装IP数据库表,然后执行mysql查询IP的位置。

 <?php include 'config.php'; mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) OR die(mysql_error()); mysql_select_db(DB_NAME) OR die(mysql_error()); $ip = $_SERVER['REMOTE_ADDR']; $long = sprintf('%u', ip2long($ip)); $sql = "SELECT * FROM `geo_ips` WHERE '$long' BETWEEN `ip_start` AND `ip_end`"; $result = mysql_query($sql) OR die(mysql_error()); $ip_detail = mysql_fetch_assoc($result); if($ip_detail){ echo $ip_detail['country']."', '".$ip_detail['state']."', '".$ip_detail['city']; } else{ //Something wrong with IP } 

这是另一个免费的地理定位请求API: http : //geoip.nekudo.com

响应采用JSON格式。 另外,API源代码在github上可用,以防您需要设置您自己的API。

如果可以的话,安装并使用PHP的GeoIP扩展 。 在debian lenny上:

 sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz sudo gunzip GeoLiteCity.dat.gz sudo mkdir -v /usr/share/GeoIP sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat sudo apt-get install php5-geoip # or sudo apt-get install php-geoip for PHP7 

然后在PHP中尝试:

 $ip = $_SERVER['REMOTE_ADDR']; $country = geoip_country_name_by_name($ip); echo 'The current user is located in: ' . $country; 

收益:

 The current user is located in: Cameroon 

您可以使用https://ip-api.io/获取国家名称,城市名称,经度和纬度。; 它支持IPv6。

作为奖励,它会告诉如果IP地址是一个tor节点,公共代理或垃圾邮件。

PHP代码:

 $result = json_decode(file_get_contents('http://ip-api.io/json/64.30.228.118')); var_dump($result); 

输出:

 { "ip": "64.30.228.118", "country_code": "US", "country_name": "United States", "region_code": "FL", "region_name": "Florida", "city": "Fort Lauderdale", "zip_code": "33309", "time_zone": "America/New_York", "latitude": 26.1882, "longitude": -80.1711, "metro_code": 528, "suspicious_factors": { "is_proxy": false, "is_tor_node": false, "is_spam": false, "is_suspicious": false } 

使用小部件www.feedsalive.com ,以获取国家信息和最后一页的信息。

您可以尝试https://www.geoip-db.com的服务。它们提供JSON或JSONPcallback解决scheme。;

  • JSON: https : //geoip-db.com/json/
  • JSONP: https ://geoip-db.com/json/geoip.php ? jsonp = callback
 <?php $json = file_get_contents("https://www.geoip-db.com/json"); $data = json_decode($json); print $data->country_code . "<br>"; print $data->country_name . "<br>"; print $data->state . "<br>"; print $data->city . "<br>"; print $data->postal . "<br>"; print $data->latitude . "<br>"; print $data->longitude . "<br>"; print $data->IPv4 . "<br>"; ?> 

我在IPLocate.io上运行这个服务,你可以通过一个简单的调用来免费获得这个服务:

 <?php $res = file_get_contents('https://www.iplocate.io/api/lookup/8.8.8.8'); $res = json_decode($res); echo $res->country; // United States echo $res->continent; // North America echo $res->latitude; // 37.751 echo $res->longitude; // -97.822 var_dump($res); 

$res对象将包含您的地理位置字段,如countrycity

查看文档以获取更多信息。

如果你需要有一个好的和更新的数据库,为了获得更多的性能,而不是从你的网站请求外部服务, 这里有一个下载更新和准确的数据库的好地方。

我推荐这个,因为根据我的经验,甚至包括我的IP,其中大多数其他数据库/ APIS没有包括/报告我的位置,除了这个服务 ,指示我到这个数据库的服务 ,包括城市和国家的位置,工作出色。
(我的IP位置来自“伊朗”“拉什特”市,当时我正在testing,IP是: 2.187.21.235等于45815275

因此,如果您不确定哪项服务更准确,我build议使用这些服务:

数据库: http://lite.ip2location.com/databases : http://lite.ip2location.com/databases

API服务: http://ipinfodb.com/ip_location_api.php : http://ipinfodb.com/ip_location_api.php