这是关于Google Weather Report XML。
Google正在做什么计算?
当我使用天气=伦敦,英国关键字进行谷歌时,它显示的内容类似于此屏幕截图,
alt text http://img293.imageshack.us/img293/4746/weathergoogle.jpg
在我的XML中,没有像26,11,26,22没有
的东西我的XML如下所示。
alt text http://img171.imageshack.us/img171/7408/weatherxml.jpg
天气预报中涉及的计算是什么?
如何将这些变成PHP变量?
答案 0 :(得分:4)
没有计算。您链接的XML具有以下节点:
<temp_f data="70"/>
<temp_c data="21"/>
和
<low data="11"/>
<high data="26"/>
前两个是当前天气的摄氏温度和华氏温度,对应于Google Seach页面上的左手部分。 “高”和“低”是Google搜索页面针对预测显示的内容(仅限摄氏)。
PHP provides a number of libraries for working with XML。最突出的是DOM,XMLReader和SimpleXML。查看PHP手册中有关如何使用它们的示例。 Stack Overflow还有许多关于其用法的问题和答案。
更新后编辑:似乎Google会根据请求Feed的浏览器中设置的语言为您提供华氏度的高/低值。将语言参数hl=[languagecode]
添加到网址,以查看您是否可以在摄氏度中申请,或者 - 如果不可能 - convert high/low by hand:
from Fahrenheit to Fahrenheit
Celsius [°C] = ([°F] − 32) × 5⁄9 [°F] = [°C] × 9⁄5 + 32
答案 1 :(得分:1)
实际上,它就像在URL的末尾添加&amp; hl = en-GB一样简单 http://www.google.com/ig/api?weather=London,UK&hl=en-GB 应该管用。 对于其他语言,存在解析问题(与UTF-8相关),但是对于英语转为摄氏,您只需切换到GB语言环境(默认设置为US)。
答案 2 :(得分:0)
<?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;
}
这个代码段解决了我的问题,谷歌天气API与图像,PHP。
答案 3 :(得分:0)
正如AR解释的那样,返回Celsius值实际上就像传递语言代码一样简单。
Google天气API尚未正式记录,但我发现这两个链接很有用: