php 从 ip 获取地区/城市
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6350626/
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 region/city from ip
提问by michele
how can I retrieve the region and the city from an ip? I have found this service: http://api.hostip.info/?ip=xyz.qwe.rty
如何从 ip 中检索地区和城市?我找到了这个服务:http: //api.hostip.info/?ip=xyz.qwe.rty
But It doesn't give me accurate info like this: http://www.ipaddresslocation.org/
但它没有给我这样的准确信息:http: //www.ipaddresslocation.org/
Do you know some free service? I need to retrieve this info with a php script, but I wouldn't install external library.
你知道一些免费服务吗?我需要使用 php 脚本检索此信息,但我不会安装外部库。
Thank you so much
非常感谢
回答by Ben Dowling
http://ipinfo.io, my service, gives you city, country and other related information about an IP:
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
}
Here's a PHP example: $details = json_decode(file_get_contents("http://ipinfo.io/".$_SERVER['REMOTE_ADDR']")); echo $details->city; // -> "Mountain View"
这是一个 PHP 示例: $details = json_decode(file_get_contents(" http://ipinfo.io/".$_SERVER['REMOTE_ADDR']")); echo $details->city; // -> "Mountain View"
回答by changeip
Free Geolocation APIhttp://ip-api.com/docs/
免费地理定位 API http://ip-api.com/docs/
You can get information about your ip-address, country, region, city, isp, organization.
您可以获得有关您的 IP 地址、国家、地区、城市、ISP、组织的信息。
Response formats and examples: XML, JSON, CSV, PHP.
响应格式和示例:XML、JSON、CSV、PHP。
Usage limits: Our system will automatically ban any IP addresses doing over 240 requests per minute.
使用限制:我们的系统将自动禁止每分钟超过 240 个请求的任何 IP 地址。
PHP Example
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';
}
回答by siniradam
You may try this.
你可以试试这个。
$tags = get_meta_tags('http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=94.55.180.139'); print $tags['city']; // city name
$tags = get_meta_tags('http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=94.55.180.139'); 打印 $tags['city']; // 城市名称
回答by Antonio
if you want to retrieve the city of an IP through http://www.ipaddresslocation.org/you follow this tutorial: https://www.facebook.com/Websitedevelopar/posts/468049883274297But you change the RegEx with this string:
如果您想通过http://www.ipaddresslocation.org/检索 IP 的城市,请按照本教程进行操作:https: //www.facebook.com/Websitedevelopar/posts/468049883274297但是您可以使用以下字符串更改 RegEx:
/<i>([a-z\s]+)\:[<\/i>\s]+<b>(.*)<\/b>/im
BYE
再见