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

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

May 25, 2016 pm 05:15 PM

php代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

<?

//- 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 ();

?>

Salin selepas log masuk
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

Video Face Swap

Video Face Swap

Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)