Development

NBA fixture API with PHP; Making simple team fixture

NBA offers many useful and detailed API endpoints, from stats to scores and shot charts they are very generous and providing given that it is for free. In this post I will be focusing on the NBA fixture API.

The NBA fixture API endpoint gives obviously scheduled games and details for that game, it also gives scores for past games however I wont be giving that any attention.

This tutorial is done in PHP, if you prefer something else that is fine. You can view an image for the end result at the end of the page or click here.

The fixture API endpoint is https://data.nba.com/data/10s/v2015/json/mobile_teams/nba/2018/league/00_full_schedule_week.json

In PHP we call and get this data with file_get_contents and json_decode

$data = json_decode(file_get_contents("https://data.nba.com/data/10s/v2015/json/mobile_teams/nba/2018/league/00_full_schedule_week.json"), true);

Next we want to set our team we support or want the fixture for. In this case its the abbreviated name, for the Clippers (I will be doing) it is LAC. Search through the json for your team’s name.

$my_team = 'LAC';

If you had a look around in the json call you will have noticed that the fixture includes preseason games, which for cleanliness and authenticity I don’t want in this fixture output. We want to set our game counter to -4 so when the loop gets to the 2018/19 game 1 it is at 1 not 6.

$game = -4;

Now for the fun, the API is split into months. We want all the months for regular season games at the same time.

for ($x = 1; $x <= 7; $x++) {
     foreach ($data['lscd'][$x]['mscd']['g'] as $val) {

This loops through from 1 to 7, getting the data for 7 months of NBA basketball.

To get only the games our team is in we can filter a string to the team name we set above by:

if (strpos($val['gcode'], $my_team) !== false) {

This means that if the string contains LAC (is not == false) then do what is in the brackets which is

$h_team = $val['h']['ta'];//Home team abrev
$h_team_n = $val['h']['tn'];//Home team name
$h_team_c = $val['h']['tc'];//Home team city
$v_team = $val['v']['ta'];//Visiting team
$v_team_n = $val['v']['tn'];//Visiting team name
$v_team_c = $val['v']['tc'];//Visiting team city
$arena = $val['an'];//arena
$gid = $val['gid'];//gameid
$game_dt = $val['htm'];//local fate time for match
$date = new DateTime($game_dt);//assign DateTime to date var
$game_dt_f = $date->format('g:ia D jS M');//our date output formatting
$game++;

assigning variables to the API data. I left comments for each of them. The last line is the game counter adding one to the previous number. Now we can output data for each game where game is equal to or greater than 1

if ($game >= 1)
  { //if game is greater than or = 1 echo
  if ($h_team == $my_team)
    {
    echo "";
    }
    else
    {
    echo "";
    }

  echo "$game";
  echo "$v_team_n";
  echo "$h_team_n";
  echo "$arena";
  echo "$game_dt_f";
  echo "";
  }

You will see I did a check on if it was a LAC home game or not, if it is the background is green for that row (CSS class home).

Outputting this data I used Bootstrap with a compact, responsive and borderless style. The end result is

2018/19 Clippers fixture (Full screenshot)

The whole code is:


    
    
    


format('g:ia D jS M');//our date output formatting $game++; if ($game >= 1) {//if game is greater than or = 1 echo if ($h_team == $my_team) { echo ""; } else { echo ""; } echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } } } } ?>
# Visit Home Arena Local TD
$game$v_team_n$h_team_n$arena$game_dt_f

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago