Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php JSON/API处理过程中出现的错误不清楚_Php_Json_Api_Curl - Fatal编程技术网

Php JSON/API处理过程中出现的错误不清楚

Php JSON/API处理过程中出现的错误不清楚,php,json,api,curl,Php,Json,Api,Curl,我使用RITOS 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_RETURNTRANSF

我使用RITOS 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/>";
        }
错误消息:
注意:未定义索引:第23行的/hermes/bosoraweb130/b411/ipg.notsureifpossiblecom/index.php中的条目

警告:为第23行/hermes/bosoraweb130/b411/ipg.notsureeifpossiblecom/index.php中的foreach()提供的参数无效

我的代码中的第23行是:

foreach($json['entries'][0]作为$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/>";
            }
ini\u集(“显示错误”、“1”);错误报告(E\u全部);
$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\u exec($ch);
卷曲关闭($ch);
$json=json_decode($response,true);
foreach($json['entries']作为$entry){
echo$entry['playerOrTeamName'.'、'.$json['tier'.]、'.$entry['division'.]、'.$entry['leaguePoints'.]、'.$entry['wins'.'”
; }

唯一的变化是api url请求,
challenger
版本。为什么此代码适用于此api url请求而不适用于第一个请求。我要执行的回显应该适用于数组中显示的条目。我不明白发生了什么,一定是非常简单的事情?请帮助我。

似乎有两个o阵列的级别更多,因此您实际上有:

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

您可以使用解析的数组(在json_decode()之后)添加一个var_转储吗给出了相同的结果,还是我做错了?这些是api指南参考。代码在2919上不起作用,但在2921上运行得很好。完全不知道这是怎么发生的。var_dump返回的结果与第一个发布的数组完全相同。或者你想看
var_dump($json)
?这是我想要的var_dump吗($json)因为它是php array.yep,它现在正在运行。它尝试了代码,但没有工作。这很奇怪,因为完全相同的代码在完全相同的api参考指南中工作,唯一改变的是输出。两个api参考使用完全相同的请求处理。这是非常奇怪的行为。使用代码后的错误消息:
注意:第23行/hermes/bosoraweb130/b411/ipg.notsureifpossiblecom/index.php中未定义的偏移量:0警告:为foreach()提供的参数无效在第23行的/hermes/bosoraweb130/b411/ipg.notsureifpossiblecom/index.php中
解析的JSON的结构不同于给定的JSON尝试循环解析数组的项并打印结果以测试结构。我将编辑代码。您编辑的代码工作正常。您能解释为什么会发生这种行为吗?我仍然不知道我不明白出了什么问题。JSON对象被封装在两个额外的级别中-第一个级别是保存主数组的ID。它用作数组索引,因为解析时它是一个整数,您无法使用$arr[0]访问它,但你必须知道确切的索引。在给定的示例中,你不知道它的值,所以你弹出数组以获得下一个级别,即零索引数组,其中包含你需要的真实响应数据。最后一个对象实际上是你粘贴的部分。可能在他们的API中的某个地方,他们解释了他们用于响应的封装。啊,好的,这是感谢您的帮助和分享您的知识!
$json = array_pop($json);
foreach($json[0]['entries'] as $entry){
    ....
}