Jump to content

Recommended Posts

I'm developing some games and was wondering how I can grab the updated scores for the NFL as they occur in realtime so the admin doesn't have to go into my php script and input them manually.  I want to store the scores of each game into variables so I can use them.

 

anybody have an idea?  sorry if it's not the appropriate forum but it is technically php related

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/
Share on other sites

This post demonstrates an easy way to crawl. You will just have to make the search pattern suit your needs.

 

Example: The scores at NFL.com is found inside div tags with the class "scoresBoxTeamScore". A search pattern to grab those scores would look like

 

'|<div class="scoresBoxTeamScore">(.*?)</div>|is'

 

instead of

 

'|<a.*?href="(.*?)"|is'

 

in the post linked above.

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626421
Share on other sites

This post demonstrates an easy way to crawl. You will just have to make the search pattern suit your needs.

 

Example: The scores at NFL.com is found inside div tags with the class "scoresBoxTeamScore". A search pattern to grab those scores would look like

 

'|<div class="scoresBoxTeamScore">(.*?)</div>|is'

 

instead of

 

'|<a.*?href="(.*?)"|is'

 

in the post linked above.

 

ok I'll check it out more.  I'm not familiar with preg replace.  if I'm on http://www.nfl.com/scores I do a view page source and can see those "scoresBoxTeamScore" such as

<div class="scoresBoxTeamScore">17</div>

and

<div class="scoresBoxTeamScore">18</div>

 

 

sorry to be dumb but could you show me some code to grab each teams score into a variable such as:

$NE = // score

$Ind = // score

 

Each week the matchups will change of course.

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626429
Share on other sites

If you specify more precisely what you want to grab, I'll give you a working example. I.e. do you want the scores of all the games listed or only from one match (and which one)? And I think it may be easier to grab them from Yahoo! at http://sports.yahoo.com/nfl/scoreboard. NFL.com also lists upcoming games with no scores to grab.

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626816
Share on other sites

If you specify more precisely what you want to grab, I'll give you a working example. I.e. do you want the scores of all the games listed or only from one match (and which one)? And I think it may be easier to grab them from Yahoo! at http://sports.yahoo.com/nfl/scoreboard. NFL.com also lists upcoming games with no scores to grab.

 

I really just need to find out who won every matchup during the current week.  If we're on Week 1 of the NFL then I need to keep searching to see if any games have been completed and grab the scores of each matchup to find out who won.  I'm not sure if nfl.com or yahoo has a variable so you know when the game is done or not.

 

Does yahoo update the teams scores in realtime or as close as they can?

 

My script will tell you which week we're on.  $Current_Week

I'd like to grab the score for every team for that week.  so something like

$NE_Score =

$Ind_Score =

$Dal_Score =

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626825
Share on other sites

I would guess that both pages are updated ~ realtime. And it also says "Final" on both pages, when the game is done, so that's a way to check it. Yahoo's code is the easiest to handle I think, I'll try to write a script grabbing from there. If I succeed, you should try to understand the code, so you maybe can write it yourself next time. I'll try to comment it throughly :)

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626835
Share on other sites

sounds good.  your the man!  thanks

 

oh ya, since it says "final" it would be good to grab the actual winner so I don't have to calculate the winner if it's provided already for me. 

$Matchup_01_Winner =

$Matchup_02_Winner =

 

I'd still like to grab the scores in real time for every matchup (or a specific matchup) for another script I have so if you could provide both that would be awesome

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626836
Share on other sites

Check it out:

 

<?php
//set current game week
$Current_Week = 'p4'; #preweek 3, just to see if the script works. When the season starts, '1' will denote week one etc.

//load source code, depending on the current week, of the website into a variable as a string
$url = "http://sports.yahoo.com/nfl/scoreboard?w=$Current_Week";
$string = file_get_contents($url);

//set search pattern (using regular expressions)
$find = '|<a href="/nfl/teams/.*?">(.*?)</a>.*?<td align="right" class="ysptblclbg6 total">.*?<span class="yspscores">(.*?) |is';

//search the string for the pattern, and store the content found inside the set of parens in the array $matches
//$matches[1] is going to hold team names in the order they appear on the page, and $matches[2] the scores
preg_match_all($find, $string, $matches);

//initiate scores array, to group teams and scores together in games
$scores = array();

//count number of teams found, to be used in the loop below
$count = count($matches[1]);

//loop from 0 to $count, in steps of 2
//this is done in order to group 2 teams and 2 scores together in games, with each iteration of the loop
//trim() is used to trim away any whitespace surrounding the team names and scores
//strip_tags() is used to remove the HTML bold tag (<b>) from the winning scores
for ($i = 0; $i < $count; $i += 2) {
$away_team = trim($matches[1][$i]);
$away_score = trim($matches[2][$i]);
$home_team = trim($matches[1][$i + 1]);
$home_score = trim($matches[2][$i + 1]);
$winner = (strpos($away_score, '<') === false) ? $home_team : $away_team;
$scores[] = array(
	'awayteam' => $away_team,
	'awayscore' => strip_tags($away_score),
	'hometeam' => $home_team,
	'homescore' => strip_tags($home_score),
	'winner' => $winner
);
}

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

//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)
?>

 

If you need help handling the $scores array, just ask. I can't guarantee that the script will work when the season starts, but I'm pretty sure it will.

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626863
Share on other sites

  • 2 weeks later...

I see the script 'erroneously' also lists the upcoming matches. A quick fix to remove these matches from the $scores array, is to add this line just inside the for loop:

 

if (!ctype_digit(strip_tags(trim($matches[2][$i])))) {continue;}

 

Full code:

 

<?php
//set current game week
$Current_Week = '1'; #preweek 3, just to see if the script works. When the season starts, '1' will denote week one etc.

//load source code, depending on the current week, of the website into a variable as a string
$url = "http://sports.yahoo.com/nfl/scoreboard?w=$Current_Week";
$string = file_get_contents($url);

//set search pattern (using regular expressions)
$find = '|<a href="/nfl/teams/.*?">(.*?)</a>.*?<td align="right" class="ysptblclbg6 total">.*?<span class="yspscores">(.*?) |is';

//search the string for the pattern, and store the content found inside the set of parens in the array $matches
//$matches[1] is going to hold team names in the order they appear on the page, and $matches[2] the scores
preg_match_all($find, $string, $matches);

//initiate scores array, to group teams and scores together in games
$scores = array();

//count number of teams found, to be used in the loop below
$count = count($matches[1]);

//loop from 0 to $count, in steps of 2
//this is done in order to group 2 teams and 2 scores together in games, with each iteration of the loop
//trim() is used to trim away any whitespace surrounding the team names and scores
//strip_tags() is used to remove the HTML bold tag (<b>) from the winning scores
for ($i = 0; $i < $count; $i += 2) {
if (!ctype_digit(strip_tags(trim($matches[2][$i])))) {continue;}
$away_team = trim($matches[1][$i]);
$away_score = trim($matches[2][$i]);
$home_team = trim($matches[1][$i + 1]);
$home_score = trim($matches[2][$i + 1]);
$winner = (strpos($away_score, '<') === false) ? $home_team : $away_team;
$scores[] = array(
	'awayteam' => $away_team,
	'awayscore' => strip_tags($away_score),
	'hometeam' => $home_team,
	'homescore' => strip_tags($home_score),
	'winner' => $winner
);
}

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

//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)
?>

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-634368
Share on other sites

This code is great, but for safety's sake, it would be very important to attach a date to each game in the array.

 

The following code seems to proceed each section date label:

 

    <tr class="ysptblthbody2">

      <td colspan="3" height="18" class="yspdetailttl"> 

 

what does the find query look like to grab the date (e.g., "Sunday September 7, 2008"), given that it doesn't necessarily appear before each game?

 

 

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-636349
Share on other sites

Actually, now that I look while a game is in session, the code as presented also could lead you astray by presenting you with a "Final" score at halftime. A game status is needed that captures the "1st", "2nd", "Half", "3rd", "4th", and "Final" as a discreet value in the scores array. The regex is beyond my ability, but I'd sure be grateful for the help.

 

    <td align="right" class="ysptblclbg6 total"><span class="yspscores">Half</span> </td>

 

 

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-637150
Share on other sites

Hi there. I figured how to save the game status in the array, and only display a winner when the status is 'Final', but can't quite get my head around adding the game date. Updated code:

 

<?php
//set current game week
$Current_Week = '1';

//load source code, depending on the current week, of the website into a variable as a string
$url = "http://sports.yahoo.com/nfl/scoreboard?w=$Current_Week";
$string = file_get_contents($url);

//set search pattern (using regular expressions)
$find = '~<a href="/nfl/teams/.*?">(.*?)</a>.*?<td align="right" class="ysptblclbg6 total">.*?<span class="yspscores">(.*?) </span>.*?</td>.*?<td align="right" class="ysptblclbg6 total"><span class="yspscores">(.*?)</span>~is';

//search the string for the pattern, and store the content found inside the set of parens in the array $matches
//$matches[1] is going to hold team names in the order they appear on the page, and $matches[2] the scores
preg_match_all($find, $string, $matches);

//initiate scores array, to group teams and scores together in games
$scores = array();

//count number of teams found, to be used in the loop below
$count = count($matches[1]);

//loop from 0 to $count, in steps of 2
//this is done in order to group 2 teams and 2 scores together in games, with each iteration of the loop
//trim() is used to trim away any whitespace surrounding the team names and scores
//strip_tags() is used to remove the HTML bold tag (<b>) from the winning scores
for ($i = 0; $i < $count; $i += 2) {
if (!ctype_digit(strip_tags(trim($matches[2][$i])))) {continue;}
$away_team = trim($matches[1][$i]);
$away_score = trim($matches[2][$i]);
$home_team = trim($matches[1][$i + 1]);
$home_score = trim($matches[2][$i + 1]);
$winner = (strpos($away_score, '<') === false) ? $home_team : $away_team;
$game_status = $matches[3][$i];
$scores[] = array(
	'awayteam' => $away_team,
	'awayscore' => strip_tags($away_score),
	'hometeam' => $home_team,
	'homescore' => strip_tags($home_score),
	'gamestatus' => $game_status,
	'winner' => ($game_status == 'Final') ? $winner : '-'
);
}

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

//game standings, game statuses and winning teams (of finished games) 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)
?>

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-637335
Share on other sites

I also nailed the game dates, yay 8)

 

Updated code (getting bigger and bigger, eh?):

 

<?php
//set current game week
$Current_Week = '1';

//load source code, depending on the current week, of the website into a variable as a string
$url = "http://sports.yahoo.com/nfl/scoreboard?w=$Current_Week";
$string = file_get_contents($url);

//search and store game dates, later linked to the games
preg_match_all('~<td colspan="3" height="18" class="yspdetailttl"> (.*?)</td>~is', $string, $matches);
$matches = array_map('trim', $matches[1]);
$pos_arr = array();
foreach ($matches as $date) {
$pos_arr[strpos($string, $date)] = $date;
}

//set search pattern (using regular expressions)
$find = '~<a href="/nfl/teams/.*?">(.*?)</a>.*?<td align="right" class="ysptblclbg6 total">.*?<span class="yspscores">(.*?) </span>.*?</td>.*?<td align="right" class="ysptblclbg6 total"><span class="yspscores">(.*?)</span>~is';

//search the string for the pattern, and store the content found inside the set of parens in the array $matches
//$matches[1] is going to hold team names in the order they appear on the page, and $matches[2] the scores
preg_match_all($find, $string, $matches);

//initiate scores array, to group teams and scores together in games
$scores = array();

//count number of teams found, to be used in the loop below
$count = count($matches[1]);

//loop from 0 to $count, in steps of 2
//this is done in order to group 2 teams and 2 scores together in games, with each iteration of the loop
//trim() is used to trim away any whitespace surrounding the team names and scores
//strip_tags() is used to remove the HTML bold tag (<b>) from the winning scores
for ($i = 0; $i < $count; $i += 2) {
if (!ctype_digit(strip_tags(trim($matches[2][$i])))) {continue;}
$away_team = trim($matches[1][$i]);
//associate game dates with games, by checking which date the game appears after
$pos = strpos($string, $away_team . '</a>');
foreach ($pos_arr as $datepos => $date) {
	if ($pos > $datepos) {
		$game_date = $date;
	}
}
$away_score = trim($matches[2][$i]);
$home_team = trim($matches[1][$i + 1]);
$home_score = trim($matches[2][$i + 1]);
$winner = (strpos($away_score, '<') === false) ? $home_team : $away_team;
$game_status = $matches[3][$i];
$scores[] = array(
	'gamedate' => $game_date,
	'gamestatus' => $game_status,
	'awayteam' => $away_team,
	'awayscore' => strip_tags($away_score),
	'hometeam' => $home_team,
	'homescore' => strip_tags($home_score),
	'winner' => ($game_status == 'Final') ? $winner : '-'
);
}

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

//game standings, game statuses and winning teams (of finished games) 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)
?>

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-637683
Share on other sites

Obviously because no game in week 2 has started yet. The script only grabs ongoing and finished games. Although I haven't tested it on an ongoing game, so I'm not 100% sure it works. But for finished games it should.

 

The source code looks different for future games, so the RegEx will have to be different if you wanna grab these games.

Link to comment
https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-640775
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.