Revision: 54295
                            
                                                            
                                    
                                        
Updated Code
                                    
                                    
                                                    
                        at December 20, 2011 09:42 by wa-rufio
                            
                            Updated Code
<?php
$uname = '';
$pswrd = '';
$con = mysql_connect("localhost", $uname, $pswrd);  
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("pitchfx",$con) or die('Error while selecting db');
/*Debugging purposes
$ob_file = fopen('test.txt' , 'w');
function ob_file_callback($buffer)
{
  global $ob_file;
  fwrite($ob_file,$buffer);
}
ob_start('ob_file_callback');*/
$start_date = '2011-03-30';  //Opening day -1 day
$end_date = '2011-10-03'; //Last regular season game
$cur_d = $start_date;
//Open dedicated XML stream
$opts = array(
    'http' => array(
        'user_agent' => 'PHP libxml agent',
    )
);
$context = stream_context_create($opts);
libxml_set_streams_context($context);
$name_p = array();
$value_p = array();
$p_seq = array();
while ($cur_d != $end_date) {
    $cur_d = date ("Y-m-d", strtotime ($cur_d . "+1 day"));
    $year = date("Y",strtotime($cur_d));
    $month = date("m",strtotime($cur_d));
    $day = date("d",strtotime($cur_d));
    $xmldoc = new DOMDocument();
    $url = 'http://gd2.mlb.com/components/game/mlb/year_' . $year . '/month_' . $month . '/day_' . $day . '/scoreboard.xml';
    $xmldoc->load($url);
    $doc = $xmldoc->documentElement;
    $game = $doc->getElementsbyTagName('game');
    foreach($game as $g){
       $gid = $g->getattribute('id');
       $t_url = 'http://gd2.mlb.com/components/game/mlb/year_' . $year . '/month_' . $month . '/day_' . $day . '/gid_' . $gid . '/inning/inning_all.xml';
       $xmldoc = new DOMDocument();
       $xmldoc->load($t_url);
       if(!$xmldoc->load($t_url)){
          continue;
       }
       $doc = $xmldoc->documentElement;
       $event = $doc->getElementsbyTagName('inning');
       $away = $event->item(0)->getattribute('away_team');
       $home = $event->item(0)->getattribute('home_team');
       foreach($event as $ev){
          $inning = $ev->getattribute('num');
          $atbat = $ev->getElementsbyTagName('atbat');
          foreach($atbat as $ab){
            if ($ab->parentNode->nodeName == 'top'){
               $top = 1;
            }
            else {
               $top = 0;
            }
            $ab_id = $ab->getattribute('batter');
            $p_id = $ab->getattribute('pitcher');
            $ab_hand = $ab->getattribute('stand');
            $p_hand = $ab->getattribute('p_throws');
            $pitch = $ab->getElementsbyTagName('pitch');
            foreach ($pitch as $p){
               unset($name_p);
               unset($value_p);
               unset($p_seq);
               foreach($p->attributes as $a) {
                  if ($a->nodeName == 'tfs' || $a->nodeName == 'tfs_zulu' || $a->nodeName == 'mt' || $a->nodeName=='cc' ) {
                    continue;
                  }
                  $value_p[] = $a->nodeValue;
                  $name_p[] = $a->nodeName; 
                  $p_seq[] = $p->getattribute('type');    
               }
                  $sql = "INSERT INTO pitchfx.pitches (inning,date_id,top_bot,away,home,pitch_id,pitch_hand,ab_id,ab_hand," . implode($name_p,',') . ",pitch_seq) VALUES ('$inning','$gid','$top','$away','$home','$p_id','$p_hand','$ab_id','$ab_hand','" . implode($value_p,"','") . "','" . implode($p_seq) . "')";
                  mysql_query($sql,$con) or die ('Error executing query. ' . mysql_error() . "\n");
            }
          }
       }
    }
}
//ob_end_flush();
//fclose($ob_file);
mysql_close($con);
?>
                                
                            Revision: 54294
                            
                                                            
                                    
                                        
Initial Code
                                    
                                    
                                                            
                                    
                                        
Initial URL
                                    
                                    
                                
                                                            
                                    
                                        
Initial Description
                                    
                                    
                                                            
                                    
                                        
Initial Title
                                    
                                    
                                                            
                                    
                                        
Initial Tags
                                    
                                    
                                                            
                                    
                                        
Initial Language
                                    
                                    
                                                    
                        at December 19, 2011 07:28 by wa-rufio
                            
                            Initial Code
<?php
$uname = '';
$pswrd = '';
$con = mysql_connect("localhost", $uname, $pswrd);  
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("pitchfx",$con) or die('Error while selecting db');
/*Debugging purposes
$ob_file = fopen('test.txt' , 'w');
function ob_file_callback($buffer)
{
  global $ob_file;
  fwrite($ob_file,$buffer);
}
ob_start('ob_file_callback');*/
$start_date = '2011-03-30';  //Opening day -1 day
$end_date = '2011-10-03'; //Last regular season game
$cur_d = $start_date;
//Open dedicated XML stream
$opts = array(
    'http' => array(
        'user_agent' => 'PHP libxml agent',
    )
);
$context = stream_context_create($opts);
libxml_set_streams_context($context);
$name_p = array();
$value_p = array();
$p_seq = array();
while ($cur_d != $end_date) {
    $cur_d = date ("Y-m-d", strtotime ($cur_d . "+1 day"));
    $year = date("Y",strtotime($cur_d));
    $month = date("m",strtotime($cur_d));
    $day = date("d",strtotime($cur_d));
    $xmldoc = new DOMDocument();
    $url = 'http://gd2.mlb.com/components/game/mlb/year_' . $year . '/month_' . $month . '/day_' . $day . '/scoreboard.xml';
    $xmldoc->load($url);
    $doc = $xmldoc->documentElement;
    $game = $doc->getElementsbyTagName('game');
    foreach($game as $g){
       $gid = $g->getattribute('id');
       $t_url = 'http://gd2.mlb.com/components/game/mlb/year_' . $year . '/month_' . $month . '/day_' . $day . '/gid_' . $gid . '/inning/inning_all.xml';
       $xmldoc = new DOMDocument();
       $xmldoc->load($t_url);
       if(!$xmldoc->load($t_url)){
          continue;
       }
       $doc = $xmldoc->documentElement;
       $event = $doc->getElementsbyTagName('inning');
       $away = $event->item(0)->getattribute('away_team');
       $home = $event->item(0)->getattribute('home_team');
       foreach($event as $ev){
          $inning = $ev->getattribute('num');
          $atbat = $ev->getElementsbyTagName('atbat');
          foreach($atbat as $ab){
            if ($ab->parentNode->nodeName == 'top'){
               $top = 1;
            }
            else {
               $top = 0;
            }
            $ab_id = $ab->getattribute('batter');
            $p_id = $ab->getattribute('pitcher');
            $ab_hand = $ab->getattribute('stand');
            $p_hand = $ab->getattribute('p_throws');
            $pitch = $ab->getElementsbyTagName('pitch');
            foreach ($pitch as $p){
               unset($name_p);
               unset($value_p);
               unset($p_seq);
               foreach($p->attributes as $a) {
                  if ($a->nodeName == 'tfs' || $a->nodeName == 'tfs_zulu' || $a->nodeName == 'mt' || $a->nodeName=='cc' ) {
                    continue;
                  }
                  $value_p[] = $a->nodeValue;
                  $name_p[] = $a->nodeName; 
                  $p_seq[] = $p->getattribute('type');    
               }
                  $sql = "INSERT INTO pitchfx.pitches (inning,date_id,top_bot,away,home,pitch_id,pitch_hand,ab_id,ab_hand," . implode($name_p,',') . ",pitch_seq) VALUES ('$inning','$gid','$top','$away','$home','$p_id','$p_hand','$ab_id','$ab_hand','" . implode($value_p,"','") . "','" . implode($p_seq,'') . "')";
                  mysql_query($sql,$con) or die ('Error executing query. ' . mysql_error() . "\n");
            }
          }
       }
    }
}
//ob_end_flush();
//fclose($ob_file);
mysql_close($con);
?>
                                Initial URL
Initial Description
Open PHP file and enter User Credentials for localhost. The default dates are set to negotiate the 2011 season, but change the date variables to match your desired season data. The start date should be the day before opening day, and the end date the last day of the regular season. Append ignored attributes to conditional statement on line 81 to match your pitchfx table structure.
Initial Title
Extract PitchFX from MLB.com
Initial Tags
mysql, php, DOM, xml
Initial Language
PHP