Using the Yahoo Weather API (in your WordPress themes)

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

  1. Work out what you want to request.
  2. Request it from the Yahoo servers (grab content from another site using WP_HTTP).
  3. 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.

WordPress News

The latest WordPress updates from the WPBriefs Podcast.

Related Posts

29 Mar 2008

In the workplace

37Signals have recently posted a couple of articles on improving the workplace experience and they’re doing things I can only dream of. The posts in questions are workplace experiments, and fire the workaholics.Workplace experimentsSome of their ideas are great. I...
26 Mar 2021

Creating a Twitter Maze Bot

I recently had the idea to make a Twitter Emoji Maze bot, so I tweeted my idea and it was suggested I write a tutorial about how I made it. So I’m going to try to write the article as...
09 Feb 2017

How to Hide the Archive Title Prefix in WordPress

I don’t remember exactly when it happened but about a year ago WordPress introduced a prefix at the front of WordPress archive titles. This prefix is designed to give context to archives. For example it helps to distinguish between tag...
23 Jan 2011

How to Contribute to TimThumb

The Google Code blog has recently announced a new method for modifying code committed to the site. They have created an online code editor that can be used by anyone.This is fantastic for people who want to contribute to any...
16 Apr 2010

Using cURL to Read the Contents of a Web Page

Recently I wrote about how to use the Yahoo! weather api with WordPress and in the comments I was asked how to use it without relying on WordPress. The answer – is cURL.In this post I’m going to show you...
26 Sep 2018

WordPress get_post_gallery() Gutenberg Polyfill

I’m working on a new WordPress theme designed for Gutenberg, however Gutenberg isn’t finished, and there are elements that won’t work until the plugin is merged with core.One such issue, that I ran into this weekend, is that get_post_gallery() doesn’t...