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 "<tr class='home'>";
    }
    else
    {
    echo "<tr>";
    }

  echo "<td><a href='$gid'>$game</a></td>";
  echo "<td>$v_team_n</td>";
  echo "<td>$h_team_n</td>";
  echo "<td>$arena</td>";
  echo "<td>$game_dt_f</td>";
  echo "</tr>";
  }

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

NBA api php fixture
2018/19 Clippers fixture (Full screenshot)

The whole code is:

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css"https://cpwp.b-cdn.net/>
    <style>
        .table-borderless > tbody > tr > td,
        .table-borderless > tbody > tr > th,
        .table-borderless > tfoot > tr > td,
        .table-borderless > tfoot > tr > th,
        .table-borderless > thead > tr > td,
        .table-borderless > thead > tr > th {
            border: none;
        }

        tr {
            background: #ffc1c1;
        }

        .home {
            background: rgba(23, 210, 82, 0.36);
        }

        a {
            color: #353535;
            text-decoration: none;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="table-responsive">
        <table class="table table-sm table-borderless table-condensed">
            <thead>
            <tr>
                <th scope="col">#</th>
                <th scope="col">Visit</th>
                <th scope="col">Home</th>
                <th scope="col">Arena</th>
                <th scope="col">Local TD</th>
            </tr>
            </thead>
            <tbody>
            <?php
            ini_set('max_execution_time', 300); //300 seconds = 5 minutes
            $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);
            $my_team = 'LAC';//
            $game = -4;//start -4 games because of pre season
            for ($x = 1; $x <= 7; $x++) {
                foreach ($data['lscd'][$x]['mscd']['g'] as $val) {//$x = month?
                    if (strpos($val['gcode'], $my_team) !== false) {
                        $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);
                        $game_dt_f = $date->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 "<tr class='home'>";
                            } else {
                                echo "<tr>";
                            }
                            echo "<td><a href='$gid'>$game</a></td>";
                            echo "<td>$v_team_n</td>";
                            echo "<td>$h_team_n</td>";
                            echo "<td>$arena</td>";
                            echo "<td>$game_dt_f</td>";
                            echo "</tr>";
                        }
                    }
                }
            }
            ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>