How to access and filter through the NBA schedule API for season 2021-22 using PHP.
This is not the only way and endpoint to get the NBA schedule however since it is a one and done call where you need no other information like team id’s and dates it is ideal.
The NBA schedule endpoint:
https://cdn.nba.com/static/json/staticData/scheduleLeagueV2_9.json
As this involves the NBA static CDN API you won’t need to do a fancy cURL GET request with origins, referrers and User-Agents included.
The GET request and decode
$data = json_decode(file_get_contents("https://cdn.nba.com/static/json/staticData/scheduleLeagueV2_9.json"), true);
The call will return a large multidimensional array with the data for all the pre-season and regular-season NBA games for season 2021-22.
Below is the correct paths in the array to access every game through a loop.
Looping through the returned data
foreach ($data['leagueSchedule']['gameDates'] as $games) { foreach ($games['games'] as $game) { //Do stuff here } }
Outputting all the games with the date and time formatted as a readable string:
foreach ($data['leagueSchedule']['gameDates'] as $games) { foreach ($games['games'] as $game) { $date_time = DateTime::createFromFormat('Y-m-d H:i:s', str_replace(["T", "Z"], [" ", ""], $game['gameDateTimeEst'])); $date_formatted = $date_time->format('g:ia D jS M Y'); $home_team = $game['homeTeam']['teamName']; $away_team = $game['awayTeam']['teamName']; echo "$home_team vs $away_team $date_formatted<br>"; } }
This looks like this:
Today’s games only
Getting only today’s games using the ET timezone:
date_default_timezone_set('America/New_York'); $todays_date = date('Y-m-d') . "T00:00:00Z"; foreach ($data['leagueSchedule']['gameDates'] as $games) { foreach ($games['games'] as $game) { if ($todays_date === $game['gameDateEst']) { echo "{$game['homeTeam']['teamName']} vs {$game['awayTeam']['teamName']}<br>"; } } }
The output:
Filter for only one team
Filter for only one team’s games and add colour depending on home or away game:
$filter_team = 'Clippers'; foreach ($data['leagueSchedule']['gameDates'] as $games) { foreach ($games['games'] as $game) { $show = false; $date_time = DateTime::createFromFormat('Y-m-d H:i:s', str_replace(["T", "Z"], [" ", ""], $game['gameDateTimeEst'])); $home_team = $game['homeTeam']['teamName']; $away_team = $game['awayTeam']['teamName']; $date_formatted = $date_time->format('g:ia D jS M Y'); if ($home_team === $filter_team) { $show = true; $style = 'background: #87ff8747;'; } else if ($away_team === $filter_team) { $show = true; $style = 'background: #ff878747;'; } if ($show) { echo "<span style='$style'>$home_team vs $away_team <span style='font-size: 80%'>$date_formatted</span></span><br>"; } } }
Green is home games whilst red means an away game:
Filter for only regular-season games
Finally to filter only the regular season and not include pre-season games. This is done by using the 3rd character from the game id. 1 is pre-season whilst 2 is regular-season.
Once there was been one regular-season game this confirms that no more checks will be made for game type aiding efficiency in the filter.
$is_reg_season = false; foreach ($data['leagueSchedule']['gameDates'] as $games) { foreach ($games['games'] as $game) { if (!$is_reg_season) { $game_type_int = (int)substr($game['gameId'], 2, 1); if ($game_type_int === 2) { $is_reg_season = true; } } if ($is_reg_season) { //Gather and output from here } } }