【问题标题】:Google Weather API - parsing and modifying dataGoogle Weather API - 解析和修改数据
【发布时间】:2011-07-05 08:04:21
【问题描述】:

这个问题不再是最新的——谷歌在 2012 年关闭了非官方的天气 API


我想在朋友的网页上放一些天气预报。 当我为

http://www.google.com/ig/api?weather=koprivnica,croatia&hl=hr

浏览器返回我想用这段代码解析到 PHP 的正确内容:

<?php
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=koprivnica,croatia&hl=hr');
$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");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>VREMENSKA PROGNOZA</title>
</head>
    <body>
        <h1><?= print $information[0]->city['data']; ?></h1>
        <h2>Danas</h2>
        <div class="weather">
            <img src="<?= 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"?>
            <span class="condition">
            <?= $current[0]->temp_f['data'] ?>&deg; F,
            <?= $current[0]->condition['data'] ?>
            </span>
        </div>
        <h2>Prognoza</h2>
        <?php 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'] ?>&deg; F - <?= $forecast->high['data'] ?>&deg; F,
                <?= $forecast->condition['data'] ?>
            </span>
        </div>
        <?php endforeach ?>
    </body>
</html>

但上面的代码不起作用,因为我使用了“hr”而不是“en”(hr = 克罗地亚语):

$xml = simplexml_load_file('http://www.google.com/ig/api?weather=koprivnica,croatia&hl=en')

是工作语法,但返回的数据是英文,温度是华氏度。

我想这是错误的 UTF-8 编码属性的问题。

我不知道如何获取准确的克罗地亚语文本并将华氏度转换为摄氏度。


  1. 后来我发现了一个指向F-to-C solution 的链接并更改了第 19 行:

    &lt;?= $current[0]-&gt;temp_f['data'] ?&gt;&amp;deg; F,

    &lt;?= $current[0]-&gt;temp_c['data'] ?&gt;&amp;deg; C,

    (我在当前版本中不使用它,因为 API 似乎可以处理摄氏度。)

  2. 要保持 C 中的度数同时将语言设置为“en”,您可以使用 en-gb

【问题讨论】:

  • “行不通”是什么意思?有任何错误信息吗?

标签: php xml parsing weather


【解决方案1】:

编码问题:

由于某种原因,Google 在没有正确编码声明的情况下返回 XML 内容。人们会期待这样的事情:

<?xml version='1.0' encoding='ISO-8859-2'?>

但是他们跳过了标头中的编码属性。这使得simplexml_load_file 函数假定默认编码为UTF-8。我认为这是他们 API 实现中的一个错误,因为 XML spec defines UTF-8 as the fallback default encoding

为了弥补这一点,请尝试以下操作:

<?php
$URL = "http://www.google.com/ig/api?weather=koprivnica,croatia&hl=hr";
$dataInISO = file_get_contents($URL);
$dataInUTF = mb_convert_encoding($dataInISO, "UTF-8", "ISO-8859-2");
$xml = simplexml_load_string($dataInUTF);
...

这似乎有效。 ISO-8859-2 值纯属猜测。

华氏度/摄氏度

在此 API 中,我没有看到一种简单的方法来请求以摄氏温度而不是华氏温度提供温度数据(我找不到官方文档,我是不是瞎了?)。但是从 F 到 C 的转换应该一点也不难。

试试这个公式:

(°F  -  32)  x  5/9 = °C

您可以在数千个地方找到它。我是从http://www.manuelsweb.com/temp.htm获取的

【讨论】:

  • 是的,我试过各种&lt;?xml version='1.0' encoding=' [types] '?&gt; 但是,你的这个脚本! ......我没有别的词:天才!我非常感谢你!现在我只需要在摄氏温度下工作! ;)
【解决方案2】:

google xml 确实以摄氏度返回温度,并在 current_conditons 中查找 temp_c 标记

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多