免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2547 | 回复: 0
打印 上一主题 下一主题

用php Simplexml 调用google天气预报api [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-09 04:09 |只看该作者 |倒序浏览

                ?php
// Charset: utf-8
/**
* 用php Simplexml 调用google天气预报api,和g官方的例子不一样
* google 官方php domxml 获取google天气预报的例子
* http://www.google.com/tools/toolbar/buttons/intl/zh-CN/apis/howto_guide.html
*
* @copyright  Copyright (c) 2008
* @license    New BSD License
* @version    2008-11-9
*/
// 城市,用城市拼音
$city = empty($_GET['city']) ? 'Nanning' : $_GET['city'];
$content = file_get_contents("http://www.google.com/ig/api?weather=$city&hl=zh-cn");
$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;
?>
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/27731/showart_1388428.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP