Google 天气报告计算出现问题

发布于 2024-09-06 09:03:33 字数 608 浏览 3 评论 0原文

这是关于 Google 天气报告 XML

Google 到底在打什么算盘?

当我使用 weather=London,UK 关键字进行谷歌搜索时,它显示的内容类似于此屏幕截图,

替代文本 http://img293.imageshack.us/img293/4746/weathergoogle.jpg

在我的 XML 中,没有像 26、11、26、22 这样的内容,

我的 XML 如下所示。

替代文本

天气预报中涉及什么计算?

如何将这些放入 PHP 变量中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

当爱已成负担 2024-09-13 09:03:33

没有计算。您链接的 XML 具有以下节点:

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

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

两个是当前天气的摄氏温度和华氏温度,对应于 Google Seach 页面的左侧部分。高低是 Google 搜索页面显示的预测内容(仅限摄氏度)。

PHP 提供了许多用于处理 XML 的库。最突出的是 DOMXMLReaderSimpleXML。查看 PHP 手册中的示例了解如何使用它们。 Stack Overflow 上还有许多有关其使用的问题和解答。


更新后编辑:似乎谷歌会根据请求提要的浏览器中设置的语言为您提供华氏度的高/低值。将语言参数 hl=[languagecode] 添加到 URL,看看您是否可以用摄氏度请求此参数,或者 - 如果不可能 - 手动转换高/低

            from Fahrenheit               to Fahrenheit
Celsius     [°C] = ([°F] − 32) × 5⁄9      [°F] = [°C] × 9⁄5 + 32

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
疾风者 2024-09-13 09:03:33

实际上,只需在 URL 末尾添加 &hl=en-GB 即可
http://www.google.com/ig/api ?weather=伦敦,英国&hl=en-GB
应该有效。
对于其他语言,存在解析问题(与 UTF-8 相关),但对于英语要转为摄氏度,您只需切换到 GB 区域设置(默认设置为 US)。

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

上课铃就是安魂曲 2024-09-13 09:03:33
    <?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'])); ?>° 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'])); ?>° C - <?php echo round(conver_f_c($forecast->high['data'])); ?>° C,
                    <?php echo $forecast->condition['data'] ?>
                </span>
            </div>
        <?php endforeach ?>
    </body>
</html>

<?php
function conver_f_c($F){

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

这段代码解决了我的问题,带有图像的 Google 天气 API,PHP。

    <?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'])); ?>° 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'])); ?>° C - <?php echo round(conver_f_c($forecast->high['data'])); ?>° 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.

冷…雨湿花 2024-09-13 09:03:33

正如 AR 所解释的,返回摄氏度值实际上就像传递语言代码一样简单。

Google 天气 API 没有正式记录,但我发现这两个链接很有用:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文