php - Google Weather API - parsing and modifying data -
this question no longer up-to-date -- google shut down unofficial weather api in 2012
i'd put weather forecast friend's web page. when address for
http://www.google.com/ig/api?weather=koprivnica,croatia&hl=hr
the browser returns right content i'd parse php code:
<?php $xml = simplexml_load_file('http://www.google.com/ig/api?weather=koprivnica,croatia&hl=hr'); $information = $xml->xpath("/xml_api_reply/weather/forecast_information"); $current = $xml->xpath("/xml_api_reply/weather/current_conditions"); $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions"); ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>vremenska prognoza</title> </head> <body> <h1><?= print $information[0]->city['data']; ?></h1> <h2>danas</h2> <div class="weather"> <img src="<?= 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"?> <span class="condition"> <?= $current[0]->temp_f['data'] ?>° f, <?= $current[0]->condition['data'] ?> </span> </div> <h2>prognoza</h2> <?php foreach ($forecast_list $forecast) : ?> <div class="weather"> <img src="<?= 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?> <div><?= $forecast->day_of_week['data']; ?></div> <span class="condition"> <?= $forecast->low['data'] ?>° f - <?= $forecast->high['data'] ?>° f, <?= $forecast->condition['data'] ?> </span> </div> <?php endforeach ?> </body> </html>
but code above won't work because used 'hr' instead of 'en' (hr = croatian language):
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=koprivnica,croatia&hl=en')
is working syntax returned data in english , temperature in fahrenheit.
i suppose it's matter of wrong utf-8 encoding property.
i not know how grab exact croatian text , convert degrees f celsius.
i found afterwards link f-to-c solution , changed line 19:
<?= $current[0]->temp_f['data'] ?>° f,
to
<?= $current[0]->temp_c['data'] ?>° c,
(i not use in current version because seems api handles celsius.)
to keep degrees in c while having language set "en" @ same time can use
en-gb
.
encoding problem:
for reason google returns xml content without proper encoding declaration. 1 expect like:
<?xml version='1.0' encoding='iso-8859-2'?>
but skip encoding attribute in header. makes simplexml_load_file
function assume default encoding of utf-8. consider bug in api implementation, since xml spec defines utf-8 fallback default encoding.
to compesate this, try like:
<?php $url = "http://www.google.com/ig/api?weather=koprivnica,croatia&hl=hr"; $datainiso = file_get_contents($url); $datainutf = mb_convert_encoding($datainiso, "utf-8", "iso-8859-2"); $xml = simplexml_load_string($datainutf); ...
which seems work. iso-8859-2 value pure guess.
fahrenheit/celsius:
i don't see easy way request temperature data provided in celsius instead of fahrenheit in api (i couldn't find official doc, blind?). conversion f c shouldn't hard @ all.
try formula:
(°f - 32) x 5/9 = °c
which can find in thousand of places. took http://www.manuelsweb.com/temp.htm
Comments
Post a Comment