1

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,
        ),
      ),    
3
  • Try varexport() instead of vardump(), that will give us something we can actually work with. Commented Jan 20, 2023 at 20:47
  • The root of your JSON response is api, not fixtures. Try echo $data['api']["fixtures"][0]["league"]["name"]; Commented Jan 20, 2023 at 20:52
  • I got those errors: Pro League Notice: Undefined index: teamname in /var/www/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(99) : eval()'d code on line 14 Notice: Undefined index: teamname in /var/www/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(99) : eval()'d code on line 15 Commented Jan 20, 2023 at 21:14

1 Answer 1

1

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"]; 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.