Home  >  Article  >  php教程  >  借用Google API 写一个查看天气预报的类

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

PHP中文网
PHP中文网Original
2016-05-25 17:15:53891browse

php代码

<?
//- Describe: 
//- Author: liuguichun
//- Link: 
//- CreateTime: 2010-6-21
//- UpdateTime: 
//- Package: 
class weather {
 static $url = &#39;http://www.google.com/ig/api?hl=zh-cn&weather=&#39;;
 static $city = &#39;Beijing&#39;; //默认城市北京
 static $weatherXML = &#39;&#39;;
 /**
  * 获得远程xml并缓存到本地
  */
 static public function getXML() {
  header ( &#39;Content-Type: text/html; charset = utf-8&#39; );
  if (isset ( $_GET [&#39;city&#39;] )) {
   self::$city = empty ( $_GET [&#39;city&#39;] ) ? &#39;Beijing&#39; : $_GET [&#39;city&#39;];
  }
  $contents = file_get_contents ( self::$url . self::$city ) or die ( &#39;查询出错&#39; );
  self::$weatherXML = date ( "Ymd" ) . &#39;-&#39; . self::$city . &#39;-weather.xml&#39;;
  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 [&#39;weather&#39;]->forecast_information->city;
  if (isset ( $xml [&#39;weather&#39;]->problem_cause )) {
   $problem = ( array ) $xml [&#39;weather&#39;]->problem_cause;
   echo $problem [&#39;@attributes&#39;] [&#39;data&#39;];
   return;
  }
  
  $conditions = ( array ) $xml [&#39;weather&#39;]->current_conditions->condition;
  $humidity = ( array ) $xml [&#39;weather&#39;]->current_conditions->humidity;
  $temp_c = ( array ) $xml [&#39;weather&#39;]->current_conditions->temp_c;
  $conditions_icon = ( array ) $xml [&#39;weather&#39;]->current_conditions->icon;
  $wind_condition = ( array ) $xml [&#39;weather&#39;]->current_conditions->wind_condition;
  $forecast = ( array ) $xml [&#39;weather&#39;];
  $forecast = ( array ) $forecast [&#39;forecast_conditions&#39;];
  $html = &#39;&#39;;
  foreach ( $forecast as $key => $val ) {
   
   ${&#39;day_of_week_&#39; . $key} = ( array ) $val->day_of_week;
   ${&#39;low_&#39; . $key} = ( array ) $val->low;
   ${&#39;high_&#39; . $key} = ( array ) $val->high;
   ${&#39;icon_&#39; . $key} = ( array ) $val->icon;
   ${&#39;condition_&#39; . $key} = ( array ) $val->condition;
   $html .= "

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

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

  
{$city[&#39;@attributes&#39;][&#39;data&#39;]}  

    

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

      

      
{$temp_c[&#39;@attributes&#39;][&#39;data&#39;]}°C

      
当前: {$conditions[&#39;@attributes&#39;][&#39;data&#39;]}

        {$wind_condition[&#39;@attributes&#39;][&#39;data&#39;]}

        {$humidity[&#39;@attributes&#39;][&#39;data&#39;]}

    

    

 $html
    

  



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

css;
 }
 /**
  * 创建xml缓存
  * @param $contents 要缓存的内容
  */
 static private function cacheXML($contents) {
  $contents = str_ireplace ( &#39;&#39;, " \n", $contents );
  $contents = mb_convert_encoding ( $contents, &#39;utf-8&#39;, &#39;gbk&#39; );
  file_put_contents ( self::$weatherXML, $contents ) or die ( &#39;没有写权限&#39; );
 }
}
weather::getXML ();
weather::analysisXML ();
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn