HOW TO GET STANDINGS FOR ALL CURRENT SEASONS

- Posted in Tutorials by

We will see how to retrieve the standings of all the active competitions of the API.

To get the standings we need two parameters:

  • the league id
  • the season

We will assume that we do not know the league ids. So we need a way to get all the leagues ids and also the active season attached to this leagues because we want only the current season and not the past seasons.

One point to take into account is the coverage of each competition which can be different and therefore some competitions do not necessarily have the standings available and it is not useful to call them.

Fortunately a single call to the endpoint leagues allows us to get all the information we need for our script.

So will call the endpoint leagues with the parameter current=true, which will return us only the active seasons. Note: In the API a season is considered current as long as it has not been renewed. For example the 2018 World Cup is currently active because the schedule for the 2022 is not yet available.

Here is the first part of the script and we will save the response of the API in a variable named leagues.

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://v3.football.api-sports.io/leagues?current=true',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'x-rapidapi-key: YOUR_API_KEY_HERE'
  ),
));
$response = curl_exec($curl);
curl_close($curl);
$leagues = json_decode($response);

We thus obtain a result with 900 leagues and cups. Like this:

{
    "get": "leagues",
    "parameters": {
        "current": "true"
    },
    "errors": [],
    "results": 900,
    "paging": {
        "current": 1,
        "total": 1
    },
    "response": [
        {
            "league": {
                "id": 39,
                "name": "Premier League",
                "type": "League",
                "logo": "https://media.api-sports.io/football/leagues/39.png"
            },
            "country": {
                "name": "England",
                "code": "GB",
                "flag": "https://media.api-sports.io/flags/gb.svg"
            },
            "seasons": [
                {
                    "year": 2021,
                    "start": "2021-08-13",
                    "end": "2022-05-22",
                    "current": true,
                    "coverage": {
                        "fixtures": {
                            "events": true,
                            "lineups": true,
                            "statistics_fixtures": true,
                            "statistics_players": true
                        },
                        "standings": true,
                        "players": true,
                        "top_scorers": true,
                        "top_assists": true,
                        "top_cards": true,
                        "injuries": true,
                        "predictions": true,
                        "odds": true
                    }
                }
            ]
        }
    ]
}

We will have to go through the 900 results and keep only the competitions that have the coverage->standings field set to true.

To do this we just need to create an empty array that we call $leagues_for_standings and will fill with the league id and the season only if the competition covers the standings.

Let's start by doing this script:

$leagues_for_standings = [] ;
/*
The content of the $league variable is the result of our first call to the API earlier in the tutorial
 */
foreach ($leagues->response as $league) {

    if ($league->seasons[0]->coverage->standings == 'true') {

        $leagues_for_standings[] = [
            'league' => $league->league->id,
            'season' => $league->seasons[0]->year
        ];
    }
}

The array $leagues_for_standings now contains all the active competitions that have standings available, we just have to call the endpoint standings for each of these competitions and we will get all the data we want.

Note: We could have called the endpoint standings directly inside the condition in our loop but for more clarity we do it step by step.

We will now browse our $leagues_for_standings array and for each iteration call the standings endpoint with the league and season parameters.

foreach ($leagues_for_standings as $league) {

    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://v3.football.api-sports.io/standings?league='.$league["league"].'&season='.$league["season"],
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
      CURLOPT_HTTPHEADER => array(
        'x-rapidapi-key: YOUR_API_KEY_HERE'
      ),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    echo $response;
}

And here we have the desired data set.

Note: The API having a ratelimit per minute (depending on your subscribed plan) it will thus be necessary to delay certain calls to the API in order not to have errors 429 - Too Many Requests and thus not retrieve all the data.

Here is the whole script, you just have to copy it and add your API-KEY. Feel free to improve it.

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://v3.football.api-sports.io/leagues?current=true',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'x-rapidapi-key: YOUR_API_KEY_HERE'
  ),
));
$response = curl_exec($curl);
curl_close($curl);
$leagues = json_decode($response);

$leagues_for_standings = [] ;
foreach ($leagues->response as $league) {

    if ($league->seasons[0]->coverage->standings == 'true') {

        $leagues_for_standings[] = [
            'league' => $league->league->id,
            'season' => $league->seasons[0]->year
        ];
    }
}

foreach ($leagues_for_standings as $league) {

    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://v3.football.api-sports.io/standings?league='.$league["league"].'&season='.$league["season"],
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
      CURLOPT_HTTPHEADER => array(
        'x-rapidapi-key: YOUR_API_KEY_HERE'
      ),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    echo $response;
}