/ Published in: PHP
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.
Append ignored attributes to conditional statement on line 81 to match your pitchfx table structure.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php $uname = ''; $pswrd = ''; if (!$con) { } /*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 'user_agent' => 'PHP libxml agent', ) ); while ($cur_d != $end_date) { $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){ 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'); } } } } } } //ob_end_flush(); //fclose($ob_file); ?>