PHP读取谷歌天气API的XML方法 [ 技术分享 ]
<?php
$content = file_get_contents("http://www.google.com/ig/api?weather=nanjing&hl=zh-cn");
$content || die("No such city's data");
$content = mb_convert_encoding($content, 'UTF-8','GBK');//由于谷歌的xml头部没有标注编码所以要进行转换
$xml = simplexml_load_string($content);
//
$date = $xml->weather->forecast_information->forecast_date->attributes();
$html = $date. "<br>\r\n";
$current = $xml->weather->current_conditions;
$condition = $current->condition->attributes();
$temp_c = $current->temp_c->attributes();
$humidity = $current->humidity->attributes();
$icon = $current->icon->attributes();
$wind = $current->wind_condition->attributes();
$condition && $condition = $xml->weather->forecast_conditions->condition->attributes();
$icon && $icon = $xml->weather->forecast_conditions->icon->attributes();
$html.= "当前: {$condition}, {$temp_c}°C,<img src='http://www.google.com/ig{$icon}'/> {$humidity} {$wind} <br />\r\n";
foreach($xml->weather->forecast_conditions as $forecast) {
$low = $forecast->low->attributes();
$high = $forecast->high->attributes();
$icon = $forecast->icon->attributes();
$condition = $forecast->condition->attributes();
$day_of_week = $forecast->day_of_week->attributes();
$html.= "{$day_of_week} : {$high} / {$low} °C, {$condition} <img src='http://www.google.com/ig{$icon}' /><br />\r\n";
}
echo $html;
?>
共 12 条回复
-
yk19880829 回复于 2012-08-03 18:29 举报
要加精 确实不错!:loveliness:
-
ck86631570 回复于 2012-09-12 09:50 举报
我顶顶:)
-
world_voice 回复于 2012-09-26 14:08 举报
好东西!谢谢楼主分享!
落叶 江苏南京
注册时间:2011-03-29
最后登录:1970-01-01
在线时长:0小时0分
最后登录:1970-01-01
在线时长:0小时0分
- 粉丝8
- 金钱1190
- 威望10
- 积分1290