繁体   English   中英

为什么将json解码打印到屏幕上?

[英]Why is this json decode printing to screen?

这段php发生了非常奇怪的事情。 而不是填充$ country变量,而是将整个json打印到浏览器窗口中。 我不明白为什么要这么做。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://ipinfo.io/".$this_ip."/json");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36");
$headers = array();
$headers[] = 'Referer: http://www.example.com';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$json = curl_exec ($ch);
curl_close ($ch);

 $decode = json_decode($json,true);
 $country = $decode[country];

这是它吐出的全部错误:

<body style="height:100%; overflow:auto; padding:0px; margin:0px;">{
  "ip": "8.8.8.8",
  "hostname": "google-public-dns-a.google.com",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "loc": "37.3860,-122.0838",
  "org": "AS15169 Google Inc.",
  "postal": "94040"
}<br>
<b>Notice</b>:  Trying to get property of non-object in <b>/var/www/html/example.php</b> on line <b>59</b><br>

另外,为什么会出现此非对象错误?

添加curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 卷曲

$country = $decode['country'];

代替

$country = $decode[country];

请注意您如何访问$ decode数组的国家/地区密钥。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM