views:

45

answers:

2

Hi,

This is GOOGLE Weather Report XML

What is the calculation google doing ,

When i google with weather=London,UK keyword ,

Its showing somthing like this screen shot alt text

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,

+3  A: 

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 Celcius 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=[countrycode] 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
Gordon
Should i use<temp_f data="70"/><temp_c data="21"/> or <low data="11"/><high data="26"/>
Bharanikumar
Gordon
A: 
    <?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

Bharanikumar