Google Weather API - 解析和修改数据

Rok*_*jan 2 php xml parsing weather

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


我想把一些天气预报放到朋友的网页上.当我致力于

http://www.google.com/ig/api?weather=koprivnica,croatia&hl=hr
Run Code Online (Sandbox Code Playgroud)

浏览器使用以下代码返回我想要解析为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>
Run Code Online (Sandbox Code Playgroud)

但上面的代码不起作用,因为我使用'hr'代替'en'(hr =克罗地亚语):

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

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

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

我不知道如何抓取确切的克罗地亚文本并将F度转换为摄氏度.


  1. 之后我找到了F-to-C解决方案的链接并更改了第19行:

    <?= $current[0]->temp_f['data'] ?>&deg; F,

    <?= $current[0]->temp_c['data'] ?>&deg; C,

    (我不会在当前版本中使用它,因为它似乎API处理Celsius.)

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

Grz*_*zki 6

编码问题:

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

<?xml version='1.0' encoding='ISO-8859-2'?>
Run Code Online (Sandbox Code Playgroud)

但是他们跳过了标题中的编码属性.这使得该simplexml_load_file函数采用UTF-8的默认编码.我认为这是他们的API实现中的一个错误,因为XML规范将UTF-8定义为回退默认编码.

为了证明这一点,请尝试以下方法:

<?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);
...
Run Code Online (Sandbox Code Playgroud)

这似乎有效.ISO-8859-2的值是一个纯粹的猜测.

华氏度/摄氏度:

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

试试这个公式:

(°F  -  32)  x  5/9 = °C
Run Code Online (Sandbox Code Playgroud)

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