我使用riots api(传说联盟)来使用json/php/ccurl获取一些信息。我遇到了一个非常不清楚的错误信息,我不明白。此代码不工作:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/31827832?api_key=key');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
//var_dump($response);
$json = json_decode($response, true);
foreach($json['entries'] as $entry){
echo $entry['playerOrTeamName'] . ',' . $json['tier'] . ',' . $entry['division'] . ',' . $entry['leaguePoints'] . ',' . $entry['wins'] . "<br/>";
}
这是数组中的var_dump:
{
"name":"Ezreal's Zealots",
"tier":"PLATINUM",
"queue":"RANKED_SOLO_5x5",
"entries": [{
"playerOrTeamId":"34458086",
"playerOrTeamName":"OverdrivZ",
"division":"V",
"leaguePoints":21,
"wins":102,
"isHotStreak":false,
"isVeteran":false,
"isFreshBlood":false,
"isInactive":false
}]
错误消息:Notice: Undefined index: entries in /hermes/bosoraweb130/b411/ipg.notsureifpossiblecom/index.php on line 23
和
CCD_ 2。
我的代码中的第23行是:
foreach($json['entries'][0] as $entry){
但是当我使用不同的api请求时,这个完全相同的代码可以工作:
ini_set("display_errors", "1"); error_reporting(E_ALL);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://euw.api.pvp.net/api/lol/euw/v2.5/league/challenger?type=RANKED_SOLO_5x5&api_key=key');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response, true);
foreach($json['entries'] as $entry){
echo $entry['playerOrTeamName'] . ',' . $json['tier'] . ',' . $entry['division'] . ',' . $entry['leaguePoints'] . ',' . $entry['wins'] . "<br/>";
}
唯一的变化是在api url请求中,即challenger
版本。为什么这段代码适用于这个api url请求而不适用于第一个请求。我想要执行的回显应该适用于数组中显示的条目。我不明白发生了什么,一定是很简单的事情吧?请帮帮我。
数组似乎还有两个级别,所以实际上有:
Array (
[31827832] => Array (
[0] => Array (
[name] => Ezreal's Zealots
[tier] => PLATINUM
[queue] => RANKED_SOLO_5x5
[entries] => Array (
[0] => Array (
[playerOrTeamId] => 34458086
[playerOrTeamName] => OverdrivZ
[division] => V
[leaguePoints] => 21
[wins] => 102
[isHotStreak] =>
[isVeteran] =>
[isFreshBlood] =>
[isInactive] =>
)
好吧,看起来数组中的第一个条目实际上是带有玩家id的整数索引,所以我想有一个API调用来获取它,但在这种情况下使用这个:
$json = array_pop($json);
foreach($json[0]['entries'] as $entry){
....
}