【发布时间】:2014-09-27 09:26:52
【问题描述】:
我使用riots api(英雄联盟)通过json/php/curl获取一些信息。我遇到了一个非常不清楚的错误消息,我不明白。此代码不起作用:
$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 和
Warning: Invalid argument supplied for foreach() in /hermes/bosoraweb130/b411/ipg.notsureifpossiblecom/index.php on line 23.
我的代码中的第 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 请求而不适用于第一个请求。我要执行的回声应该适用于数组中显示的条目。我不明白发生了什么,一定很简单?请帮帮我。
【问题讨论】:
-
你能在解析后的数组中添加一个var_dump吗(在json_decode()之后)
-
给出相同的结果还是我做错了?这些是 api 指南参考。 developer.riotgames.com/api/methods#!/828/2919 和 developer.riotgames.com/api/methods#!/828/2921 代码在 2919 上不起作用,但在 2921 上却完美运行。完全不知道这是怎么发生的。 var_dump 返回的结果与第一个发布的数组相同。
-
或者你想看
var_dump($json)吗?现在直播20ff.net -
我想要 var_dump($json) 作为它的 php 数组。
-
是的,现在在20ff.net上直播