15

I need some sort of database or feed to access live scores(and possibly player stats) for the NFL. I want to be able to display the scores on my site for my pickem league and show the users if their pick is winning or not.

I'm not sure how to go about this. Can someone point me in the right direction?

Also, it needs to be free.

John Salvetti
  • 199
  • 2
  • 2
  • 10

7 Answers7

23

Disclaimer: I'm the author of the tools I'm about to promote.

Over the past year, I've written a couple Python libraries that will do what you want. The first is nflgame, which gathers game data (including play-by-play) from NFL.com's GameCenter JSON feed. This includes active games where data is updated roughly every 15 seconds. nflgame has a wiki with some tips on getting started.

I released nflgame last year, and used it throughout last season. I think it is reasonably stable.

Over this past summer, I've worked on its more mature brother, nfldb. nfldb provides access to the same kind of data nflgame does, except it keeps everything stored in a relational database. nfldb also has a wiki, although it isn't entirely complete yet.

For example, this will output all current games and their scores:

import nfldb

db = nfldb.connect()

phase, year, week = nfldb.current(db)
q = nfldb.Query(db).game(season_year=year, season_type=phase, week=week)
for g in q.as_games():
    print '%s (%d) at %s (%d)' % (g.home_team, g.home_score,
                                  g.away_team, g.away_score)

Since no games are being played, that outputs all games for next week with 0 scores. This is the output with week=1: (of the 2013 season)

CLE (10) at MIA (23)
DET (34) at MIN (24)
NYJ (18) at TB (17)
BUF (21) at NE (23)
SD (28) at HOU (31)
STL (27) at ARI (24)
SF (34) at GB (28)
DAL (36) at NYG (31)
WAS (27) at PHI (33)
DEN (49) at BAL (27)
CHI (24) at CIN (21)
IND (21) at OAK (17)
JAC (2) at KC (28)
PIT (9) at TEN (16)
NO (23) at ATL (17)
CAR (7) at SEA (12)

Both are licensed under the WTFPL and are free to use for any purpose.

N.B. I realized you tagged this as PHP, but perhaps this will point you in the right direction. In particular, you could use nfldb to maintain a PostgreSQL database and query it with your PHP program.

BurntSushi5
  • 13,917
  • 7
  • 52
  • 45
  • I'm sorry it took me so long to accept this answer as correct. I initially passed over it because it was in Python, and my knowledge in that area is...lacking. I took a look at some of the links that you posted, but I'm not really sure how I could use it with php. Sorry if I'm overlooking something obvious in the documentation. If you could point me further in the right direction, it would be greatly appreciated. – John Salvetti May 20 '14 at 02:48
  • 1
    Use nfldb to maintain a PostgreSQL database. Query it with PHP. Alternatively, use PHP to download the JSON feed from NFL.com yourself, but this will require some work to use because the data is in a poor format. (This is why using nfldb may be easier, even though it's using Python.) – BurntSushi5 May 20 '14 at 06:17
  • 1
    Look at the source of https://github.com/BurntSushi/nflgame to find out the URL of the JSON feed. It varies from game to game. – BurntSushi5 May 20 '14 at 06:19
  • 6
    dude! I've spent the last 8 hours playing with NFL DB and the other tools...you're awesome! – Michael Brown Aug 06 '14 at 08:12
  • BurntSushi, do you have the URLs for each player info sheet – Famic Tech Aug 18 '16 at 01:23
  • Do you where the NFL schedule file is? Or where one might look to find it? [Here is my SO question](http://stackoverflow.com/q/39263443/1640892). – Let Me Tink About It Sep 01 '16 at 05:40
  • @BurntSushi5 How do you write the above code for one team? such as, Buffalo? – James Dean Nov 12 '17 at 06:50
  • No longer maintained. – mbunch Aug 25 '20 at 20:03
4

So I found something that gives me MOST of what I was looking for. It has live game stats, but doesn't include current down, yards to go, and field position.

Regular Season: http://www.nfl.com/liveupdate/scorestrip/ss.xml

Post Season: http://www.nfl.com/liveupdate/scorestrip/postseason/ss.xml

I'd still like to find a live player stat feed to use to add Fantasy Football to my website, but I don't think a free one exists.

John Salvetti
  • 199
  • 2
  • 2
  • 10
  • 1
    Did you see my answer to this question? `nflgame` uses a JSON feed from NFL.com. For example: http://www.nfl.com/liveupdate/game-center/2012080953/2012080953_gtd.json --- My post should have answered your question. If it doesn't or needs clarification, please comment and I'll update it. – BurntSushi5 Mar 02 '14 at 07:32
  • 1
    Has anyone found all of the endpoints for NFL.com? or any other sports websites? – Joshua Szuslik Dec 01 '16 at 17:07
4

I know this is old, but this is what I use for scores only... maybe it will help someone some day. Note: there are some elements that you will not use and are specific for my site... but this would be a very good start for someone.

<?php
require('includes/application_top.php');
$week = (int)$_GET['week'];

//load source code, depending on the current week, of the website into a variable as a string
$url = "http://www.nfl.com/liveupdate/scorestrip/ss.xml"; //LIVE GAMES

if ($xmlData = file_get_contents($url)) {
$xml = simplexml_load_string($xmlData);
$json = json_encode($xml);
$games = json_decode($json, true);
}

$teamCodes = array(
'JAC' => 'JAX',
);

//build scores array, to group teams and scores together in games
$scores = array();
foreach ($games['gms']['g'] as $gameArray) {
$game = $gameArray['@attributes'];

//ONLY PULL SCORES FROM COMPLETED GAMES - F=FINAL, FO=FINAL OVERTIME
if ($game['q'] == 'F' || $game['q'] == 'FO') {
    $overtime = (($game['q'] == 'FO') ? 1 : 0);
    $away_team = $game['v'];
    $home_team = $game['h'];
    foreach ($teamCodes as $espnCode => $nflpCode) {
        if ($away_team == $espnCode) $away_team = $nflpCode;
        if ($home_team == $espnCode) $home_team = $nflpCode;
    }
    $away_score = (int)$game['vs'];
    $home_score = (int)$game['hs'];

    $winner = ($away_score > $home_score) ? $away_team : $home_team;
    $gameID = getGameIDByTeamID($week, $home_team);
    if (is_numeric(strip_tags($home_score)) && is_numeric(strip_tags($away_score))) {
            $scores[] = array(
                'gameID' => $gameID,
                'awayteam' => $away_team,
                'visitorScore' => $away_score,
                'hometeam' => $home_team,
                'homeScore' => $home_score,
                'overtime' => $overtime,
                'winner' => $winner
            );
    }
  }
}

//see how the scores array looks
//echo '<pre>' . print_r($scores, true) . '</pre>';
echo json_encode($scores);

//game results and winning teams can now be accessed from the scores array
//e.g. $scores[0]['awayteam'] contains the name of the away team (['awayteam'] part) from the first game on the page ([0] part)
Mark Jones
  • 193
  • 1
  • 9
1

I've spent the last year or so working on a simple CLI tool to easily create your own NFL databases. It currently supports PostgreSql and Mongo natively, and you can programmatically interact with the Engine if you'd like to extend it.

Want to create your own different database (eg MySql) using the Engine (or even use Postgres/Mongo but with your own schema)? Simply implement an interface and the Engine will do the work for you.

Running everything, including the database setup and updating with all the latest stats, can be done in a single command:

ffdb setup

I know this question is old, but I also realize that there's still a need out there for a functional and easy-to-use tool to do this. The entire reason I built this is to power my own football app in the near future, and hopefully this can help others.

Also, because the question is fairly old, a lot of the answers are not working at the current time, or reference projects that are no longer maintained.

Check out the github repo page for full details on how to download the program, the CLI commands, and other information:

FFDB Github Repository

Isaiah Lee
  • 448
  • 2
  • 10
0
$XML = "http://www.nfl.com/liveupdate/scorestrip/ss.xml";
$lineXML = file_get_contents($XML);
$subject = $lineXML;
//match and capture week then print
$week='/w="([0-9])/';
preg_match_all($week, $subject, $week);
echo "week ".$week[1][0]."<br/>"; 
$week2=$week[1][0];
echo $week2; 

//capture team, scores in two dimensional array  
$pattern = '/hnn="(.+)"\shs="([0-9]+)"\sv="[A-Z]+"\svnn="(.+)"\svs="([0-9]+)/';
preg_match_all($pattern, $subject, $matches);

//enumerate length of array (number games played)
$count= count($matches[0]);

 //print array values
 for ($x = 0; $x < $count ; $x++) {
         echo"<br/>";
         //print home team
         echo $matches[1][$x],"   ",
         //print home score
         $matches[2][$x],"   ",
         //print visitor team
         $matches[3][$x],"   ",
         //print visitor score
         $matches[4][$x];
         echo "<br/>";

       }
Chris
  • 1
0

I was going through problems finding a new source for the 2021 season. Well I finally found one on ESPN.

http://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard

Returns the results in JSON format.

Patrick
  • 53
  • 7
-3

I recommend registering at http://developer.espn.com and get access to their JSON API. It just took me 5 minutes and they have documentation to make pretty much any call you need.

dokun1
  • 2,120
  • 19
  • 37
  • I just looked into this, it is severly limited unless you are a "partner" and thus will not give him access to the data he wants. – Ross Larson Jan 05 '14 at 20:05
  • 1
    yeah i actually found out more right after i signed up, it is pretty limited. shoulda waited to answer... – dokun1 Jan 06 '14 at 20:14