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 ) )
您还可以了解有关阵列如何工作的任何其他建议和提示。
答案 0 :(得分:-1)
试试这个:
$summoners = @json_decode($responseSummoner);
foreach($summoners as $summoner){
echo $summoner->name;
echo $summoner->profileIconId;
}
正如已经指出的那样,你需要遍历找到的召唤者(尽管它只有一个)
//编辑:看到一个不存在的对象,现在可以正常工作