我尝试使用db-ip.com解析IP地理位置。我获得了一个API密钥和有效的请求URL,如下所示:
http://api.db-ip.com/addrinfo?addr=123.457.89.10&api_key=123456789
JSON响应: {"地址":" 11.11.11.11""国家":" US"" stateprov":& #34;德克萨斯""城市":"休斯敦"}
这是我通过debian linux shell执行的php脚本(auto-location.php)
<?php
$remoteIp = '11.11.11.11';
$geoLocationUrl="http://api.db-ip.com/addrinfo?addr=$remoteIp&api_key=123456789";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $geoLocationUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$auth = curl_exec($curl);
if($auth)
{
$json = json_decode($auth,true);
echo $address = $json['address'];
echo $country =$json['country'];
echo $state =$json['stateprov'];
echo $city =$json['city'];
}
else{
echo 'ERROR';
}
?>
下面的错误消息是当我使用命令行php auto-location.php通过shell执行时。这是准确的准系统我只想让查询工作。任何帮助将不胜感激。
PHP Notice: Undefined index: address in /home/auto/auto-location.php on line 15
PHP Notice: Undefined index: country in /home/auto/auto-location.php on line 16
PHP Notice: Undefined index: stateprov in /home/auto/auto-location.php on line 17
PHP Notice: Undefined index: city in /home/auto/auto-location.php on line 18
答案 0 :(得分:1)
正如Luke在我的评论中指出的那样,我不小心将'留在了我的API密钥中。