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

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 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 채팅 명령 및 사용 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

DVWA

DVWA

DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

맨티스BT

맨티스BT

Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

SublimeText3 영어 버전

SublimeText3 영어 버전

권장 사항: Win 버전, 코드 프롬프트 지원!

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.