首页 > php教程 > PHP源码 > 正文

借用Google API 写一个查看天气预报的类

PHP中文网
发布: 2016-05-25 17:15:53
原创
1139人浏览过

php代码

课游记AI
课游记AI

AI原生学习产品

课游记AI 168
查看详情 课游记AI
<?
//- Describe: 
//- Author: liuguichun
//- Link: 
//- CreateTime: 2010-6-21
//- UpdateTime: 
//- Package: 
class weather {
 static $url = 'http://www.google.com/ig/api?hl=zh-cn&weather=';
 static $city = 'Beijing'; //默认城市北京
 static $weatherXML = '';
 /**
  * 获得远程xml并缓存到本地
  */
 static public function getXML() {
  header ( 'Content-Type: text/html; charset = utf-8' );
  if (isset ( $_GET ['city'] )) {
   self::$city = empty ( $_GET ['city'] ) ? 'Beijing' : $_GET ['city'];
  }
  $contents = file_get_contents ( self::$url . self::$city ) or die ( '查询出错' );
  self::$weatherXML = date ( "Ymd" ) . '-' . self::$city . '-weather.xml';
  if (is_file ( self::$weatherXML )) {
   $fileTime = filemtime ( self::$weatherXML );
   $stater = time () - $fileTime - 60 * 60 * 2;
   if ($stater < 0) {
    self::cacheXML ( $contents );
   }
   return true;
  }
  self::cacheXML ( $contents );
 }
 /**
  * 解析xml
  */
 static public function analysisXML() {
  if (is_file ( self::$weatherXML )) {
   $xml = simplexml_load_file ( self::$weatherXML );
  } else {
   $xml = simplexml_load_file ( self::$url . self::$city );
  }
  $xml = ( array ) $xml;
  $city = ( array ) $xml ['weather']->forecast_information->city;
  if (isset ( $xml ['weather']->problem_cause )) {
   $problem = ( array ) $xml ['weather']->problem_cause;
   echo $problem ['@attributes'] ['data'];
   return;
  }
  
  $conditions = ( array ) $xml ['weather']->current_conditions->condition;
  $humidity = ( array ) $xml ['weather']->current_conditions->humidity;
  $temp_c = ( array ) $xml ['weather']->current_conditions->temp_c;
  $conditions_icon = ( array ) $xml ['weather']->current_conditions->icon;
  $wind_condition = ( array ) $xml ['weather']->current_conditions->wind_condition;
  $forecast = ( array ) $xml ['weather'];
  $forecast = ( array ) $forecast ['forecast_conditions'];
  $html = '';
  foreach ( $forecast as $key => $val ) {
   
   ${'day_of_week_' . $key} = ( array ) $val->day_of_week;
   ${'low_' . $key} = ( array ) $val->low;
   ${'high_' . $key} = ( array ) $val->high;
   ${'icon_' . $key} = ( array ) $val->icon;
   ${'condition_' . $key} = ( array ) $val->condition;
   $html .= "

{${'day_of_week_'.$key}['@attributes']['data']}        http://www.google.com{${'icon_'.$key}['@attributes']['data']}\ " width=40 height=40>

        {${'low_'.$key}['@attributes']['data']}°C | {${'high_'.$key}['@attributes']['data']}°C
";
  
  }
  self::printCss ();
  echo <<   

  
{$city['@attributes']['data']}  

    

      
http://www.google.com{$conditions_icon['@attributes']['data' ]}" width=40 height=40>

      

      
{$temp_c['@attributes']['data']}°C

      
当前: {$conditions['@attributes']['data']}

        {$wind_condition['@attributes']['data']}

        {$humidity['@attributes']['data']}

    

    

 $html
    

  



weather;
 
 }
 /**
  * 打印样式
  */
 static public function printCss() {
  echo << 

css;
 }
 /**
  * 创建xml缓存
  * @param $contents 要缓存的内容
  */
 static private function cacheXML($contents) {
  $contents = str_ireplace ( '', " \n", $contents );
  $contents = mb_convert_encoding ( $contents, 'utf-8', 'gbk' );
  file_put_contents ( self::$weatherXML, $contents ) or die ( '没有写权限' );
 }
}
weather::getXML ();
weather::analysisXML ();
?>
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号