Home > other >  Having trouble decoding JSON response using PHP
Having trouble decoding JSON response using PHP

Time:01-21

I am having trouble trying to decode the following json response from api-football using PHP. My knowledge of PHP is very limited so please bear with me.

A sample of the vardump is as follows:

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
        }
      }

What I am trying basically is printing the fixture for "homeTeam" and "awayTeam". But I am definitely not getting it correctly. Here is my code :

<?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"]; 

 
?>

Here is dump from 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,
        ),
      ),    

CodePudding user response:

You aren't accessing the data properly to retrieve the team names, please try this instead:

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