This article was published more than a year ago, there may have been developments.
Please take this into account.

Let's see now how to show on our site weather conditions, and possibly weather forecast using API di Google and PHP. This technique can be pointless most of the time if you handle for example a personal website, but it is certainly useful, for example for institutional sites, or tourist, or still for Hotels, i camping e the tourist facilities in bother.

Some may argue that services of this kind there are many others, that free make available to the code (often using the tag <iframe>) to include in the page. In our case, however, the result will be a 'seamless integration in the page of our website, the navigator will not be sent back to Google servers, and will have the perception that the information resides on our server. If nothing else will comply with W3C standards.

The lines of code are very few.

To display the 'XML provided by Google you need to go on this page: / ig / api?weather=[city]

For output I taken as an example Merate, a small town in Brianza.

<xml_api_reply version="1">
    <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
            <city data="Merate, Lombardy"/>
            <postal_code data="merate"/>
            <latitude_e6 data=""/>
            <longitude_e6 data=""/>
            <forecast_date data="2010-01-05"/>
            <current_date_time data="2010-01-05 19:09:49 +0000"/>
            <unit_system data="SI"/>
            <condition data="Clear"/>
            <temp_f data="36"/>
            <temp_c data="2"/>
            <humidity data="Humidity: 70%"/>
            <icon data="/ig/images/weather/cloudy.gif"/>
            <wind_condition data="Wind: N a 5 km/h"/>
            <day_of_week data="sea"/>
            <low data="-2"/>
            <high data="3"/>
            <icon data="/ig/images/weather/snow.gif"/>
            <condition data="Snow Showers"/>
            <day_of_week data="more"/>
            <low data="0"/>
            <high data="6"/>
            <icon data="/ig/images/weather/partly_cloudy.gif"/>
            <condition data="Partly Cloudy"/>
            <day_of_week data="gio"/>
            <low data="0"/>
            <high data="4"/>
            <icon data="/ig/images/weather/rain_snow.gif"/>
            <condition data="Rain and snow"/>
            <day_of_week data="see"/>
            <low data="-1"/>
            <high data="4"/>
            <icon data="/ig/images/weather/rain_snow.gif"/>
            <condition data="Rain and snow"/>

Now let's see how to format this code and "pigeonhole"With PHP:

$xml = simplexml_load_file(' / ig / api?weather=bari');
$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");
        <title>Google Weather API</title>
        <h1><?= print $information[0]->city['data']; ?></h1>
        <h2>Today's weather</h2>
        <div class="weather">
            <img src="<?= ' ' . $current[0]->icon['data']?>" alt ="weather"?>
            <span class="condition">
            <?= $current[0]->temp_f['data'] ?>° F,
            <?= $current[0]->condition['data'] ?>
        <? foreach ($forecast_list as $forecast) : ?>
        <div class="weather">
            <img src="<?= ' ' . $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'] ?>
        <? endforeach ?>

Of course everything will be inserted into the layout of the site that we need to modify, but these are the basic settings.

  Working with Google docs, as if we were working in local


I keep this blog as a hobby by 2009. I am passionate about graphic, technology, software Open Source. Among my articles will be easy to find music, and some personal thoughts, but I prefer the direct line of the blog mainly to technology. For more information contact me.


Wire · 29 February 2016 at 6:42 PM

I have news that the Google API for the weather have been deprecated, it's true?

    Giovanni Mauri · 5 June 2016 at 2:24 PM

    And’ true. Google has arbitrarily canceled the service without notice, or explanation.
    Are there viable alternatives. One that I would recommend is OpenWeatherMap –
    Thanks for the comment and apologize for the delay.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.