这个API将获取您的位置详细信息。尝试一下
<?php
$ip = '168.192.0.1';
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
if($query && $query['status'] == 'success')
{
echo 'Your City is ' . $query['city'];
echo '<br />';
echo 'Your State is ' . $query['region'];
echo '<br />';
echo 'Your Zipcode is ' . $query['zip'];
echo '<br />';
echo 'Your Coordinates are ' . $query['lat'] . ', ' . $query['lon'];
}
?>
以下是您可以使用的输出。 使用键调用它们。
<?php
array (
'status' => 'success',
'country' => 'COUNTRY',
'countryCode' => 'COUNTRY CODE',
'region' => 'REGION CODE',
'regionName' => 'REGION NAME',
'city' => 'CITY',
'zip' => 'ZIP CODE',
'lat' => 'LATITUDE',
'lon' => 'LONGITUDE',
'timezone' => 'TIME ZONE',
'isp' => 'ISP NAME',
'org' => 'ORGANIZATION NAME',
'as' => 'AS NUMBER / NAME',
'query' => 'IP ADDRESS USED FOR QUERY',
);
?>
这种方法在移动端可能不太可靠。如果你正在使用移动设备,可以尝试使用以下 JavaScript 代码。
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
这将使用设备内置的GPS并获取坐标。然后,您可以与包含坐标的数据库进行交叉引用(这是一个选项,也是我所做的)。
如果您选择在移动设备上执行此操作,可以在线查找城市纬度和经度数据库。
httpd
作为Web服务器,并安装php
插件。上述提到的.exe
文件仅适用于Windows系统。 - Matt Clark