[英]json_decode return null i guess
I try to run this on my server: 我尝试在服务器上运行此命令:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
echo $ip;
$city = $details -> city;
echo $city;
?>
But, this print ip only. 但是,仅此打印ip。 Maybe a server problem or configuration?
也许是服务器问题或配置?
You need to code a bit more defensively, if that site has no data for the ip address you give it, then it wont return any city
property 您需要进行一些防御性的编码,如果该站点没有您提供的ip地址的数据,则它将不会返回任何
city
财产
This is a little safer 这样比较安全
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
echo $ip . ' ';
if (isset($details->city)){
echo $details->city;
} else {
echo 'data not available';
}
Judging from what it returned for my IP Address, the details it provides are not very accurate anyway 从它返回我的IP地址的结果来看,它提供的详细信息仍然不是很准确
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.