我如何从Riot API为召唤师创建这个变量有什么问题?

时间:2017-03-06 05:14:25

标签: php arrays riot-games-api

enter code here此代码返回的错误是

  

“尝试获取非对象的属性”或“消息:未定义的属性:stdClass :: $ name”。

变量 $ summoner 是不起作用的。假设变量sID,$ lcaseregion和$ APIkey(等)都已正确设置。

    <?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结果;

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

您还可以了解有关阵列如何工作的任何其他建议和提示。

1 个答案:

答案 0 :(得分:-1)

试试这个:

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

正如已经指出的那样,你需要遍历找到的召唤者(尽管它只有一个)

//编辑:看到一个不存在的对象,现在可以正常工作