Yahoo Weather API Mashup

Developing With The Yahoo Weather API.

Yahoo Weather API Integration

Here at 18a we love playing with API's. There's no better way to spend an afternoon than tweeking with PHP code and mashing up XML data and web services to create new and wonderful web tools to play with.

The Yahoo weather api is certainly fair from being complicated to integrate, but it was fun to add it to our site, and gives a flavour of what's possible when you put a little creative thinking in the process.

The Brief

We wanted to show a different image on our homepage depending what the current weather was like outside our office here in Bristol. Why you might ask? Well at the time we were experiencing some rather unusual weather here in Bristol and we just felt the urge to share the joys and variety of the English weather with our global audience. Probably not all that interesting you might think, but talking about the weather is a British pasttime, second only to drinking tea... (When we think of a tea app we'll add that too!)

So anyway, our first task was to find a suitable source for our weather data. There are various places online you can get automatic weather reports. The BBC for one, but we couldn't find a suitable API to use... I thought about scraping the BBC website for the data we needed, but sometimes websites don't like that, and it's far from reliable, so it was great to find the Yahoo weather api.

How we did it

The 18a Productions site runs on the CodeIgniter framework. For those that don't know, CodeIgniter is a fantastic application framework, upon which you can quickly and easily build really cool, scalable applications. It's a great place to start pretty much any web project and takes away a lot of the priliminary 'faffing' associated with setting up a new website. Having tried a number of frameworks, including CakePHP and Zend, I've found CodeIgniter to be quick, simple and flexible, and is certainly my favourite.

But anyway, I digress... For anyone interested, here's my CodeIgniter controller function to get the weather:

function _checkWeather(){ $this->load->library('curl'); $url = 'http://weather.yahooapis.com/forecastrss?w=13963&u=c'; $xml = $this->curl->simple_get($url); // SimpleXML seems to have problems with the colon ":" in the response tags $xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xml); if ($xml) { try { $xmlobject = new SimpleXMLElement($xml); if ($xmlobject == false) { return false; } } catch (Exception $e) { return false; } $forecast = array(); foreach ($xmlobject->channel as $channel) { foreach($channel->yweatherlocation->attributes() as $a => $b) { if($a=='city') { $forecast[$a] = (string)$b; } } } foreach ($xmlobject->channel->item as $item) { foreach($item->yweathercondition->attributes() as $a => $b) { $forecast[$a] = (string)$b; } } } return ($forecast); }

This gives you a handy variable, containing a human readable weather forecast for the Bristol area. If you're interested in the weather in other parts of the world, you'll need to grab the relevant feed for your region. The region is determined by the w variable or WOEID in the feed URL. In my case this was 13963 for Bristol, but you can find the right WOEID for you on the Yahoo Weather page, if you're still lost, read the weather API documentation.

So I hope that's interesting to someone... If you have an API you think is cool, send me a link.

Thanks goes to Enthropia Labs for the cool weather icons.