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

Php代码
  1. <?PHP   
  2. //- Describe:   
  3. //- Author: liuguichun   
  4. //- Link:   
  5. //- CreateTime: 2010-6-21   
  6. //- UpdateTime:   
  7. //- Package:   
  8. class weather {   
  9. static $url = 'http://www.google.com/ig/api?hl=zh-cn&weather=';   
  10. static $city = 'Beijing'//默认城市北京   
  11. static $weatherXML = '';   
  12. /**  
  13.   * 获得远程xml并缓存到本地  
  14.   */  
  15. static public function getXML() {   
  16.   header ( 'Content-Type: text/html; charset = utf-8' );   
  17.   if (isset ( $_GET ['city'] )) {   
  18.    self:city = emptyempty ( $_GET ['city'] ) ? 'Beijing' : $_GET ['city'];   
  19.   }   
  20.   $contents = file_get_contents ( self:url . self:city ) or die ( '查询出错' );   
  21.   self:weatherXML = date ( "Ymd" ) . '-' . self:city . '-weather.xml';   
  22.   if (is_file ( self:weatherXML )) {   
  23.    $fileTime = filemtime ( self:weatherXML );   
  24.    $stater = time () - $fileTime - 60 * 60 * 2;   
  25.    if ($stater < 0) {   
  26.     self::cacheXML ( $contents );   
  27.    }   
  28.    return true;   
  29.   }   
  30.   self::cacheXML ( $contents );   
  31. }   
  32. /**  
  33.   * 解析xml  
  34.   */  
  35. static public function analysisXML() {   
  36.   if (is_file ( self:weatherXML )) {   
  37.    $xml = simplexml_load_file ( self:weatherXML );   
  38.   } else {   
  39.    $xml = simplexml_load_file ( self:url . self:city );   
  40.   }   
  41.   $xml = ( array ) $xml;   
  42.   $city = ( array ) $xml ['weather']->forecast_information->city;   
  43.   if (isset ( $xml ['weather']->problem_cause )) {   
  44.    $problem = ( array ) $xml ['weather']->problem_cause;   
  45.    echo $problem ['@attributes'] ['data'];   
  46.    return;   
  47.   }   
  48.      
  49.   $conditions = ( array ) $xml ['weather']->current_conditions->condition;   
  50.   $humidity = ( array ) $xml ['weather']->current_conditions->humidity;   
  51.   $temp_c = ( array ) $xml ['weather']->current_conditions->temp_c;   
  52.   $conditions_icon = ( array ) $xml ['weather']->current_conditions->icon;   
  53.   $wind_condition = ( array ) $xml ['weather']->current_conditions->wind_condition;   
  54.   $forecast = ( array ) $xml ['weather'];   
  55.   $forecast = ( array ) $forecast ['forecast_conditions'];   
  56.   $html = '';   
  57.   foreach ( $forecast as $key => $val ) {   
  58.       
  59.    ${'day_of_week_' . $key} = ( array ) $val->day_of_week;   
  60.    ${'low_' . $key} = ( array ) $val->low;   
  61.    ${'high_' . $key} = ( array ) $val->high;   
  62.    ${'icon_' . $key} = ( array ) $val->icon;   
  63.    ${'condition_' . $key} = ( array ) $val->condition;   
  64.    $html .= "<DIV class=w_fc title={${'condition_'.$key}['@attributes']['data']}>{${'day_of_week_'.$key}['@attributes']['data']}<BR>   
  65.         <IMG class=w_fci alt={${'condition_'.$key}['@attributes']['data']} src=\"http://www.google.com{${'icon_'.$key}['@attributes']['data']}\ " width=40 height=40><BR>   
  66.         <NOBR>{${'low_'.$key}['@attributes']['data']}°C | {${'high_'.$key}['@attributes']['data']}°C</NOBR></DIV>";   
  67.      
  68.   }   
  69.   self::printCss ();   
  70.   echo <<<weather   
  71.   <DIV>   
  72.   <DIV style="ADDING-TOP: 2px" class=modlabel><A style="COLOR: #00c; FONT-WEIGHT: bold" class=w_lnk href="#">{$city['@attributes']['data']}</A></DIV>   
  73.   <DIV class=w_box>   
  74.     <DIV class=w_ccs>   
  75.       <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>   
  76.       </DIV>   
  77.       <DIV id=w_4_c0_temp class=w_cc_temp>{$temp_c['@attributes']['data']}°C</DIV>   
  78.       <DIV id=w_4_c0_text class=w_cc_text>当前: {$conditions['@attributes']['data']}< BR>   
  79.         {$wind_condition['@attributes']['data']}<BR>   
  80.         {$humidity['@attributes']['data']}</DIV>   
  81.     </DIV>   
  82.     <DIV id=w_4_c0_fcs class=w_fcs>   
  83. $html  
  84.     </DIV>   
  85.   </DIV>   
  86. </DIV>   
  87. weather;   
  88.   
  89. }   
  90. /**  
  91.   * 打印样式  
  92.   */  
  93. static public function printCss() {   
  94.   echo <<<css   
  95. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
  96. <style type="text/css">   
  97. td, div, span, p { font-family:arial, sans-serif; font-size:10pt; margin:0}   
  98. .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}   
  99. .w_fc { TEXT-ALIGN: center; PADDING-BOTTOM: 0pxADDING-LEFT: 0pxADDING-RIGHT: 10px;FLOAT: left;HEIGHT: 80pxADDING-TOP: 0px}   
  100. .w_fcs {PADDING-BOTTOM: 0pxADDING-LEFT: 0pxADDING-RIGHT: 0px;OVERFLOW: hiddenADDING-TOP: 2px}   
  101. .w_box {PADDING-BOTTOM: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 1px}   
  102. .w_ccs {PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; PADDING-TOP: 2px}   
  103. .w_ccis {PADDING-BOTTOM: 1px; PADDING-LEFT: 1px; PADDING-RIGHT: 1px; FLOAT: left; PADDING-TOP: 1px}   
  104. .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}   
  105. .w_cc_temp {PADDING-BOTTOM: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 10px; FLOAT: left; FONT-SIZE: 180%; PADDING-TOP: 0px}   
  106. .w_cc_text {HEIGHT: 60px; OVERFLOW: hidden}   
  107. .w_lnk {COLOR: #00c}   
  108. .modlabel { padding-bottom:2px; padding-top:5px;}   
  109. </style>   
  110. css;   
  111. }   
  112. /**  
  113.   * 创建xml缓存  
  114.   * @param $contents 要缓存的内容  
  115.   */  
  116. static private function cacheXML($contents) {   
  117.   $contents = str_ireplace ( '<?xml version="1.0"?>'"<?xml version=\"1.0\"?> \n"$contents );   
  118.   $contents = mb_convert_encoding ( $contents'utf-8''gbk' );   
  119.   file_put_contents ( self:weatherXML, $contents ) or die ( '没有写权限' );   
  120. }   
  121. }   
  122. weather::getXML ();   
  123. weather::analysisXML ();  
原文出处:广漠传播 上海网站建设(http://www.greatmo.com/post/199.html)