The mistake I had was solved as follows:
I followed in the footsteps of Hugo Fernandes
and of Kenny Rafael
, but I made a mess and ended up making another mistake. Let’s go in parts, aiming to help others who will use this question to help in their problems:
Where was my mistake? At first, the code was 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 errors in the syntax, as the lack of ;
after the attribute of $lista
, and the =
after the echo
(echo is not a variable, so you can’t put value in echo).
Okay, by rewriting the code, it gets more straight:
<?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 there, we already noticed the gross error:
As said the Hugo Fernandes
, I’m trying to access a Objeto
, and my goal was to access a list of Objetos
(One Array
).
All right, I did what he suggested in his reply, so the code went 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 arose. I read the answers "running" and I ended up pooping. o Kenny Rafael
said I should access without the parameter true
to access the Objeto
, and then came the second error: I tried to access a JSON``Array
from a JSON``Object
, and ended up making the mistake:
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 orient myself. I called myself that I should put the parameter true
in the json_decode
, transforming the variable $infor
in a JSON Array
, instead of a JSON Object
, then the code went 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!
json_decode with true returns an array, so you should not access the list property but something like $players = $Infor['Players']['list']. If this does not resolve put there the result of var_dump($Infor)
– Rafael Mena Barreto
The dump is this: array(3) { ["Status"]=> bool(true) ["Players"]=> array(3) { ["online"]=> int(7) ["max"]=> int(60) ["list"]=> array(7) { [0]=> string(5) "Uchoa" [1]=> string(10) "Mateusinox" [2]=> string(8) "zMarvado" [3]=> string(8) "fullsets" [4]=> string(13) "Stevegamer138" [5]=> string(16) "Diego_dias_br_rJ" [6]=> string(8) "Konotero" } } ["Version"]=> string(5) "1.8.8" }
– Brunno
Brunno, never just put the link to something essential in your question, because if it gets lost, it will not be possible to help other people too, thus avoiding the purpose of the forum!!! =)
– Kenny Rafael
Thanks for the tip, unfortunately I already modified the test.php page, but next I put the error in the topic itself
– Brunno