使用PHP解码JSON响应时遇到问题

c9x0cxw0  于 2023-01-22  发布在  PHP
关注(0)|答案(1)|浏览(150)

我在尝试使用PHP解码api-football的以下json响应时遇到了麻烦。我的PHP知识非常有限,所以请耐心等待。
vardump的示例如下所示:

array(1) {
  ["api"]=>
  array(2) {
    ["results"]=>
    int(10)
    ["fixtures"]=>
    array(10) {
      [0]=>
      array(18) {
        ["fixture_id"]=>
        int(932017)
        ["league_id"]=>
        int(4633)
        ["league"]=>
        array(4) {
          ["name"]=>
          string(10) "Pro League"
          ["country"]=>
          string(12) "Saudi-Arabia"
          ["logo"]=>
          string(52) "https://media.api-sports.io/football/leagues/307.png"
          ["flag"]=>
          string(42) "https://media-3.api-sports.io/flags/sa.svg"
        }
        ["event_date"]=>
        string(25) "2023-01-22T20:30:00+03:00"
        ["event_timestamp"]=>
        int(1674408600)
        ["firstHalfStart"]=>
        NULL
        ["secondHalfStart"]=>
        NULL
        ["round"]=>
        string(19) "Regular Season - 14"
        ["status"]=>
        string(11) "Not Started"
        ["statusShort"]=>
        string(2) "NS"
        ["elapsed"]=>
        int(0)
        ["venue"]=>
        string(11) "Mrsool Park"
        ["referee"]=>
        NULL
        ["homeTeam"]=>
        array(3) {
          ["team_id"]=>
          int(2939)
          ["team_name"]=>
          string(8) "Al-Nassr"
          ["logo"]=>
          string(53) "https://media-3.api-sports.io/football/teams/2939.png"
        }
        ["awayTeam"]=>
        array(3) {
          ["team_id"]=>
          int(2934)
          ["team_name"]=>
          string(10) "Al-Ettifaq"
          ["logo"]=>
          string(51) "https://media.api-sports.io/football/teams/2934.png"
        }
        ["goalsHomeTeam"]=>
        NULL
        ["goalsAwayTeam"]=>
        NULL
        ["score"]=>
        array(4) {
          ["halftime"]=>
          NULL
          ["fulltime"]=>
          NULL
          ["extratime"]=>
          NULL
          ["penalty"]=>
          NULL
        }
      }

我基本上是在尝试打印“homeTeam”和“awayTeam”的fixture。但是我肯定没有正确地得到它。下面是我的代码:

<?php

$url = "https://api-football-v1.p.rapidapi.com/v2/fixtures/team/2939/next/10?rapidapi-key={API-Key}";

$request = wp_remote_get( $url );

if ( ! is_wp_error( $request ) ) {
    $body = wp_remote_retrieve_body( $request );
    $data = json_decode( $body, true );
}

echo $data["fixtures"][0]["league"]["name"];
echo $data["fixtures"][0]["homeTeam"]["teamname"];
echo $data["fixtures"][0]["awayTeam"]["teamname"]; 

 
?>

以下是varexport()的转储

array (
  'api' => 
  array (
    'results' => 10,
    'fixtures' => 
    array (
      0 => 
      array (
        'fixture_id' => 932017,
        'league_id' => 4633,
        'league' => 
        array (
          'name' => 'Pro League',
          'country' => 'Saudi-Arabia',
          'logo' => 'https://media-3.api-sports.io/football/leagues/307.png',
          'flag' => 'https://media-3.api-sports.io/flags/sa.svg',
        ),
        'event_date' => '2023-01-22T20:30:00+03:00',
        'event_timestamp' => 1674408600,
        'firstHalfStart' => NULL,
        'secondHalfStart' => NULL,
        'round' => 'Regular Season - 14',
        'status' => 'Not Started',
        'statusShort' => 'NS',
        'elapsed' => 0,
        'venue' => 'Mrsool Park',
        'referee' => NULL,
        'homeTeam' => 
        array (
          'team_id' => 2939,
          'team_name' => 'Al-Nassr',
          'logo' => 'https://media.api-sports.io/football/teams/2939.png',
        ),
        'awayTeam' => 
        array (
          'team_id' => 2934,
          'team_name' => 'Al-Ettifaq',
          'logo' => 'https://media-3.api-sports.io/football/teams/2934.png',
        ),
        'goalsHomeTeam' => NULL,
        'goalsAwayTeam' => NULL,
        'score' => 
        array (
          'halftime' => NULL,
          'fulltime' => NULL,
          'extratime' => NULL,
          'penalty' => NULL,
        ),
      ),
ymzxtsji

ymzxtsji1#

您没有正确访问数据以检索球队名称,请尝试以下操作:

echo $data["api"]["fixtures"][0]["league"]["name"];
echo $data["api"]["fixtures"][0]["homeTeam"]["team_name"];
echo $data["api"]["fixtures"][0]["awayTeam"]["team_name"];

相关问题