get region/city from ip [closed]

走远了吗. 提交于 2019-12-17 17:33:35

问题


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

But It doesn't give me accurate info like this: 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.

Thank you so much


回答1:


This is my favourite:

ipinfodb




回答2:


http://ipinfo.io, my service, gives you city, country and other related information about an 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:

$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
echo $details->city; // -> "Mountain View"



回答3:


Free Geolocation API http://ip-api.com/docs/

You can get information about your ip-address, country, region, city, isp, organization.

Response formats and examples: XML, JSON, CSV, PHP.

Usage limits: Our system will automatically ban any IP addresses doing over 240 requests per minute.

PHP Example

$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';
}



回答4:


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




回答5:


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/468049883274297 But you change the RegEx with this string:

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

BYE



来源:https://stackoverflow.com/questions/6350626/get-region-city-from-ip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!