Get Yahoo Weather with SimplePie

SimplePie is a new PHP library made to make parsing RSS feeds easy and fun. Yes I said fun, because its very easy and the docs are well written. RSS is more than just news, it can be images, video and podcasts. Mash up the data however you want, I’ve seen some innovative and unique things done so far. They also offer sample code on making a site like popurls/Original Signal.

Figure I’d share some code I used with SimplePie and a Yahoo Weather RSS feed. I can’t post it all on the homepage as its really long. It does transform and output the data nicely (not any CSS styling but I do calculate the wind direction and some other things). Please note that Yahoo only permits you to use their Weather RSS feed on non-commercial sites.



//location
$weather_location = $feed->get_channel_tags('http://xml.weather.yahoo.com/ns/rss/1.0', 'location');
$location_city = $weather_location[0]['attribs']['']['city'];
$location_region = $weather_location[0]['attribs']['']['region'];
//units
$weather_units = $feed->get_channel_tags('http://xml.weather.yahoo.com/ns/rss/1.0', 'units');
$units_temperature = $weather_units[0]['attribs']['']['temperature'];
$units_distance = $weather_units[0]['attribs']['']['distance'];
$units_pressure = $weather_units[0]['attribs']['']['pressure'];
$units_speed = $weather_units[0]['attribs']['']['speed'];
//wind
$weather_wind = $feed->get_channel_tags('http://xml.weather.yahoo.com/ns/rss/1.0', 'wind');
$wind_chill = $weather_wind[0]['attribs']['']['chill'];
$wind_direction = $weather_wind[0]['attribs']['']['direction'];
$wind_speed = $weather_wind[0]['attribs']['']['speed'];
//atmosphere
$weather_atmosphere = $feed->get_channel_tags('http://xml.weather.yahoo.com/ns/rss/1.0', 'atmosphere');
$atmosphere_humidity = $weather_atmosphere[0]['attribs']['']['humidity'];
$atmosphere_visibility = $weather_atmosphere[0]['attribs']['']['visibility'];
$atmosphere_pressure = $weather_atmosphere[0]['attribs']['']['pressure'];
$atmosphere_rising = $weather_atmosphere[0]['attribs']['']['rising'];
//astronomy
$weather_astronomy = $feed->get_channel_tags('http://xml.weather.yahoo.com/ns/rss/1.0', 'astronomy');
$astronomy_sunrise = $weather_astronomy[0]['attribs']['']['sunrise'];
$astronomy_sunset = $weather_astronomy[0]['attribs']['']['sunset'];
//condition
$weather_condition = $item->get_item_tags('http://xml.weather.yahoo.com/ns/rss/1.0', 'condition');
$condition_text = $weather_condition[0]['attribs']['']['text'];
$condition_code = $weather_condition[0]['attribs']['']['code'];
$condition_temp = $weather_condition[0]['attribs']['']['temp'];
$condition_date = $weather_condition[0]['attribs']['']['date'];
//condition
$weather_forecast = $item->get_item_tags('http://xml.weather.yahoo.com/ns/rss/1.0', 'forecast');
//figure out wind direction
if ($wind_direction >= 348.75 || $wind_direction < 11.25)
$wind_direction_converted = "N";//N
else if ($wind_direction >= 11.25 && $wind_direction < 33.75)
$wind_direction_converted = "NNE";//NNE
else if ($wind_direction >= 33.75 && $wind_direction < 56.25)
$wind_direction_converted = "NE";//NE
else if ($wind_direction >= 56.25 && $wind_direction < 78.75)
$wind_direction_converted = "ENE";//ENE
else if ($wind_direction >= 78.75 && $wind_direction < 101.25)
$wind_direction_converted = "E";//E
else if ($wind_direction >= 101.25 && $wind_direction < 123.75)
$wind_direction_converted = "ESE";//ESE
else if ($wind_direction >= 123.75 && $wind_direction < 146.25)
$wind_direction_converted = "SE";//SE
else if ($wind_direction >= 146.25 && $wind_direction < 168.75)
$wind_direction_converted = "SSE";//SSE
else if ($wind_direction >= 168.75 && $wind_direction < 191.25)
$wind_direction_converted = "S";//S
else if ($wind_direction >= 191.25 && $wind_direction < 213.75)
$wind_direction_converted = "SSW";//SSW
else if ($wind_direction >= 213.75 && $wind_direction < 236.25)
$wind_direction_converted = "SW";//SW
else if ($wind_direction >= 236.25 && $wind_direction < 258.75)
$wind_direction_converted = "WSW";//WSW
else if ($wind_direction >= 258.75 && $wind_direction < 281.25)
$wind_direction_converted = "W";//W
else if ($wind_direction >= 281.25 && $wind_direction < 303.75)
$wind_direction_converted = "WNW";//WNW
else if ($wind_direction >= 303.75 && $wind_direction < 326.25)
$wind_direction_converted = "NW";//NW
else if ($wind_direction >= 326.25 && $wind_direction < 348.75)
$wind_direction_converted = "NNW";//NNW
else
$wind_direction_converted = "?";
$data .= '<h3>Current Conditions</h3>';
$data .= '<br /><img src="http://l.yimg.com/us.yimg.com/i/us/we/52/'.$condition_code.'.gif" alt="'.$condition_text.'" />';//icon
$data .= "<br />$condition_text";//weather
$data .= "<br />$condition_temp&deg;";//temp
$total_forecast_days = count($weather_forecast);//get how many days of forecast do we have
for ($x = 0; $x < $total_forecast_days; $x++)
{
if ($x == 0)
$data .= '<h4>Today</h4>';//day
else if ($x == 1)
$data .= '<h4>Tommorrow</h4>';//day
else
$data .= '<h4>' . $weather_forecast[$x]['attribs']['']['day'] . '</h4>';//day
$data .= '<img src="http://l.yimg.com/us.yimg.com/i/us/we/52/'.$weather_forecast[$x]['attribs']['']['code'].'.gif" alt="'.$weather_forecast[$x]['attribs']['']['text'].'" />';//icon
$data .= "<br />".$weather_forecast[$x]['attribs']['']['text'];//weather
$data .= "<br />".$weather_forecast[$x]['attribs']['']['high'].'&deg; / '.$weather_forecast[$x]['attribs']['']['low'].'&deg;';//high - low
}
//OUTPUT OTHER STUFF
$data .= "$location_city, $location_region";
$data .= "<br /><br />sunrise: $astronomy_sunrise<br />sunset: $astronomy_sunset";
if ($wind_chill < 60)
$data .= "<br />wind chill: $wind_chill";
if ($wind_speed != 0)
$data .= "<br/>wind: $wind_speed $units_speed $wind_direction_converted";
if ($atmosphere_visibility != 0)
$atmosphere_visibility = ($atmosphere_visibility / 100);
$data .= "<br />visibility: $atmosphere_visibility $units_distance";
$data .= "<br />barometer: $atmosphere_pressure $units_pressure and ";
//steady (0), rising (1), or falling (2)
if ($atmosphere_rising == 0)
$data .= 'steady';
else if ($atmosphere_rising == 1)
$data .= 'rising';
else
$data .= 'falling';
$data .= "<br />humidity: $atmosphere_humidity%";
$data .= '<br />'.$item->get_date('l, F j, Y g:i a');
$data .= '<br /><br />'.$condition_date;
$data .= '<br /><a href="' . $feed->get_link() .'">';
$data .= $feed->get_title();
$data .= '</a>';

Fixed the wind detection code

Tags: , , ,

Comments -49-0 of 5 to “Get Yahoo Weather with SimplePie”

  1. -49 · Ryan Parman says:

    This is Ryan from the SimplePie project. This is a very cool tutorial, and it has inspired me to write up a SimplePie Add-on that greatly simplifies the task of working with Yahoo! Weather data.

    I used the code that calculates wind direction from this script, and included the URL back to this page. I hope you don’t mind.

    Anyways, I hope this makes it easier for people to work with this kind of data!

    http://simplepie.org/wiki/addons/yahoo_weather

  2. -48 · blogger says:

    Cool! Thanks for creating SimplePie, it’s been fun mashing up feeds. 🙂

  3. -47 · JOnes says:

    Why do they have to be such dicks about what sites you can use it on? Weather channel can suck my ass

  4. -46 · joe says:

    Your direction parsing needs to be <= istead of 348.75 || $wind_direction 11.25 && $wind_direction < 33.75)
    $wind_direction_converted = “NNE”;//NNE

    in this example if direction were 11.25 the result would be ‘?’

  5. -45 · blogger says:

    Good point joe, all the if/elses for wind direction should have a equality test. I’ve now updated the code to reflect that.

Leave a Comment

Comments are reviewed before publishing to prevent spam.