借用Google API 写一个查看天气预报的类
- <?PHP
- //- 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 = emptyempty ( $_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 .= "<DIV class=w_fc title={${'condition_'.$key}['@attributes']['data']}>{${'day_of_week_'.$key}['@attributes']['data']}<BR>
- <IMG class=w_fci alt={${'condition_'.$key}['@attributes']['data']} src=\"http://www.google.com{${'icon_'.$key}['@attributes']['data']}\ " width=40 height=40><BR>
- <NOBR>{${'low_'.$key}['@attributes']['data']}°C | {${'high_'.$key}['@attributes']['data']}°C</NOBR></DIV>";
- }
- self::printCss ();
- echo <<<weather
- <DIV>
- <DIV style="ADDING-TOP: 2px" class=modlabel><A style="COLOR: #00c; FONT-WEIGHT: bold" class=w_lnk href="#">{$city['@attributes']['data']}</A></DIV>
- <DIV class=w_box>
- <DIV class=w_ccs>
- <DIV class=w_ccis><IMG id=w_4_c0_icon class=w_cci alt="{$city['@attributes']['data']}({$conditions['@attributes']['data']})" src="http://www.google.com{$conditions_icon['@attributes']['data' ]}" width=40 height=40><BR>
- </DIV>
- <DIV id=w_4_c0_temp class=w_cc_temp>{$temp_c['@attributes']['data']}°C</DIV>
- <DIV id=w_4_c0_text class=w_cc_text>当前: {$conditions['@attributes']['data']}< BR>
- {$wind_condition['@attributes']['data']}<BR>
- {$humidity['@attributes']['data']}</DIV>
- </DIV>
- <DIV id=w_4_c0_fcs class=w_fcs>
- $html
- </DIV>
- </DIV>
- </DIV>
- weather;
- }
- /**
- * 打印样式
- */
- static public function printCss() {
- echo <<<css
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <style type="text/css">
- td, div, span, p { font-family:arial, sans-serif; font-size:10pt; margin:0}
- .w_fci {BORDER-BOTTOM: #bbc 1px solid;BORDER-LEFT: #bbc 1px solidADDING-BOTTOM: 1pxADDING-LEFT: 1px;WIDTH: 40pxADDING-RIGHT: 1px; HEIGHT: 40px;BORDER-TOP: #bbc 1px solid;BORDER-RIGHT: #bbc 1px solidADDING-TOP: 1px}
- .w_fc { TEXT-ALIGN: center; PADDING-BOTTOM: 0pxADDING-LEFT: 0pxADDING-RIGHT: 10px;FLOAT: left;HEIGHT: 80pxADDING-TOP: 0px}
- .w_fcs {PADDING-BOTTOM: 0pxADDING-LEFT: 0pxADDING-RIGHT: 0px;OVERFLOW: hiddenADDING-TOP: 2px}
- .w_box {PADDING-BOTTOM: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 1px}
- .w_ccs {PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; PADDING-TOP: 2px}
- .w_ccis {PADDING-BOTTOM: 1px; PADDING-LEFT: 1px; PADDING-RIGHT: 1px; FLOAT: left; PADDING-TOP: 1px}
- .w_cci {BORDER-BOTTOM: #bbc 1px solid; BORDER-LEFT: #bbc 1px solid; PADDING-BOTTOM: 1px; PADDING-LEFT: 1px; WIDTH: 40px; PADDING-RIGHT: 1px; HEIGHT: 40px; BORDER-TOP: #bbc 1px solid; BORDER-RIGHT: #bbc 1px solid; PADDING-TOP: 1px}
- .w_cc_temp {PADDING-BOTTOM: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 10px; FLOAT: left; FONT-SIZE: 180%; PADDING-TOP: 0px}
- .w_cc_text {HEIGHT: 60px; OVERFLOW: hidden}
- .w_lnk {COLOR: #00c}
- .modlabel { padding-bottom:2px; padding-top:5px;}
- </style>
- css;
- }
- /**
- * 创建xml缓存
- * @param $contents 要缓存的内容
- */
- static private function cacheXML($contents) {
- $contents = str_ireplace ( '<?xml version="1.0"?>', "<?xml version=\"1.0\"?> \n", $contents );
- $contents = mb_convert_encoding ( $contents, 'utf-8', 'gbk' );
- file_put_contents ( self:weatherXML, $contents ) or die ( '没有写权限' );
- }
- }
- weather::getXML ();
- weather::analysisXML ();