0

enter code hereThe error this code returns is

"Trying to get property of non-object" or "Message: Undefined property: stdClass::$name".

The variable $summoner is what does not work. Assume the variables sID, $lcaseregion and $APIkey (etc.) are all properly set.

    <?php
    $summonerURL = "https://$lcaseRegion.api.pvp.net/api/lol/$lcaseRegion/v1.4/summoner/" . $sID . "?api_key=9" . $APIkey;
    $responseSummoner = @file_get_contents($summonerURL);
    $summoner = @json_decode($responseSummoner);
    echo print_r($summoner); //this works and prints things
    echo $summoner->name; //this does not work
    echo $summoner->profileIconId; //this also does not work.
    //please find print_r results below...
    ?>

print_r results;

stdClass Object ( [41245441] => stdClass Object ( [id] => 41245441 [name] => Bubbalubagus [profileIconId] => 558 [revisionDate] => 1488775287000 [summonerLevel] => 30 ) )

Any other advice and tips you can give me on how the arrays work would also be appreciated.

ben
  • 470
  • 3
  • 11
Bubba
  • 21
  • 5
  • Success on this question, to me, means successfully printing profileIconId (a number) and name as well as any other contents in the stdClass object array. – Bubba Mar 06 '17 at 05:15
  • this is object of object, try it with `foreach` or `pop` – Chetan Ameta Mar 06 '17 at 05:18

1 Answers1

-1

Try this:

$summoners = @json_decode($responseSummoner);
foreach($summoners as $summoner){
    echo $summoner->name;
    echo $summoner->profileIconId;
}

As it was pointed out already, you need to loop through the found summoners (although it is only one)

//EDIT: saw an object that wasnt there, this works now

PrimuS
  • 2,505
  • 6
  • 33
  • 66