【问题标题】:Reliable API to determine an ip address's city and country [closed]可靠的API来确定IP地址的城市和国家[关闭]
【发布时间】:2011-01-22 16:11:54
【问题描述】:

我想要一个可靠的 API,可用于根据 IP 地址确定城市和国家/地区。

我在 Google 上的搜索结果是 http://ipinfodb.com/ip_location_api.php。它看起来很完美,但我唯一担心的是这项服务的可靠性。谷歌有这样的东西吗?

我正在寻找有助于从 IP 确定位置的免费或付费 API。

【问题讨论】:

标签: php ip-geolocation


【解决方案1】:

谷歌有没有这样的东西。

不,Google 不提供类似的服务。 (请注意,这里声称它确实提供了它的答案,结果一无所获)。

帮助确定用户位置的任何其他免费或付费 API 他的IP

首先,这种服务的可靠性很难说。大多数服务依靠 ISP 来获取他们的数据,而 ISP 并不总是提供可靠的数据。此外,因为我假设您基本上关心用户的位置(而不是他的 IP 所属的位置),所以用户可以决定通过 VPN、Tor 或其他匿名者隐藏他的位置。

在您的一些 cmets 中,我看到您所说的可靠意味着 API 提供者的正常运行时间。如果是这样,我会推荐MaxMind's GeoIP2。首先,我已经看过这个网站很长时间了,它们经常更新(最后一次更新是 2014 年 4 月 1 日),此外,如果您非常害怕停机 - 您可以 download their database 并且不要依赖它们(也它们是免费的)。

我实际上很喜欢is less known and opensource 的另一项服务(它部分依赖于 MaxMind 的数据)。我只用过几次,所以我不能说它的正常运行时间,但我真的很喜欢他们的 API。它是如此简单,以至于很难想出更简单的方法:

http://www.telize.com/geoip/46.19.37.108 

或者像这样

http://www.telize.com/geoip/2a02:2770::21a:4aff:feb3:2ee

会给你

{
"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"
}

我认为这两项服务足以为您的访问者获取正常信息。但是一件重要的事情。如果您真的需要知道您的用户来自哪里 - 为什么不用html5 geolocation 询问他们?这是nice example,但您可以走得更远(询问他们位置是否正确,并让他们能够更改它)。我知道在您的情况下,您只需要 Country/City,但可能其他人会需要它。


附注2015 年 11 月。 现在 telize.com 需要付费才能使用,我找到了另一项目前免费的服务:

https://freegeoip.net/json/45.79.8.237
https://freegeoip.net/json/2600:3c00::f03c:91ff:fe98:c0f5

【讨论】:

  • 目前我用的是freegeoip.net,你也可以试试:geoplugin.net,可以帮到人。
  • 你能确认我有没有谷歌的服务可以提供给定IP地址的国家和国家代码。
【解决方案2】:

考虑使用geoplugin,它通过一行PHP 代码提供了最多的信息,但每次请求都需要连接到geoplugin.com 服务器(而且它是免费的)。

示例:

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

<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>&#36;</geoplugin_currencySymbol>
<geoplugin_currencySymbol_UTF8>$</geoplugin_currencySymbol_UTF8>
<geoplugin_currencyConverter>1</geoplugin_currencyConverter>
</geoPlugin>

其他选项

【讨论】:

  • 唯一的问题是它有一个限制。此外,您可能会被阻止。
  • 它不能使用 https。
【解决方案3】:

MaxMind 有一个精简版的 ip2country 和 ip2city 数据库: http://www.maxmind.com/app/geolitecity

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

如果您不介意自己托管数据库,它是免费的。 付费服务更准确。

他们也提供网络查询服务,但不是免费的: http://www.maxmind.com/app/web_services#country

【讨论】:

    【解决方案4】:

    从某种意义上说,您无法可靠地知道用户的位置。使用其他答案提供的提示将为您提供 IP 的地理位置,但无法知道美国纽约的 IP 地址没有通过 VPN 访问来自英国伦敦或英国出差的人是否有人在使用匿名服务,例如Tor

    【讨论】:

    • 谢谢弗兰克 - 我可能使用了错误的术语 - 当我的意思是可靠时 - 我的意思是说服务应该很快而且没有太多的停机时间。我知道我们永远无法 100% 正确确定位置。这只是为用户预先填写表格,如果城市/国家/地区错误,他可以更改它
    【解决方案5】:
    【解决方案6】:

    每个 IP 地理位置都取决于 ISP 提交的记录。这永远不会完全可靠,因为许多 ISP 会提交其办公室的位置,而不是可能位于不同城市甚至国家/地区的子网的物理位置。

    【讨论】:

    • 谢谢 SF - 我可能使用了错误的术语 - 当我的意思是可靠时 - 我的意思是说服务应该很快并且没有太多的停机时间。我知道我们永远无法 100% 正确确定位置。这只是为用户预先填写表格,如果城市/国家/地区错误,他可以更改它
    【解决方案7】:

    我用过http://www.hostip.info/,发现它真的很好。

    【讨论】:

    • 刚刚检查了使用 2 年的 IP。它甚至无法找到我的国家。不确定它是否适合国际查询...
    • 我发现ip-api.com/docs/api:json 真的很好:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    相关资源
    最近更新 更多