使用 PHP 获取 IP 地址的国家/地区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3650006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Country of IP Address with PHP
提问by Tristan
Ideally I'm trying to put together a PHP script that I can query from any web browser and it returns the country of the IP address that accessed the PHP script.
理想情况下,我正在尝试将一个 PHP 脚本放在一起,我可以从任何 Web 浏览器查询该脚本,并返回访问该 PHP 脚本的 IP 地址所在的国家/地区。
Is this possible or is there a better solution?
这是可能的还是有更好的解决方案?
回答by sprain
There are free, easy APIs you can use, like those:
您可以使用免费、简单的 API,例如:
- http://ipinfodb.com/ip_location_api.php
- http://www.ipgeo.com/api/
- http://ip2.cc/
- http://www.geobytes.com/IpLocator.htm
- https://iplocate.io/
- and plenty others.
- http://ipinfodb.com/ip_location_api.php
- http://www.ipgeo.com/api/
- http://ip2.cc/
- http://www.geobytes.com/IpLocator.htm
- https://iplocate.io/
- 还有很多其他的。
Which one looks the most trustworthy is up to you :)
哪一个看起来最值得信赖取决于你:)
Otherwise, there are scripts which are based on local databases on your server. The database data needs to be updated regularly, though. Check out this one:
否则,有些脚本基于服务器上的本地数据库。但是,数据库数据需要定期更新。看看这个:
HTH!
哼!
Edit:And of course, depending on your project you might want to look at HTML5 Locationfeatures. You can't use them yet on the Internet Explorer (IE9 will support it, long way to go yet), but in case your audience is mainly on mobile devices or using Safari/Firefox it's definitely worth to look at it!
编辑:当然,根据您的项目,您可能想要查看HTML5 位置功能。您还不能在 Internet Explorer 上使用它们(IE9 将支持它,还有很长的路要走),但是如果您的受众主要使用移动设备或使用 Safari/Firefox,那么绝对值得一看!
Once you have the coordinates, you can reverse geocode them to a country code. Again there are APIs like this one:
获得坐标后,您可以将它们反向地理编码为国家/地区代码。同样有这样的 API:
- Example: http://ws.geonames.org/countryCode?lat=47.03&lng=10.2
- More APIs: http://www.geonames.org/export/ws-overview.html
- 示例:http: //ws.geonames.org/countryCode?lat=47.03&lng=10.2
- 更多 API:http: //www.geonames.org/export/ws-overview.html
Update, April 2013
Today I would recommend using Geocoder, a PHP library which makes it very easy to geocode ip addresses as well as postal address data.
更新,2013 年 4 月
今天我建议使用Geocoder,这是一个 PHP 库,它可以非常轻松地对 IP 地址和邮政地址数据进行地理编码。
***Update, September 2016
Since Google's privacy politics has changed, you can't use HTML5 Geolocation API if your server doesn't have HTPPS certificate or user doesn't allow you check his location. So now you can use user's IP and check in in PHP or get HTTPS certificate.
***更新,2016
年9 月由于 Google 的隐私政策发生了变化,如果您的服务器没有 HTPPS 证书或用户不允许您检查他的位置,则您不能使用 HTML5 Geolocation API。所以现在您可以使用用户的 IP 并签入 PHP 或获取 HTTPS 证书。
回答by Ben Dowling
There are various web APIs that will do this for you. Here's an example using my service, http://ipinfo.io:
有各种 Web API 可以为您执行此操作。这是使用我的服务http://ipinfo.io的示例:
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
echo $details->country; // -> "US"
Web APIs are a nice quick and easy solution, but if you need to do a lot of lookups then having an IP -> country database on your own machine is a better solution. MaxMind offer a free databasethat you can use with various PHP libraries, including GeoIP.
Web API 是一个很好的快速简便的解决方案,但是如果您需要进行大量查找,那么在您自己的机器上拥有一个 IP -> 国家/地区数据库是更好的解决方案。MaxMind 提供了一个免费数据库,您可以将其与各种 PHP 库一起使用,包括GeoIP。
回答by quano
You can download ip-tables from MaxMind:
您可以从 MaxMind 下载 ip-tables:
http://www.maxmind.com/app/geolite
http://www.maxmind.com/app/geolite
Import the CSV to your database (make sure to create an index for ip_long_from + ip_long_to). Then you can simply do:
将 CSV 导入您的数据库(确保为 ip_long_from + ip_long_to 创建索引)。然后你可以简单地做:
$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
");
回答by channasmcs
Here's an example using http://www.geoplugin.net/json.gp
这是使用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;
回答by Francis
You can get country from IP address with this location API
Code
代码
echo file_get_contents('https://ipapi.co/8.8.8.8/country/');
Response
回复
US
Here's a fiddle. Response is text
when you query a specific field e.g. country
here. No decoding needed, just plug it into your code.
这是一个小提琴。响应是text
当您查询特定字段时,例如country
此处。无需解码,只需将其插入您的代码即可。
P.S. If you want all the fields e.g. https://ipapi.co/8.8.8.8/json/
, the response is JSON
.
PS如果你想要所有的字段,例如https://ipapi.co/8.8.8.8/json/
,响应是JSON
。
回答by ivan.mylyanyk
I see there are a lot of answers, but no one of them mentions https://freegeoip.net/
我看到有很多答案,但没有一个提到https://freegeoip.net/
You're allowed up to 15,000 queries per hour
. I think that's enough in most of the cases. Otherwise, you should think about using local databases from MaxMind GeoIP.
你最多允许15,000 queries per hour
. 我认为这在大多数情况下就足够了。否则,您应该考虑使用MaxMind GeoIP 的本地数据库。
Freegeoip is open-sourceand you can find sources on github, deploy it locally and get rid of the query limits.
Freegeoip 是开源的,你可以在github上找到源代码,在本地部署它并摆脱查询限制。
Also, service provides latitude/longitude/timezoneand other useful stuff.
此外,服务提供纬度/经度/时区和其他有用的东西。
回答by Mohammad Anini
$country_code = trim(file_get_contents("http://ipinfo.io/{$ip_address}/country"))
will get you the country code.
会给你国家代码。
回答by John Franklin
Look at the GeoIP functions under "Other Basic Extensions." http://php.net/manual/en/book.geoip.php
查看“其他基本扩展”下的 GeoIP 功能。 http://php.net/manual/en/book.geoip.php
回答by che-azeh
Install and use PHP's GeoIP extensionif you can. On debian lenny:
如果可以,安装并使用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
and then try it in PHP:
然后在 PHP 中尝试:
$ip = $_SERVER['REMOTE_ADDR'];
$country = geoip_country_name_by_name($ip);
echo 'The current user is located in: ' . $country;
returns:
返回:
The current user is located in: Cameroon
回答by Kurt Van den Branden
You can try the services of https://www.geoip-db.comThey provide a JSON or JSONP-callback solution.
您可以尝试https://www.geoip-db.com的服务,他们提供了 JSON 或 JSONP 回调解决方案。
- 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>";
?>