Code / PHP Snippets / ESPN Football Score Fetch

  1. <?
  2. // Live Football Score Fetch
  3.  
  4. // Get Live Score Information from ESPN Soccernet.
  5. // You'll need a frontend to this, such as an ajax frontend
  6. // or IRC bot.
  7.  
  8. // Make a database connection
  9. $_db = mysql_connect("localhost","user","password") or die ("Unable to access data source.");
  10. $_dbtable = mysql_select_db("database",$_db) or die("Could not select database.");
  11.  
  12. // This function will remove the specified HTML tags out of a string.
  13. function strip_selected_tags($text, $tags = array()) {
  14.    $args = func_get_args();
  15.    $text = array_shift($args);
  16.    $tags = func_num_args() > 2 ? array_diff($args,array($text))  : (array)$tags;
  17.    foreach ($tags as $tag){
  18.            if(preg_match_all('/<'.$tag.'[^>]*>(.*)</'.$tag.'>/iU', $text, $found)){
  19.                    $text = str_replace($found[0],$found[1],$text);
  20.          }
  21.    }
  22.  
  23.    return $text;
  24. }
  25.  
  26. // This function uses libcurl to fetch the contents of a page, its a
  27. // huge amount faster than php's own file_get_contents()
  28. function get_content($url) {
  29.    $ch = curl_init();
  30.    curl_setopt ($ch, CURLOPT_URL, $url);
  31.    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla 5.0");
  32.    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  33.    curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
  34.    ob_start();
  35.    curl_exec ($ch);
  36.    curl_close ($ch);
  37.    $string = ob_get_contents();
  38.    ob_end_clean();
  39.    return $string;  
  40. }
  41.  
  42. // Start of the regex to match
  43. $start = "<div id="OutputWindow" class="vidiText">";
  44. // End of the regex to match
  45. $end = "<div style="clear: both; padding: 0px 0; border-bottom: 1px solid #CCCCCC; width:748px;">";
  46.  
  47. // The feed url to fetch
  48. $url = "http://soccernet.espn.go.com/scoreboard/vidiprinter?cc=5739&pk=".time();
  49. $page = get_content($url);
  50. preg_match("/$start(.*)$end/s",$page,$match);
  51. $scores = $match[1];
  52. $scores = strip_selected_tags($scores,"div");
  53. preg_match("/<a.*>(.*)</a>/s",$scores,$match2);
  54.  
  55. $plinks = explode("</a>",$scores);
  56. $links = array_reverse($plinks);
  57.  
  58. // Loop through the links collected and add them to the database.
  59. foreach($links as $link) {
  60.         // Add each link to db
  61.         $final = trim(strip_tags($link));
  62.         $final = str_replace("  "," ",$final);
  63.         if ($final) {
  64.                 // Lookup and see if its been added
  65.                 $q = mysql_query("SELECT COUNT(*) FROM scores WHERE text = '".addslashes($final)."'");
  66.                 list($t) = mysql_fetch_array($q);
  67.                 if ($t) {
  68.                         // Don't add it, its there already.
  69.                         echo "[INFO] Skipping $finaln";
  70.                 } else {
  71.                         // Add it to the database
  72.                         $i = mysql_query("INSERT INTO `scores` (`timestamp`,`status`,`text`) VALUES ('".time()."','1','".addslashes($final)."')");
  73.                         if (mysql_insert_id()) {
  74.                                 echo "[INFO] Added $final to databasen";
  75.                         } else {
  76.                                 echo "[FAIL] Unable to add $finaln";
  77.                         }
  78.                 }
  79.         }
  80. }
  81. ?>

Download as Text : January 6th, 2009