我需要制作一个自定义代码,帮助我找到通过文本框输入的特定IP的位置。当我在互联网上搜索时,我发现只提供IP及其位置的自动检测。请帮帮我。
我在我的网站中使用此代码
请自己试试-
<?php
function countryCityFromIP($ipAddr)
{
$url = "http://api.ipinfodb.com/v3/ip-city/?key=5cfaab6c5af420b7b0f88d289571b990763e37b66761b2f053246f9db07ca913&ip=$ipAddr&format=json";
$d = file_get_contents($url);
return json_decode($d , true);
}
if(isset($_REQUEST['submit'])){
$ip=countryCityFromIP($_REQUEST['ip']);
//print_r($ip);
echo $ip['cityName'];
}
?>
<form method="post">
<input type="text" name="ip" />
<input type="submit" name="submit" value="Find" />
</form>
请使用print_r($ip)以获取将从url中获取哪个参数的详细信息。
查看MaxMind GeoIP数据库。该教程可在http://www.phpandstuff.com/articles/geoip-country-lookup-with-php.还有各种其他可用的数据库。看看https://stackoverflow.com/search?q=geolocation+%5BPH%5D