get region/city from ip [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
开发者_如何转开发 Improve this questionhow 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
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:
$details = json_decode(file_get_contents("http://ipinfo.io/".$_SERVER['REMOTE_ADDR']"));
echo $details->city; // -> "Mountain View"
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';
}
This is my favourite:
ipinfodb
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
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
精彩评论