I'm attempting to retrieve a local weather forecast, based on the IP of the user.
I'm using geoplugin.net to get the user location and feed the city and country name to the Google Weather API.
//Get user IP
$ip = $_SERVER['REMOTE_ADDR'];
$geolocation = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip));
$geo_city = $geolocation['geoplugin_city'];
$geo_country = $geolocation['geoplugin_countryName'];
$file = "http://www.google.com/ig/api?weather=".$geo_city.",".$geo_country;
$xml = simplexml_load_file($file);
//Echo content of retrieved XML for debugging purposes
echo "<pre>";
print_r($xml);
echo "</pre>";
It works well for most cases, but when I try it on my own IP, I get Søborg, Denmark (which is not 100% accurate, but close enough) and that gives me an almost empty response from the weather API.
The main suspect in the case, is the dastardly "ø"-character.
The XML that I want can be seen here: http://www.google.com/ig/api?weather=S%C3%B8borg,Denmark
The XML that I'm getting can be seen here: http://www.google.com/ig/api?weather=S
When I type this URL into the browser it works fine:
http://www.google.com/ig/api?weather=Søborg,Denmark
When I use this version it works as well (in the browser):
http://www.google.com/ig/api?weather=S%C3%B8borg,Denmark
but this version returns the forecast for Borg,Syddanmark:
http://www.google.com/ig/api?weather=S%26oslash%3Bborg,Denmark
None of the above returns the desired result, when fed to the simplexml_load_file().
As stated, I suspect that it is a character set issue, but I can't figure out what to do about it.
What is the correct way to solve it?
I know that I can use latitude and longtitude as parameters for Google Weather API instead, but that's just circumventing the problem, not solving it.