Google Weather API - parsing and modifying data

2019-02-26 02:07发布

This question is no longer up-to-date -- Google shut down the unofficial weather API in 2012


I'd like to put some weather forecast to a friend's web page. When I address for

http://www.google.com/ig/api?weather=koprivnica,croatia&hl=hr

The browser returns the right content I'd like to parse to PHP with this 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'] ?>&deg; F,
            <?= $current[0]->condition['data'] ?>
            </span>
        </div>
        <h2>Prognoza</h2>
        <?php foreach ($forecast_list as $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'] ?>&deg; F - <?= $forecast->high['data'] ?>&deg; F,
                <?= $forecast->condition['data'] ?>
            </span>
        </div>
        <?php endforeach ?>
    </body>
</html>

But the code above won't work because I used 'hr' instead of 'en' (hr = Croatian language):

$xml = simplexml_load_file('http://www.google.com/ig/api?weather=koprivnica,croatia&hl=en')

is the working syntax but the returned data are in English and the temperature is in Fahrenheit.

I suppose it's matter of a wrong UTF-8 encoding property.

I do not know how to grab the exact Croatian text and convert degrees F into Celsius.


  1. I found afterwards a link to the F-to-C solution and changed line 19:

    <?= $current[0]->temp_f['data'] ?>&deg; F,

    to

    <?= $current[0]->temp_c['data'] ?>&deg; C,

    (I do not use it in the current version because it seems the API handles Celsius.)

  2. To keep the degrees in C while having language set to "en" at the same time you can use en-gb.

2条回答
贪生不怕死
2楼-- · 2019-02-26 02:20

The google xml does return the temperature in Celsius as well look for a temp_c tag inside current_conditons

查看更多
SAY GOODBYE
3楼-- · 2019-02-26 02:21

Encoding problem:

For some reason Google returns the XML content without proper encoding declaration. One would expect something like:

<?xml version='1.0' encoding='ISO-8859-2'?>

But they skip the encoding attribute in the header. This makes the simplexml_load_file function assume the default encoding of UTF-8. I would consider this a bug in their API implementation, since the XML spec defines UTF-8 as the fallback default encoding.

To compesate for this, try something 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 to work. The ISO-8859-2 value was a pure guess.

Fahrenheit/Celsius:

I don't see an easy way to request the temperature data to be provided in Celsius instead of Fahrenheit in this API (I couldn't find the official doc, am I blind?). However conversion from F to C shouldn't be hard at all.

Try this formula:

(°F  -  32)  x  5/9 = °C

which you can find in thousand of places. I took it from http://www.manuelsweb.com/temp.htm

查看更多
登录 后发表回答