|
类的代码:
1<?php 2/** 3 * 抓取腾讯天气,存储成XML 4 * @author PeterPan 5 * @final 2007-08-27 6 */ 7class 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?>
测试程序:
1<?php 2header("Content-Type: text/html; charset=UTF-8"); //设置页面编码 3$weather_obj = new Weather(); 4//获取可用的城市 5$city_list = $weather_obj->getAllCity(); 6var_dump($city_list); 7echo "<br><br>"; 8//获取北京的天气情况,北京的代号是125 9$result = $weather_obj->getWeather("125"); 10var_dump($result); 11echo "<br><br>"; 12//获取所有地区的天气 13$result = $weather_obj->getAllCityWeather(); 14var_dump($result); 15echo "<br><br>"; 16//保存数据到文件 17$xml = $weather_obj->saveXML("a.xml"); 18//从文件加载数据 19$weather_obj->loadDataFromXML("a.xml"); 20var_dump($weather_obj->getAllCityWeather()); 21?>
|