【问题标题】:Unclear appearance of errors during JSON/API processingJSON/API 处理过程中出现的错误不清楚
【发布时间】: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",
    "league‌​Points":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/2919developer.riotgames.com/api/methods#!/828/2921 代码在 2919 上不起作用,但在 2921 上却完美运行。完全不知道这是怎么发生的。 var_dump 返回的结果与第一个发布的数组相同。
  • 或者你想看var_dump($json) 吗?现在直播20ff.net
  • 我想要 var_dump($json) 作为它的 php 数组。
  • 是的,现在在20ff.net上直播

标签: php json api curl


【解决方案1】:

数组似乎还有两个层次,所以你实际上有:

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){
    ....
}

【讨论】:

  • 尝试了代码,但没有奏效。这很奇怪,因为完全相同的代码确实适用于完全相同的 api 参考指南,唯一改变的是输出。两个 api 引用使用完全相同的请求处理。这是非常奇怪的行为。使用您的代码后的错误消息:Notice: Undefined offset: 0 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
  • 解析后的 JSON 的结构不同于给定 JSON 尝试循环遍历已解析数组的项目并 print_r 测试结构的结果。我将编辑代码。
  • 您编辑的代码运行良好。您能否解释一下为什么会发生这种行为?我还是不明白出了什么问题。
  • JSON 对象封装在两个附加级别中 - 首先是包含主数组的 ID。它用作数组索引,因为它在解析时是一个整数,所以您无法使用 $arr[0] 访问它,但您必须使用确切的索引。在给定的示例中,您不知道它的值,因此您弹出数组以获取下一个级别,该级别是零索引数组,包含您需要的真实响应数据。最后一个对象实际上是您粘贴的部分。可能在他们的 API 中的某个地方,他们解释了他们用于响应的封装。
  • 啊,好吧,这很有道理。先生,感谢您的热心帮助和分享您的知识!
猜你喜欢
  • 2021-12-26
  • 2016-06-30
  • 2013-12-23
  • 2021-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多