抓取騰訊天氣預報的類
類的代碼:
1
<?php
2
/**
3
* 抓取騰訊天氣,存儲成XML
4
* @author PeterPan
5
* @final 2007-08-27
6
*/
7
class Weather{
8
/**成員變量*/
9
var $m_city_list = array();
10
var $m_data = array();
11
var $m_url_tpl = "http://weather.news.qq.com/inc/07_ss{city_code}.htm";
12
var $m_url = "";
13
var $m_city_code = "125";
14
/**
15
* 構造函數
16
*/
17
function __construct($city_code=""){
18
if ($city_code){
19
$this->m_city_code = $m_city_code;
20
}
21
}
22
function __destruct(){
23
unset($this->m_data);
24
unset($this->m_url_tpl);
25
unset($this->m_url);
26
unset($this->m_city_code);
27
}
28
/**
29
* 根據$url提供的地址,讀取數據
30
*
31
* @param unknown_type $url
32
*/
33
function getUrlContent($url){
34
//先嘗試用file_get_contents獲取
35
if ( $data=file_get_contents($url) ){
36
return $data;
37
}
38
else {
39
$url_a = parse_url($url);
40
if ( $url_a['scheme']!="http"&&$url_a['scheme']!="https" ){
41
return "";
42
}
43
if ( empty($url_a['port']) ){
44
$url_a['port'] = 80;
45
}
46
//開始連接
47
$data = "";
48
if ( $fp=fsockopen($url_a['host'],$url_a['port'],$errno,$errstr,30) ){
49
$request = "GET ".$url_a['path']." HTTP/1.1\r\n";
50
$request .= "Host: ".$url_a['host']."\r\nConnection: Close\r\n\r\n";
51
fwrite($fp,$request);
52
$line = "";
53
while ( $line=fread($fp,2048) ){
54
$data .= $line;
55
}
56
fclose($fp);
57
}
58
$pos = strpos($data,'<html>');
59
$data = substr($data,$pos);
60
echo $data;
61
return $data;
62
}
63
}
64
/**
65
* 獲取某個城市的天氣情況,需要打開allow_url_open
66
*
67
* @param string $city_code 城市代碼,默認為$m_city_code指定的城市
68
* @param int $reget 是否重新獲取,默認為0
69
* @return array
70
*/
71
function getWeather($city_code="",$reget=0){
72
if ($city_code){
73
$this->m_city_code = $city_code;
74
}
75
if ( $reget&&key_exists($this->m_city_code,$this->m_data) ){
76
return $this->m_data[$this->m_city_code];
77
}
78
$this->m_url = str_replace("{city_code}",$this->m_city_code,$this->m_url_tpl);
79
if ( $content = $this->getUrlContent($this->m_url) ){
80
$content = iconv('gb2312','UTF-8',$content);
81
$weather = array();
82
//讀取城市信息
83
$pattern = '/<td width="151" height="82" class="wht1 lk37">(\S+)<\/td>/ims';
84
preg_match_all($pattern,$content,$matches);
85
$weather['city'] = trim($matches[1][0]);
86
//讀取天氣情況
87
$pattern = '/<td height="77" class="wht2 lk37">.<div class="txbd">(\S+)<\/div>([^<]+)<\/td>/ims';
88
preg_match_all($pattern,$content,$matches);
89
$weather['desc'] = trim($matches[1][0]);
90
$weather['temperature'] = trim($matches[2][0]);
91
//獲取天氣詳細
92
$pattern = '/<td class="whp3 lk37">(.+)<\/td>/ims';
93
preg_match_all($pattern,$content,$matches);
94
$weather['detail'] = trim($matches[1][0]);
95
$weather['detail'] = preg_replace('/([\r\n])[\s]+/','',$weather['detail']);
96
$this->m_data[$this->m_city_code] = $weather;
97
unset($weather);
98
return $this->m_data[$this->m_city_code];
99
}
100
else {
101
return array();
102
}
103
}
104
/**
105
* 獲取頁面中的所有地區
106
*
107
* @return array
108
*/
109
function getAllCity(){
110
$url = "http://weather.news.qq.com/inc/07_ss125.htm";
111
if ( $content = $this->getUrlContent($url) ){
112
$content = iconv('gb2312','UTF-8',$content);
113
$city_list = array();
114
//獲取城市列表
115
$pattern = '/<option (selected="selected" )?value="(\S+)">(\S+)<\/option>/ims';
116
preg_match_all($pattern,$content,$matches);
117
$c = count($matches[2]);
118
for ($i=0;$i<$c;$i++){
119
$city_list[$matches[2][$i]] = trim($matches[3][$i]);
120
}
121
$this->m_city_list = $city_list;
122
return $this->m_city_list;
123
}
124
else {
125
return array();
126
}
127
}
128
/**
129
* 獲得所有城市的天氣情況
130
*
131
* @return array
132
*/
133
function getAllCityWeather(){
134
if ( empty($this->m_city_list) ){
135
$this->m_city_list = $this->getAllCity();
136
}
137
foreach ($this->m_city_list as $key=>$value){
138
$this->getWeather($key);
139
}
140
return $this->m_data;
141
}
142
/**
143
* 將天氣信息存儲到XML文件中
144
*
145
* @param string $xml_path 文件路徑
146
*/
147
function saveXML($xml_path=""){
148
$xml = '<?xml version="1.0" encoding="UTF-8"?>
149
<root>
150
';
151
foreach ($this->m_data as $key=>$value){
152
$xml .= '
153
<city code="'.$key.'" name="'.$value['city'].'">
154
<desc><![CDATA['.$value['desc'].']]></desc>
155
<temperature><![CDATA['.$value['temperature'].']]></temperature>
156
<detail><![CDATA['.$value['detail'].']]></detail>
157
</city>';
158
}
159
$xml .= '</root>';
160
if ( $xml_path ){
161
//打開并寫入數據
162
if ( $fp=fopen($xml_path,"w") ){
163
fwrite($fp,$xml);
164
}
165
fclose($fp);
166
}
167
return $xml;
168
}
169
/**
170
* 從XML中讀取數據
171
*
172
* @param unknown_type $xml_path
173
*/
174
function loadDataFromXML($xml_path){
175
$this->m_city_list = array();
176
$this->m_data = array();
177
if ( $data=file_get_contents($xml_path) ){
178
$xml = new SimpleXMLElement($data);
179
foreach ($xml->city as $city){
180
$weather = array('city'=>'','desc'=>'','temperature'=>'','detail'=>'');
181
$code = (string)$city['code'];
182
$name = (string)$city['name'];
183
$this->m_city_list[$code] = $name;
184
$weather['city'] = $name;
185
$weather['desc'] = (string)$city->desc;
186
$weather['temperature'] = (string)$city->temperature;
187
$weather['detail'] = (string)$city->detail;
188
$this->m_data[$code] = $weather;
189
}
190
}
191
//var_dump($this->m_city_list);
192
var_dump($this->m_data);
193
}
194
}
195
?>

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

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

測試程序:
1
<?php
2
header("Content-Type: text/html; charset=UTF-8"); //設置頁面編碼
3
$weather_obj = new Weather();
4
//獲取可用的城市
5
$city_list = $weather_obj->getAllCity();
6
var_dump($city_list);
7
echo "<br><br>";
8
//獲取北京的天氣情況,北京的代號是125
9
$result = $weather_obj->getWeather("125");
10
var_dump($result);
11
echo "<br><br>";
12
//獲取所有地區的天氣
13
$result = $weather_obj->getAllCityWeather();
14
var_dump($result);
15
echo "<br><br>";
16
//保存數據到文件
17
$xml = $weather_obj->saveXML("a.xml");
18
//從文件加載數據
19
$weather_obj->loadDataFromXML("a.xml");
20
var_dump($weather_obj->getAllCityWeather());
21
?>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

posted on 2007-08-27 16:37 編程之道 閱讀(2306) 評論(2) 編輯 收藏 引用 所屬分類: web編程 、開發相關