首頁 php教程 PHP源码 借用Google API 写一个查看天气预报的类

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

May 25, 2016 pm 05:15 PM

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 ();
?>
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)