When developing our Hyperlocal WordPress theme called The Local Darren came up with the idea of adding the local weather to the themes header. After a bit of research I decided that the Yahoo! Weather api was the best solution.
Before writing the code I Googled around to find something that would speed up development but all the solutions I found were overly complicated and cumbersome. I wanted some small fast code – so decided to write my own.
The process for using the Yahoo Weather API is relatively simple
- Work out what you want to request.
- Request it from the Yahoo servers (grab content from another site using WP_HTTP).
- Parse the text that is sent back and display the relevant parts.
The code below is a slightly simplified version of what I used in my WordPress theme, but there’s no reason it couldn’t be modified to work stand-alone as well – it would only need a few small changes to remove the WordPress dependencies.
<?php
function bm_getWeather ($code = '', $temp = 'c') {
$file = 'http://weather.yahooapis.com/forecastrss?p=' . $code . '&u=' . $temp;
$request = new WP_Http;
$result = $request->request($file);
if (isset($result->errors)) {
return FALSE;
}
$data = $result['body'];
$output = array (
'temperature' => bm_getWeatherProperties('temp', $data),
'weather' => bm_getWeatherProperties('text', $data),
'weather_code' => bm_getWeatherProperties('code', $data),
'class' => 'weatherIcon-' . bm_getWeatherProperties('code', $data),
);
return $output;
}
function bm_getWeatherProperties ($needle, $data) {
$regex = '<yweather:condition.*' . $needle . '="(.*?)".*/>';
preg_match($regex, $data, $matches);
return $matches[1];
}
One thing to note, that is not in the code above, is that Yahoo! places an API usage limit on the requests by IP address, so to be able to get consistent usage you need to add some sort of data storage, to keep the information safe for X seconds and reduce the requests to their servers. I have a series of posts on WordPress caching planned so I will save these posts for another time.
Break it Down
The bm_getWeather function grabs the content as text from the Yahoo website (using the Http get stuff I mentioned recently) and then reads through the results grabbing the bits it is most interested in. The bm_getWeatherProperties function is a simple reusable way to read the data I need from the text file that was downloaded. It uses a regular expression to grab the meta data Yahoo! are returning and then inserts it into an array.
Using it
Usage is simple, pass the ‘code’ and the ‘temperature’ to the bm_getWeather function, and then get an array of info back to use and display however you fancy. The code should be the location code as used by Yahoo! to specify where you are based. The temperature (temp) should be either ‘c’ or ‘f’ based upon what temperature measurement you want the weather returned in.
To get the location code you should search for your location on Yahoo! weather and then area code from the resultant URL. For example the url: http://weather.yahoo.com/forecast/UKXX0085.html would give you the code UKXX0085 (which is actually the code for London, England).
A simple request would look like this:
$weather = bm_getWeather('IDXX0022', 'c');
print_r($weather);
and give results like this:
Array (
[temperature] => 28
[weather] => Partly Cloudy
[weather_code] => 30
[class] => weatherIcon-30
)
Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).
Link to this page
Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.