我想从访问者的IP地址中检索他们的城市、州和国家等信息,这样我就可以根据他们的位置定制我的网页。在PHP中是否有一种好的、可靠的方法来做到这一点?我使用JavaScript作为客户端脚本,PHP作为服务器端脚本,MySQL作为数据库。


当前回答

一个纯Javascript的例子,使用https://geolocation-db.com的服务,他们提供了一个JSON和jsonp -回调解决方案。

JSON: https://geolocation-db.com/json JSONP-callback: https://geolocation-db.com/jsonp

不需要jQuery !

<!DOCTYPE html> <html> <head> <title>Geo City Locator by geolocation-db.com</title> </head> <body> <div>Country: <span id="country"></span></div> <div>State: <span id="state"></span></div> <div>City: <span id="city"></span></div> <div>Postal: <span id="postal"></span></div> <div>Latitude: <span id="latitude"></span></div> <div>Longitude: <span id="longitude"></span></div> <div>IP address: <span id="ipv4"></span></div> </body> <script> var country = document.getElementById('country'); var state = document.getElementById('state'); var city = document.getElementById('city'); var postal = document.getElementById('postal'); var latitude = document.getElementById('latitude'); var longitude = document.getElementById('longitude'); var ip = document.getElementById('ipv4'); function callback(data) { country.innerHTML = data.country_name; state.innerHTML = data.state; city.innerHTML = data.city; postal.innerHTML = data.postal; latitude.innerHTML = data.latitude; longitude.innerHTML = data.longitude; ip.innerHTML = data.IPv4; } var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'https://geolocation-db.com/json/geoip.php?jsonp=callback'; var h = document.getElementsByTagName('script')[0]; h.parentNode.insertBefore(script, h); </script> </html>

其他回答

你需要使用外部服务…例如http://www.hostip.info/,如果你谷歌搜索“geo-ip”,你可以得到更多的结果。

主机ip API是基于HTTP的,因此您可以根据需要在PHP或JavaScript中使用它。

我用ipapi写了一个机器人。下面是如何在php中获取IP地址(例如1.2.3.4)的location:

设置报头:

$opts = array('http'=>array('method'=>"GET", 'header'=>"User-Agent: mybot.v0.7.1"));
$context = stream_context_create($opts);

获取JSON响应

echo file_get_contents('https://ipapi.co/1.2.3.4/json/', false, $context);

获取一个特定的字段(国家、时区等)

echo file_get_contents('https://ipapi.co/1.2.3.4/country/', false, $context);

本·道林回复中的服务已经改变了,所以现在更简单了。要找到位置,只需做:

// no need to pass ip any longer; ipinfo grabs the ip of the person requesting
$details = json_decode(file_get_contents("http://ipinfo.io/"));
echo $details->city; // city

坐标以单一字符串形式返回,如'31,-80',因此从那里你只需:

$coordinates = explode(",", $details->loc); // -> '31,-89' becomes'31','-80'
echo $coordinates[0]; // latitude
echo $coordinates[1]; // longitude

您可以下载免费的GeoIP数据库并在本地查找IP地址,也可以使用第三方服务并执行远程查找。这是一个更简单的选项,因为它不需要设置,但它确实引入了额外的延迟。

你可以使用的第三方服务是我的,http://ipinfo.io。它们提供主机名、地理位置、网络所有者和其他信息,例如:

$ 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示例:

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

您也可以在客户端使用它。下面是一个简单的jQuery示例:

$.get("https://ipinfo.io/json", function (response) { $("#ip").html("IP: " + response.ip); $("#address").html("Location: " + response.city + ", " + response.region); $("#details").html(JSON.stringify(response, null, 4)); }, "jsonp"); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3> <hr/> <div id="ip"></div> <div id="address"></div> <hr/>Full response: <pre id="details"></pre>

Ipdata。co是一个快速、高可用的IP地理定位API,性能可靠。

它具有极高的可扩展性,在世界各地有10个端点,每个端点每秒可以处理>万个请求!

这个答案使用了一个非常有限的“测试”API键,仅用于测试几个调用。注册您自己的免费API密钥,每天最多可获得1500个开发请求。

在php中

php > $ip = '8.8.8.8';
php > $details = json_decode(file_get_contents("https://api.ipdata.co/{$ip}?api-key=test"));
php > echo $details->region;
California
php > echo $details->city;
Mountain View
php > echo $details->country_name;
United States
php > echo $details->latitude;
37.751

下面是一个客户端示例,展示了如何获取国家、地区和城市;

$ . get (" https://api.ipdata.co?Api-key =test",函数(响应){ $(" #反应”). html (JSON。Stringify(响应,null, 4)); $("#country").html(' country: ' + response.country_name); $("#region").html(' region ' + response.region); $("#city").html(' city ' + response.city); }, " jsonp "); < script src = " https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js " > < /脚本> < div id = "国家" > < / div > < div id = "地区" > < / div > < div id = "城市" > < / div > < pre id =“响应”> < / >之前

免责声明;

我创建了这项服务。

有关多种语言的示例,请参阅文档

还可以查看最佳IP地理定位api的详细分析。