24

如何从 ip 中检索区域和城市?我找到了这项服务: http ://api.hostip.info/?ip=xyz.qwe.rty

但它没有给我这样的准确信息: http ://www.ipaddresslocation.org/

你知道一些免费的服务吗?我需要使用 php 脚本检索此信息,但我不会安装外部库。

非常感谢

4

5 回答 5

36

我的服务http://ipinfo.io为您提供有关 IP 的城市、国家和其他相关信息:

$ curl ipinfo.io/8.8.8.8
{
  "ip": "8.8.8.8",
  "hostname": "google-public-dns-a.google.com",
  "loc": "37.385999999999996,-122.0838",
  "org": "AS15169 Google Inc.",
  "city": "Mountain View",
  "region": "CA",
  "country": "US",
  "phone": 650
}

这是一个 PHP 示例:

$details = json_decode(file_get_contents("http://ipinfo.io/".$_SERVER['REMOTE_ADDR']"));
echo $details->city; // -> "Mountain View"
于 2013-07-14T02:54:59.427 回答
20

免费地理定位 API http://ip-api.com/docs/

您可以获得有关您的 IP 地址、国家、地区、城市、ISP、组织的信息。

响应格式和示例:XML、JSON、CSV、PHP。

使用限制:我们的系统将自动禁止每分钟执行超过 240 个请求的任何 IP 地址。

PHP 示例

$ip = $_SERVER['REMOTE_ADDR']; 
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
if($query && $query['status'] == 'success') {
echo 'My IP: '.$query['query'].', '.$query['isp'].', '.$query['org'].', '.$query ['country'].', '.$query['regionName'].', '.$query['city'].'!';
} else {
echo 'Unable to get location';
}
于 2014-03-14T10:48:01.400 回答
19

这是我最喜欢的:

信息数据库

于 2011-06-14T22:03:33.460 回答
1

你可以试试这个。

$tags = get_meta_tags('http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=94.55.180.139'); 打印 $tags['city']; // 城市名称

于 2012-09-25T10:58:13.510 回答
1

如果您想通过http://www.ipaddresslocation.org/检索 IP 所在的城市,请遵循本教程:https ://www.facebook.com/Websitedevelopar/posts/468049883274297 但是您使用此字符串更改正则表达式:

/<i>([a-z\s]+)\:[<\/i>\s]+<b>(.*)<\/b>/im

再见

于 2013-07-02T16:53:19.193 回答