开发者

Problem on Google weather report calcuation

This is about the Google Weather Report XML.

What is the calculation Google is doing?

When I google with weather=London,UK k开发者_运维知识库eyword, 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.

Problem on Google weather report calcuation

What is the calculation involved in the weather report?

How to get these into PHP variable?


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


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).


    <?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.


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:

  • http://code.google.com/p/python-weather-api/#Google_Weather
  • http://dennisdel.com/content/conditions.xml (view source to see list of weather return types)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜