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)
- 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 )
15 Responses to “Using the Yahoo Weather API (in your WordPress themes)” Leave a reply ›
Nice tutorial. Your code is minimalist and optimized. It can be easily integrated into any website with PHP. Thank you very much for sharing.
How would I implement this on a normal website? I'm new to rss/php so any help would be greatly appreciated!
You'd need to add some additional code for reading the content from another website. This functionality is built into WordPress but it shouldn't be hard to do. Perhaps I could do another post with info on doing this for a standalone site.
getting all those things by wordpress isn't very cool is it?
I'm starting to feel much more for a combination of wordpress and jquery....
So why nog request the weather by jsonp??
http://www.ibm.com/...eb/library/wa-aj-jsonp2/index.html
This might be cool....
Sorry my reply before doesn't tell the real story.
you could also use the following link.
http://developer.yahoo.com/...bles.org/alltableswithkeys
This is a xml data store. You can also use jsonp. So this uses the clients ip adress.
I will try to create some sort of wordpress plugin in the near future.
I have been developing for iPhone weather apps and have been using this free weather API (http://www.worldweatheronline.com/weather-api.aspx) in my commercial apps. It supports JSON and JSON-P and has quite extended global weather forecast.
Can you tell me where to add the above codes? I know the top code goes in wordpress functions.php but the other two sets of codes..where do I add them? Why not explain where to add all codes to help? Thanks....
Hi Ed - the top two functions goes in your themes functions.php. The other bit of code can go inside your theme and be used in any way you see fit.
All you need to do is use $weather = bm_getWeather('IDXX0022', 'c'); in your theme to get the data and then print the properties you want to display. Eg
The temperature is <?php echo $weather ['temperature']; ?>Where do I add this
$weather = bm_getWeather('IDXX0022', 'c');1. in my functions.php or the theme file I want to use it...trying to getting this in my header.php...I add that top code into functions but still a bit confused...thanks for the help...I'm new to wordpress...you add it to the theme in whatever place you want to access the info. It's entirely up to you.
Thanks...I did get it working!
In case your new to wordpress:
1. Add the following code to functions.php file:
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 = ''; preg_match($regex, $data, $matches); return $matches[1]; }2. Add the following in which theme file you want to show the weather:
(ie. Header.php)
Demo Click Here:
http://www.wolverinesocceracademy.com
Thanks Ben! Keep up the great work with the site...
sorry Ben I will try again...not sure why the code did not display....
Not sure if the Yahoo API got changed or if I didn't do something right, but I just wanted to drop a comment incase someone else has this issue.
The parameter used to request a locations weather is 'w' not 'p'.
So, http://weather.yahooapis.com/forecastrss?p=' . $code . '&u=' . $temp;
Should be http://weather.yahooapis.com/forecastrss?w=' . $code . '&u=' . $temp;
Hope that helps.