2

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.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
Lowkey
  • 75
  • 2
  • 6

3 Answers3

1

If you URL-decode S%26oslash%3Bborg you'll see that this string corresponds to S&oslash;borg which gives us Søborg after we decode HTML entities like so:

$city = 'S%26oslash%3Bborg,Denmark';
echo $city = rawurldecode($city);
//prints S&oslash;borg,Denmark

echo $city = html_entity_decode($city, 0, 'UTF-8');
//prints Søborg,Denmark

echo $city = rawurlencode($city);
//prints S%C3%B8borg%2CDenmark

And then:

$xml = file_get_contents('http://www.google.com/ig/api?weather='.$city);
$xml = mb_convert_encoding($xml, 'UTF-8');
$xml = simplexml_load_string($xml);
echo $xml->weather->forecast_information->city['data'];

Outputs expected:

Søborg, Capital Region of Denmark
sanmai
  • 29,083
  • 12
  • 64
  • 76
  • Excellent! Can you also tell me what the right way is, to turn "ø" in into %C3%B8? Is there a built in function? – Lowkey Jan 25 '12 at 08:47
  • Indeed it does, but the the string I get from the geoplugin.net xml, seems (according to `mb_detect_encoding()`) to be in ASCII, so when I `rawurlencode()` it, I get `S%26oslash%3Bborg`returned. I've searched for [a way to convert ASCII to UTF-8](http://stackoverflow.com/questions/4983989/convert-ascii-to-utf-8-encoding) and learned that ASCII in fact is a subset of UTF-8. I've tried `$string = iconv('ASCII', 'UTF-8//IGNORE', $geo_city); echo mb_detect_encoding($string);` as suggested in the link and it still returns ASCII. – Lowkey Jan 25 '12 at 11:21
0

It does indeed sound like a character set issue. Have you tried converting the URL to another encoding, e.g. using iconv, before passing the result into simplexml_load_file()?

Nick Shaw
  • 2,083
  • 19
  • 27
  • Not quite sure what I'm converting to and from. I've tried both `iconv("ISO-8859-1", "UTF-8", $file)` and the other way around with no success. `utf8_encode($file)` yields no result either. – Lowkey Jan 20 '12 at 15:19
0

Try this out:

$file = "http://www.google.com/ig/api?weather=" . $geo_city . "," . $geo_country;
$data = file_get_contents($file);
$data = mb_convert_encoding($data, "UTF-8", "ISO-8859-2");

$xml = simplexml_load_string($data);
echo "<pre>"; print_r($xml); echo "</pre>";

It's taken from this maybe similar thread: https://stackoverflow.com/a/5136549/949476

Community
  • 1
  • 1
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • The response is still empty, like it would be if the service was called with this url: [http://www.google.com/ig/api?weather=s](http://www.google.com/ig/api?weather=s). – Lowkey Jan 20 '12 at 15:14
  • So if you just run this code `$data = file_get_contents("http://www.google.com/ig/api?weather=Søborg,Denmark");` response is empty as well? – dfsq Jan 20 '12 at 17:26
  • Yes, empty in the same sense as above. I do get _some_ xml back, but there's no forecast data in it. – Lowkey Jan 20 '12 at 18:11