Google Weather API - 解析和修改数据
这个问题不再是最新的 - Google 在 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'] ?>° 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'] ?>° F - <?= $forecast->high['data'] ?>° 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 编码属性错误的问题。
我不知道如何获取准确的克罗地亚语文本并将华氏度转换为摄氏度。
后来我发现了一个指向F-to-C解决方案的链接并更改了第 19 行:
temp_f['data'] ?>° F,
至
temp_c['data'] ?>° C、
(我在当前版本中没有使用它,因为 API 似乎可以处理摄氏温度。)
要在将语言设置为“en”的同时保留 C 的度数,您可以使用 < code>en-gb。
This question is no longer up-to-date -- Google shut down the unofficial weather API in 2012
I'd like to put some weather forecast to a friend's web page.
When I address for
http://www.google.com/ig/api?weather=koprivnica,croatia&hl=hr
The browser returns the right content I'd like to parse to PHP with this code:
<?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'] ?>° 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'] ?>° F - <?= $forecast->high['data'] ?>° F,
<?= $forecast->condition['data'] ?>
</span>
</div>
<?php endforeach ?>
</body>
</html>
But the code above won't work because I used 'hr' instead of 'en' (hr = Croatian language):
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=koprivnica,croatia&hl=en')
is the working syntax but the returned data are in English and the temperature is in Fahrenheit.
I suppose it's matter of a wrong UTF-8 encoding property.
I do not know how to grab the exact Croatian text and convert degrees F into Celsius.
I found afterwards a link to the F-to-C solution and changed line 19:
<?= $current[0]->temp_f['data'] ?>° F,
to
<?= $current[0]->temp_c['data'] ?>° C,
(I do not use it in the current version because it seems the API handles Celsius.)
To keep the degrees in C while having language set to "en" at the same time you can use
en-gb
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编码问题:
由于某种原因,Google 返回的 XML 内容没有正确的编码声明。人们会期望类似的内容:
但他们跳过标头中的编码属性。这使得
simplexml_load_file
函数采用默认编码 UTF-8。我认为这是他们的 API 实现中的一个错误,因为 XML 规范定义了 UTF- 8 作为后备默认编码。为了弥补这一点,请尝试以下方法:
这似乎有效。 ISO-8859-2 值纯粹是猜测。
华氏度/摄氏度:
我没有找到一种简单的方法来请求在此 API 中以摄氏度而不是华氏度提供温度数据(我找不到官方文档,我是瞎子吗?) 。然而,从 F 到 C 的转换应该不难。
试试这个公式:
你可以在数千个地方找到它。我从 http://www.manuelsweb.com/temp.htm 获取它
Encoding problem:
For some reason Google returns the XML content without proper encoding declaration. One would expect something like:
But they skip the encoding attribute in the header. This makes the
simplexml_load_file
function assume the default encoding of UTF-8. I would consider this a bug in their API implementation, since the XML spec defines UTF-8 as the fallback default encoding.To compesate for this, try something like:
Which seems to work. The ISO-8859-2 value was a pure guess.
Fahrenheit/Celsius:
I don't see an easy way to request the temperature data to be provided in Celsius instead of Fahrenheit in this API (I couldn't find the official doc, am I blind?). However conversion from F to C shouldn't be hard at all.
Try this formula:
which you can find in thousand of places. I took it from http://www.manuelsweb.com/temp.htm
google xml 确实返回摄氏温度,并在 current_conditons 中查找 temp_c 标签
The google xml does return the temperature in Celsius as well look for a temp_c tag inside current_conditons