Cookie

Caution


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:

http://www.google.com / 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">
        <forecast_information>
            <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"/>
        </forecast_information>
        <current_conditions>
            <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"/>
        </current_conditions>
        <forecast_conditions>
            <day_of_week data="sea"/>
            <low data="-2"/>
            <high data="3"/>
            <icon data="/ig/images/weather/snow.gif"/>
            <condition data="Snow Showers"/>
        </forecast_conditions>
        <forecast_conditions>
            <day_of_week data="more"/>
            <low data="0"/>
            <high data="6"/>
            <icon data="/ig/images/weather/partly_cloudy.gif"/>
            <condition data="Partly Cloudy"/>
        </forecast_conditions>
        <forecast_conditions>
            <day_of_week data="gio"/>
            <low data="0"/>
            <high data="4"/>
            <icon data="/ig/images/weather/rain_snow.gif"/>
            <condition data="Rain and snow"/>
        </forecast_conditions>
        <forecast_conditions>
            <day_of_week data="see"/>
            <low data="-1"/>
            <high data="4"/>
            <icon data="/ig/images/weather/rain_snow.gif"/>
            <condition data="Rain and snow"/>
        </forecast_conditions>
    </weather>
</xml_api_reply>

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

<?
$xml = simplexml_load_file('http://www.google.com / 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");
?>
<html>
    <head>
        <title>Google Weather API</title>
    </head>
    <body>
        <h1><?= print $information[0]->city['data']; ?></h1>
        <h2>Today's weather</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>Forecast</h2>
        <? 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'] ?>° F - <?= $forecast->high['data'] ?>° F,
                <?= $forecast->condition['data'] ?>
            </span>
        </div>
        <? endforeach ?>
    </body>
</html>

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

Not enough?


TheJoe

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.

2 Comments

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 – http://openweathermap.org/api
    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.