全部博文(42)
分类:
2008-11-09 04:09:26
<?php
// Charset: utf-8
/**
* 用php Simplexml 调用google天气预报api,和g官方的例子不一样
* google 官方php domxml 获取google天气预报的例子
*
*
* @copyright Copyright (c) 2008
* @license New BSD License
* @version 2008-11-9
*/
// 城市,用城市拼音
$city = empty($_GET['city']) ? 'Nanning' : $_GET['city'];
$content = file_get_contents("");
$content || die("No such city's data");
$content = mb_convert_encoding($content, 'UTF-8', 'GBK');
$xml = simplexml_load_string($content);
$date = $xml->weather->forecast_information->forecast_date->attributes();
$html = $date. "
\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, {$humidity} {$wind}
\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}
\r\n";
}
header('Content-type: text/html; Charset: utf-8');
print $html;
?>