php 获取实时 NFL 比分/统计数据以阅读和操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18686884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get live NFL scores/stats to read and manipulate?
提问by John Salvetti
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.
我需要某种数据库或提要来访问 NFL 的实时比分(以及可能的球员统计数据)。我希望能够在我的网站上显示我的pickem 联赛的分数,并向用户展示他们的选择是否获胜。
I'm not sure how to go about this. Can someone point me in the right direction?
我不知道该怎么做。有人可以指出我正确的方向吗?
Also, it needs to be free.
此外,它必须是免费的。
回答by BurntSushi5
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 wikiwith some tips on getting started.
在过去的一年里,我编写了几个 Python 库,它们可以满足您的需求。第一个是nflgame,它从 NFL.com 的 GameCenter JSON 提要收集游戏数据(包括逐个游戏)。这包括数据大约每 15 秒更新一次的活跃游戏。nflgame有一个 wiki,里面有一些入门技巧。
I released nflgame last year, and used it throughout last season. I think it is reasonably stable.
我去年发布了 nflgame,并在上个赛季使用了它。我认为它相当稳定。
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.
在过去的这个夏天,我一直在研究它更成熟的兄弟nfldb。nfldb 提供对与 nflgame 相同类型的数据的访问,除了它将所有内容都存储在关系数据库中。nfldb也有一个 wiki,尽管它还没有完全完成。
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)
由于没有比赛正在进行,因此输出下周所有比赛的0
分数。这是输出week=1
:(2013 赛季)
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.
两者均根据 WTFPL 获得许可,可免费用于任何目的。
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.
注意,我意识到您将其标记为 PHP,但这可能会为您指明正确的方向。特别是,您可以nfldb
用来维护一个 PostgreSQL 数据库并使用您的 PHP 程序查询它。
回答by John Salvetti
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
常规赛:http: //www.nfl.com/liveupdate/scorestrip/ss.xml
Post Season: http://www.nfl.com/liveupdate/scorestrip/postseason/ss.xml
季后赛: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.
我仍然想找到一个实时球员统计信息,用于将 Fantasy Football 添加到我的网站,但我认为不存在免费的。
回答by Mark Jones
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)
回答by Isaiah Lee
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.
在过去一年左右的时间里,我一直致力于开发一个简单的 CLI 工具,以轻松创建您自己的 NFL 数据库。它目前本身支持 PostgreSql 和 Mongo,如果你想扩展它,你可以以编程方式与引擎交互。
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.
想要使用引擎创建您自己的不同数据库(例如 MySql)(或者甚至使用 Postgres/Mongo 但使用您自己的架构)?只需实现一个接口,引擎就会为您完成工作。
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:
查看 github 存储库页面,了解有关如何下载程序、CLI 命令和其他信息的完整详细信息:
回答by Chris
$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/>";
}
回答by dokun1
I recommend registering at http://developer.espn.comand get access to their JSON API. It just took me 5 minutes and they have documentation to make pretty much any call you need.
我建议在http://developer.espn.com 上注册并访问他们的 JSON API。我只花了 5 分钟,他们有文档可以拨打您需要的几乎任何电话。