Sign up to create your own snipts, or login.

Public snipts » pkarl's snipts » Parse Yahoo! Weather Feeds with SimpleXML & PHP

posted on Feb 07, 2009 at 10:13 a.m. EST in 
  • <?php
    $weather_feed = file_get_contents("http://weather.yahooapis.com/forecastrss?p=14607&u=f");
    $weather = simplexml_load_string($weather_feed);
    if(!$weather) die('weather failed');
    $copyright = $weather->channel->copyright;
    
    $channel_yweather = $weather->channel->children("http://xml.weather.yahoo.com/ns/rss/1.0");
    
    foreach($channel_yweather as $x => $channel_item) 
    	foreach($channel_item->attributes() as $k => $attr) 
    		$yw_channel[$x][$k] = $attr;
    
    $item_yweather = $weather->channel->item->children("http://xml.weather.yahoo.com/ns/rss/1.0");
    
    foreach($item_yweather as $x => $yw_item) {
    	foreach($yw_item->attributes() as $k => $attr) {
    		if($k == 'day') $day = $attr;
    		if($x == 'forecast') { $yw_forecast[$x][$day . ''][$k] = $attr;	} 
    		else { $yw_forecast[$x][$k] = $attr; }
    	}
    }
    
    var_dump($yw_forecast)
    ?>
    

    copy | embed

0 Comments

Sign up, or login to leave a comment.