Weather API update ...

Ok, so most of you will now have heard that the much used and loved unofficial google weather api has closed for good. In my previous post I mentioned this and gave you a couple of alternatives to use instead.

Now here is a follow up ...

On a project we use geolocation and geo-data searches and previous to the Google Weather API closing I was using this to display the current weather and forcast for specific cities. Back then I could simply use the city name and the country and send it off to the API. Unfortunately though, this didn't quite work out with the wunderground api. Whil;est I would work in most cases, certain cities just wouldn't be found, or it would give me the american equivalent, even though I specified the country. Then there was the issue with Umlauten (eg: Zürich -> zurich / München -> munich / etc ...) ...

So I had to rethink my strategy!

Since we hold the geodata info for these places in our database (when we add a city or place we would automatically add the geodata to the node using Google geocoder api).
So, I had both Lat and Lng values to be able to send to the wunderground API and in return get the weather info from the closest weatherstation. Sure, in some cases this would be in a different country for bordering places, but the info was correct.

So I spend a few hours re-working the existing module moving it away from the Google weather api and I must say, so far I have not received any error messages from this. Since the module code is very specific to our site and the info we hold for nodes as well as views pages, it wouldn't make much sense to post it here, however, here a few tips for someone looking to do the same:

1) Wunderground limits the daily calls to 500 -> if you think you will reach this limit, use caching and make the call dependent on the age of the cachefile (for our project, I make a call only every 30 minutes per geo-set)

2) Sometimes wunderground may return an empty .xml or .json document - make sure you add a check before proceeding.

3) if you don't have any geodata use Google Geocoding tool to grab it before sending your request

4) use fallbacks in case something goes wrong. I use drupal_get_request() for example to check the file status before proceeding if the initial call failed for some reason.

eg: here is a snipped of my code:

	// check if the cached file doesn't exist or is older than xxx seconds or is smaller than 1000kb
	// the normal file is usually larger than 1400kb
	// recreate the xml file
	if(  !file_exists($file) || filemtime($file) < $cache_time ) { 
	
		// set options for the get http request
		$request_options = array(
					  'timeout' => 3,
					  'headers' => array(
						'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0)',
						'Referer' => 'http://www.google.com',
					  ),
		);

		// try to grab the file from the url
		$cache_result = drupal_http_request($url, $request_options);

		// if we have errors grabbing the data (eg: 404, 500 etc ..)
		while(!in_array($cache_result->code, array(200, 304))) {

		  // try to grab the file from the url
		  $cache_result = drupal_http_request($url, $request_options);

		}

		$cache_xml = $cache_result->data;

		// save the cache file for php5
		file_put_contents($file, $cache_xml);

		$result = $cache_xml;

these are just some of the things to think about. I hope this helps someone out there =)