php 确定 IP 地址所在城市和国家的可靠 API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2362413/
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
Reliable API to determine an ip address's city and country
提问by Gublooo
I want a reliable API that can be used to determine a city and country from an IP address.
我想要一个可靠的 API,可用于从 IP 地址确定城市和国家/地区。
My search on Google led to http://ipinfodb.com/ip_location_api.php. It seems perfect but my only concern is the reliability of this service. Does google have anything like this?
我在 Google 上的搜索导致了http://ipinfodb.com/ip_location_api.php。这看起来很完美,但我唯一担心的是这项服务的可靠性。谷歌有这样的东西吗?
I'm looking for free or paid APIs that help determine location from IP.
我正在寻找帮助从 IP 确定位置的免费或付费 API。
采纳答案by codaddict
I've used http://www.hostip.info/and found it really good.
我使用过http://www.hostip.info/并发现它非常好。
回答by Kjetil Watnedal
MaxMind has a lite version of their ip2country and ip2city databases: http://www.maxmind.com/app/geolitecity
MaxMind 有一个精简版的 ip2country 和 ip2city 数据库:http: //www.maxmind.com/app/geolitecity
http://www.maxmind.com/app/geolitecountry
http://www.maxmind.com/app/geolitecountry
If you don't mind hosting the database yourself, it's free. The paid services are more accurate.
如果您不介意自己托管数据库,它是免费的。付费服务更准确。
They also provide web services for querying, but it is not free: http://www.maxmind.com/app/web_services#country
他们还提供用于查询的网络服务,但它不是免费的:http: //www.maxmind.com/app/web_services#country
回答by Chandra Nakka
Consider using geoplugin, it provides the most information from a single line PHP code, but requires a connection to the geoplugin.com server for every request (and it is free).
考虑使用geoplugin,它从单行 PHP 代码中提供最多的信息,但需要为每个请求连接到 geoplugin.com 服务器(并且它是免费的)。
Examples :
例子 :
Json : http://www.geoplugin.net/json.gp?ip=74.125.236.174
JSON : http://www.geoplugin.net/json.gp?ip=74.125.236.174
{
geoplugin_request: "74.125.236.174",
geoplugin_status: 200,
geoplugin_city: "Mountain View",
geoplugin_region: "CA",
geoplugin_areaCode: "650",
geoplugin_dmaCode: "807",
geoplugin_countryCode: "US",
geoplugin_countryName: "United States",
geoplugin_continentCode: "NA",
geoplugin_latitude: "37.419201",
geoplugin_longitude: "-122.057404",
geoplugin_regionCode: "CA",
geoplugin_regionName: "California",
geoplugin_currencyCode: "USD",
geoplugin_currencySymbol: "$",
geoplugin_currencySymbol_UTF8: "$",
geoplugin_currencyConverter: 1
}
Xml : http://www.geoplugin.net/xml.gp?ip=74.125.236.174
xml:http: //www.geoplugin.net/xml.gp?ip =74.125.236.174
<geoPlugin>
<script/>
<geoplugin_request>74.125.236.174</geoplugin_request>
<geoplugin_status>200</geoplugin_status>
<geoplugin_city>Mountain View</geoplugin_city>
<geoplugin_region>CA</geoplugin_region>
<geoplugin_areaCode>650</geoplugin_areaCode>
<geoplugin_dmaCode>807</geoplugin_dmaCode>
<geoplugin_countryCode>US</geoplugin_countryCode>
<geoplugin_countryName>United States</geoplugin_countryName>
<geoplugin_continentCode>NA</geoplugin_continentCode>
<geoplugin_latitude>37.419201</geoplugin_latitude>
<geoplugin_longitude>-122.057404</geoplugin_longitude>
<geoplugin_regionCode>CA</geoplugin_regionCode>
<geoplugin_regionName>California</geoplugin_regionName>
<geoplugin_currencyCode>USD</geoplugin_currencyCode>
<geoplugin_currencySymbol>$</geoplugin_currencySymbol>
<geoplugin_currencySymbol_UTF8>$</geoplugin_currencySymbol_UTF8>
<geoplugin_currencyConverter>1</geoplugin_currencyConverter>
</geoPlugin>
Other options
其他选项
回答by Ravia
回答by SF.
Every IP geolocation depends on records submitted by the ISP. This will never be completely reliable because many ISPs submit location of their office instead of physical locations of their subnets which may be in a different city or even country.
每个 IP 地理位置都取决于 ISP 提交的记录。这永远不会完全可靠,因为许多 ISP 会提交其办公室的位置,而不是其子网的物理位置,这些物理位置可能位于不同的城市甚至国家/地区。
回答by Frank Shearar
In a sense, you can't reliably know the location of a user. Using the tips the other answers provide will give you the geolocation of an IP, but there's no way to know that that IP address in New York USA isn't being accessed through a VPN from someone on a business trip to London, UK, or whether someone's using an anonymizing service like Tor.
从某种意义上说,您无法可靠地知道用户的位置。使用其他答案提供的提示将为您提供 IP 的地理定位,但无法知道美国纽约的 IP 地址没有被出差到英国伦敦的人通过 VPN 访问,或者是否有人在使用Tor等匿名服务。
回答by Salvador Dali
Does google have anything like this.
谷歌有没有这样的东西。
No, Google does not provide a service similar to this. (note that the answer here which claims that it does provide it, leads to nowhere).
不,Google 不提供与此类似的服务。(请注意,此处声称它确实提供了答案的答案一无所获)。
Any other Free or paid API's that help determine user's location from his IP
任何其他帮助从用户的 IP 确定用户位置的免费或付费 API
First of all it is hard to speak about reliability of such service. Most of the services rely on ISP to get their data and ISP not always provide reliable data. Moreover because I assume that you basically cares about the location of your user (and not what location his IP belongs), the user can decide to hide his location either through VPN, Tor or other anonymisers.
首先,很难谈论这种服务的可靠性。大多数服务都依赖 ISP 来获取数据,而 ISP 并不总是提供可靠的数据。此外,因为我假设您基本上关心用户的位置(而不是他的 IP 所属的位置),所以用户可以决定通过 VPN、Tor 或其他匿名程序隐藏他的位置。
In some of your comments, I have seen that by reliable you means uptime of the API provider. If so, than I would recommend MaxMind's GeoIP2. First of all, I have seen this site for long time, they are updated pretty often (last update 1-st April 2014), moreover if you are so afraid of downtime - you can download their databaseand do not rely on them (also they are free).
在您的一些评论中,我看到可靠意味着 API 提供者的正常运行时间。如果是这样,我会推荐MaxMind 的 GeoIP2。首先,我已经看到这个站点很长时间了,它们经常更新(上次更新时间为 2014 年 4 月 1 日),此外,如果您非常害怕停机 - 您可以下载他们的数据库,不要依赖它们(也他们是免费的)。
Another service I actually like is less known and opensource(it partially relies on data from MaxMind). I have used it only a few times, so I can not say about it's uptime, but I really like theirs API. It is so simple, that it is hard to come up with simpler:
我真正喜欢的另一项服务鲜为人知并且是开源的(它部分依赖于 MaxMind 的数据)。我只用过几次,所以我不能说它的正常运行时间,但我真的很喜欢他们的 API。太简单了,很难想出更简单的:
http://www.telize.com/geoip/46.19.37.108
or like this
或者像这样
http://www.telize.com/geoip/2a02:2770::21a:4aff:feb3:2ee
will give you
会给你
{
"country":"Netherlands",
"dma_code":"0",
"timezone":"Europe\/Amsterdam",
"area_code":"0",
"ip":"46.19.37.108",
"asn":"AS196752",
"continent_code":"EU",
"isp":"Tilaa V.O.F.",
"longitude":5.75,
"latitude":52.5,
"country_code":"NL",
"country_code3":"NLD"
}
I think that these two services are pretty enough to get normal information for your visitors. But one important thing. If you really need to know where you users came from - why not to ask them with html5 geolocation? Here is a nice example, but you can go further (ask them if the location is correct and give them ability to change it). I know that in your case you need only Country/City, but may be other people will need it.
我认为这两项服务足以为您的访问者获取正常信息。但是一件很重要的事情。如果您真的需要知道您的用户来自哪里 - 为什么不使用html5 地理定位来询问他们呢?这是一个很好的例子,但你可以更进一步(询问他们位置是否正确并让他们能够更改它)。我知道在您的情况下,您只需要国家/城市,但其他人可能会需要它。
P.S. Nov 2015.Now that telize.com costs money to use, I found another service that is currently free:
PS 2015 年 11 月。现在使用 telize.com 需要花钱,我发现了另一项目前免费的服务:
https://freegeoip.net/json/45.79.8.237
https://freegeoip.net/json/2600:3c00::f03c:91ff:fe98:c0f5

