0

This is about the Google Weather Report XML.

What is the calculation Google is doing?

When I google with weather=London,UK keyword, it's showing something like this screen shot,

alt text http://img293.imageshack.us/img293/4746/weathergoogle.jpg

In my XML there is nothing like 26, 11, 26, 22 no

My XML look like below.

alt text

What is the calculation involved in the weather report?

How to get these into PHP variable?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Bharanikumar
  • 25,457
  • 50
  • 131
  • 201

4 Answers4

4

There is no calculation. The XML you linked has these nodes:

<temp_f data="70"/>
<temp_c data="21"/>

and

<low data="11"/>
<high data="26"/>

The first two are the temperature in Celsius and Fahrenheit for the current weather and corresponds to the left hand part on the Google Seach page. High and Low is what the Google Search Page shows for the Forecast (Celsius only).

PHP provides a number of libraries for working with XML. The most prominent being DOM, XMLReader and SimpleXML. Have a look at the examples in the PHP Manual on how to use them. Stack Overflow also has numerous questions and answers regarding their usage.


EDIT after Update: Seems like Google gives you the high/low values in Fahrenheit depending on the language set in the browser requesting the feed. Either add the language param hl=[languagecode] to the URL to see if you can request this in Celsius or - if that's not possible - convert high/low by hand:

            from Fahrenheit               to Fahrenheit
Celsius     [°C] = ([°F] − 32) × 5⁄9      [°F] = [°C] × 9⁄5 + 32
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Should i use or – Bharanikumar Jun 23 '10 at 09:26
  • @Bharanikumar Like explained: temp_f data is fahrenheit already, so you don't need to convert temp_c data. You have to convert low and high. Apparently, Google changes language and temperature units depending on the language. You can force a specific language by adding the additional param `&hl=[countrycode]` to the URL. You might want to consider using the [Yahoo Weather API instead](http://developer.yahoo.com/weather/) instead. – Gordon Jun 23 '10 at 09:29
1

Actually, it's as easy as adding &hl=en-GB to the end of URL http://www.google.com/ig/api?weather=London,UK&hl=en-GB should work. For other languages there are parsing problems (UTF-8 related), but for English to turn to Centigrade you just need to switch to GB locale (US is set by default).

AR.
  • 1,935
  • 1
  • 11
  • 14
0
    <?php
        $xml = simplexml_load_file('http://www.google.com/ig/api?weather=London');
        $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><?php print $information[0]->city['data']; ?></h1>
        <h2>Today's weather</h2>
        <div class="weather">
            <img src="<?php echo  'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"?>
            <span class="condition">
            <?php echo round(conver_f_c($current[0]->temp_f['data'])); ?>&deg; C,
            <?php echo $current[0]->condition['data'] ?>
            </span>
        </div>
        <h2>Forecast</h2>
        <?php foreach ($forecast_list as $forecast) : ?>
            <div class="weather">
                <img src="<?php echo 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?>
                <div><?php echo $forecast->day_of_week['data']; ?></div>
                <span class="condition">
                    <?php echo round(conver_f_c($forecast->low['data'])); ?>&deg; C - <?php echo round(conver_f_c($forecast->high['data'])); ?>&deg; C,
                    <?php echo $forecast->condition['data'] ?>
                </span>
            </div>
        <?php endforeach ?>
    </body>
</html>

<?php
function conver_f_c($F){

    return  $C = ($F - 32) * 5/9;
}

This snippet fixed my problem, Google weather API with image, PHP.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bharanikumar
  • 25,457
  • 50
  • 131
  • 201
0

As AR explains, returning Celsius values really is as easy as passing the language code.

The Google weather API isn't officially documented, but I found these 2 links useful:

Peter O.
  • 32,158
  • 14
  • 82
  • 96
steddy_eddie
  • 151
  • 3