我想从访问者的IP地址中检索他们的城市、州和国家等信息,这样我就可以根据他们的位置定制我的网页。在PHP中是否有一种好的、可靠的方法来做到这一点?我使用JavaScript作为客户端脚本,PHP作为服务器端脚本,MySQL作为数据库。
当前回答
我喜欢Maxmind的免费GeoLite City,它最适用 如果它不够精确,你可以从它升级到付费版本。包括一个PHP API,以及其他语言的API。如果你将Lighttpd作为web服务器运行,你甚至可以使用一个模块为每个访问者获取SERVER变量中的信息,如果这是你所需要的。
我应该补充的是,还有一个免费的Geolite Country(如果你不需要确定IP来自哪个城市,这个会更快)和Geolite ASN(如果你想知道谁拥有IP),最后所有这些都可以在你自己的服务器上下载,每月更新一次,使用提供的api可以非常快地查找,因为他们说“每秒数千次查找”。
其他回答
假设您想自己做而不依赖于其他提供者,IP2Nation提供了一个映射的MySQL数据库,该数据库会随着区域注册中心的变化而更新。
你需要使用外部服务…例如http://www.hostip.info/,如果你谷歌搜索“geo-ip”,你可以得到更多的结果。
主机ip API是基于HTTP的,因此您可以根据需要在PHP或JavaScript中使用它。
一个纯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>
我在IPLocate上运行服务。Io,你可以通过一个简单的电话免费连接到它:
<?php
$res = file_get_contents('https://www.iplocate.io/api/lookup/8.8.8.8');
$res = json_decode($res);
echo $res->country; // United States
echo $res->continent; // North America
echo $res->latitude; // 37.751
echo $res->longitude; // -97.822
var_dump($res);
$res对象将包含您的地理位置字段,如国家、城市等。
查看文档了解更多信息。
本·道林回复中的服务已经改变了,所以现在更简单了。要找到位置,只需做:
// 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
推荐文章
- 在文本文件中创建或写入/追加
- 为什么PHP的json_encode函数转换UTF-8字符串为十六进制实体?
- 如何从一个查询插入多行使用雄辩/流利
- 在PHP单元测试执行期间,如何在CLI中输出?
- 在PHP中使用heredoc的优势是什么?
- PHP中的echo, print和print_r有什么区别?
- 如何将XML转换成PHP数组?
- 如何将对象转换为数组?
- 从IP地址获取位置
- 获取数组值的键名
- HTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA is OK
- PHP -获取bool值,当为false时返回false
- 在foreach中通过引用传递
- 如何触发命令行PHP脚本的XDebug分析器?
- 如何找出如果你使用HTTPS没有$_SERVER['HTTPS']