Maison 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 ();
?>
Copier après la connexion
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)