Find a city in wunderground when there is more than one entry

Wunderground’s API website seem to cover every single possible thing you might want to do with their service, apart from, inexplicably, actually having a list of cities and corresponding codes for you to look at in order to get the weather as accurately as possible for outlying regions (in other words, to work out what the nearest city to the place you want is that they have data on).

What you have to do is go to their main website and search for your country, it’ll give you a list of names of cities. You can use these and just add %20 instead of spaces, etc, and shove it into the code example they give you, such as :

$json_string = file_get_contents("");
$parsed_json = json_decode($json_string);

But you’ll run into problems when there is more than one entry for the city you’re searching for, for example Pietermaritzberg or Nelspruit in South Africa both (oddly) have two entries – see

As far as I can see there’s nothing mentioned on their API documentation about this. It’d be fine if the json you got given back contained forecasts for both of the locations, but sadly it doesn’t. So what you have to do is cunningly look at the main website’s code, check various links to see if you can work out a possible way to query stuff, google fruitlessly, try random combinations and then (!!) take the zmw code for the city you want out of the array of responses you get, and use it like this:

$json_string = file_get_contents("");
 $parsed_json = json_decode($json_string);

In general their new website, service and api website seem very nice and organised so it’s entirely possible this information is somewhere and I was just blindly not seeing it, but I did look quite a bit and sometimes the most simple obvious things just get overlooked.

4 thoughts on “Find a city in wunderground when there is more than one entry

  1. biorhythm

    Thank you very much for this tip. I’m gonna present some weather information on my webpage but I couldn’t figure out how to make it work for all visitors. With your solution it seems to be pretty simple. You’ve made my day 😉


Leave a Reply

Your email address will not be published. Required fields are marked *