chiefrokka Posted August 26, 2008 Share Posted August 26, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/ Share on other sites More sharing options...
phoenixx Posted August 26, 2008 Share Posted August 26, 2008 Just scrape the page using preg_replace Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626418 Share on other sites More sharing options...
thebadbad Posted August 26, 2008 Share Posted August 26, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626421 Share on other sites More sharing options...
chiefrokka Posted August 26, 2008 Author Share Posted August 26, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626429 Share on other sites More sharing options...
Dada78 Posted August 26, 2008 Share Posted August 26, 2008 Does it have to be in php solution? Have you looked in RSS/XML feed? You are going to need to have the entire schedule layout out somewhere or you have to manually update the match ups from week to week anyways. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626439 Share on other sites More sharing options...
chiefrokka Posted August 26, 2008 Author Share Posted August 26, 2008 I'll use whatever way is best but my scripts are in php so I just need to store each teams score into a php variable somehow. my scripts run each week of the season so yes I'd need a way to grab them each week. no clue how to do this really Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626442 Share on other sites More sharing options...
thebadbad Posted August 27, 2008 Share Posted August 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626816 Share on other sites More sharing options...
chiefrokka Posted August 27, 2008 Author Share Posted August 27, 2008 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 = Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626825 Share on other sites More sharing options...
thebadbad Posted August 27, 2008 Share Posted August 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626835 Share on other sites More sharing options...
chiefrokka Posted August 27, 2008 Author Share Posted August 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626836 Share on other sites More sharing options...
Mchl Posted August 27, 2008 Share Posted August 27, 2008 Use http://www.totallyscored.com/get/sport/7 perhaps? Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626840 Share on other sites More sharing options...
thebadbad Posted August 27, 2008 Share Posted August 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626863 Share on other sites More sharing options...
thebadbad Posted August 27, 2008 Share Posted August 27, 2008 To grab the realtime scores, I would have to see how the HTML looks during a game. There's a good chance it's different from the HTML used to display final scores. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626866 Share on other sites More sharing options...
chiefrokka Posted August 27, 2008 Author Share Posted August 27, 2008 thanks dude. I will play with your code this weekend when the games start and see how it works. I appreciate it! your comments were great by the way Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626884 Share on other sites More sharing options...
chiefrokka Posted August 27, 2008 Author Share Posted August 27, 2008 thebadbad... how do I pull the 'final' winner of each matchup so I don't have to calculate who won each matchup? not that it's hard to do Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626904 Share on other sites More sharing options...
thebadbad Posted August 27, 2008 Share Posted August 27, 2008 $scores[n]['winner'], where n is the number of the game as it appears on the page (i.e. in the HTML source). 0 is the first game on the page. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-626952 Share on other sites More sharing options...
underparnv Posted September 4, 2008 Share Posted September 4, 2008 Thank you very much for this code. I do, however, have a request. How could you grab only the scores that are final?!?! Thanks again!! Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-634117 Share on other sites More sharing options...
thebadbad Posted September 5, 2008 Share Posted September 5, 2008 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) ?> Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-634368 Share on other sites More sharing options...
jdashca Posted September 8, 2008 Share Posted September 8, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-636349 Share on other sites More sharing options...
jdashca Posted September 9, 2008 Share Posted September 9, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-637150 Share on other sites More sharing options...
thebadbad Posted September 9, 2008 Share Posted September 9, 2008 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) ?> Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-637335 Share on other sites More sharing options...
thebadbad Posted September 9, 2008 Share Posted September 9, 2008 I also nailed the game dates, yay 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) ?> Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-637683 Share on other sites More sharing options...
chiefrokka Posted September 9, 2008 Author Share Posted September 9, 2008 awesome. your the man. I'm going to try integrating your code into my scripts soon once I get some free time. I did try it week 4 during preseason and it was capturing the scores very nicely. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-637684 Share on other sites More sharing options...
jdashca Posted September 13, 2008 Share Posted September 13, 2008 Something is wrong... the new code that also captures dates works for week 1, but not for week 2. In other words, when I change $Current_Week = '1'; to $Current_Week = '1'; The scores array doesn't get filled. Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-640749 Share on other sites More sharing options...
thebadbad Posted September 13, 2008 Share Posted September 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/#findComment-640775 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.