The error I had was resolved as follows:
I followed the steps of Hugo Fernandes
and Kenny Rafael
, however I made confusion and ended up giving another error. We are going in parts to help others who will use this question to help with their problems:
Where was my mistake? At first, the code looked like this:
<?php
$lista = file_get_contents("http://mcapi.ca/query/ip.craftlite.com.br:25571/list")
$infor = json_decode($lista, true);
$players = $infor->Players->list;
echo = "$players";
?>
You can already notice a number of syntax errors, such as the lack of ;
after the $lista
attribute, and =
after echo
(echo is not a variable, so do not put value not echo).
Ok, reforming the code, it gets more accurate:
<?php
$lista = file_get_contents("http://mcapi.ca/query/ip.craftlite.com.br:25571/list"); //Botei o ;
$infor = json_decode($lista, true);
$players = $infor->Players->list;
echo "$players";//Tirei o =
?>
From here, we've noticed the gross error:
As I said Hugo Fernandes
, I'm trying to access a Objeto
, and my goal was to access a Objetos
( Array
) list.
Beauty, I did what he suggested in his response, so the code looks like this:
<?php
$lista = file_get_contents("http://mcapi.ca/query/ip.craftlite.com.br:25571/list");
$infor = json_decode($lista);
$players = $infor['Players'];
$players_list = $players['list'];
foreach($players_list as $i){
echo ' '.$i.',';
}
?>
But then the confusion came. I read the answers "running" and ended up doing poop. Kenny Rafael
said that I should access without the true
parameter to access Objeto
, and then the second error appeared: I tried to access a JSON''Array
from a JSON''Object
, and ended up giving the error :
Fatal error: Cannot use object of type stdClass as array in /customers/6/8/7/craftlite.com.br/httpd.www/test.php on line 8
I spent some time reading the answers and managed to get my bearings. I 've called that you should set the parameter true
to json_decode
, turning the $infor
variable into a JSON Array
, instead of a JSON Object
, then the code looks like this:
<?php
$lista = file_get_contents("http://mcapi.ca/query/ip.craftlite.com.br:25571/list");
$infor = json_decode($lista, true);
$players = $infor['Players'];
$players_list = $players['list'];
foreach($players_list as $i){
echo ' '.$i.',';
}
?>
After the help of these two masters, and a little more attention, I completed the code without errors!