<meta charset='utf-8' /> <?php class Weather { /** * 需求:根據中國氣象臺天氣預報xml接口查詢天氣 * 全中國的調用china.xml * 能夠得到 所有省會城市 天氣預報 * 全省的調用省份列表 如 heilongjiang.xml * 能夠得到 所有市區/單個市區 天氣預報 * 全市的調用市區列表 如 qiqihaer.xml * 能夠得到 所有區縣/單個區縣 天氣預報 * * 參數: * 城市信息:array $data['province'] $data['city'] $data['area'] * 例如 array('province'=>'heilongjiang','city'=>'qiqihaer','area'=>'龍江') * 是否直轄市/省會城市:Boolean 0/1/2 普通城市/直轄市/省會城市 * 收集信息類型 type:0-單個城市(非特別城市) 1-單個區縣 2-區縣列表 3-市區列表 4-全國列表(只有省會城市) * type 默認爲0 * */ protected $url = 'http://flash.weather.com.cn/wmaps/xml/'; private $dom = null; private $city_len = 0; protected $city = null; protected $city_arr = array('province'=>'','city'=>'','area'=>''); protected $type = 0; protected $special = 0; protected $attr_arr = array(); public function __construct($city_arr,$type=0,$special=0){ $this->city_arr = $city_arr; $this->type = $type; $this->special = $special; $this->get_url(); echo $this->url; $this->dom = new DomDocument(); $this->dom->load($this->url); $this->city = $this->dom->getElementsByTagName('city'); $this->city_len = $this->city->length; $this->attr_arr = $this->get_all_attr(); } // 根據給定的城市信息,特別城市信息,以及返回類型分析應該加載頁面 protected function get_url(){ if (($this->special!=0 && $this->type==0) || $this->type==4) { // 若是是特別城市,而且獲取的是單個城市 $page = 'china'; }else { // 若是是獲取市區列表 type=3 或者是普通城市的單個城市信息 // 則進入省份頁面 // 若是獲取的是區縣列表 $page = ($this->type == 3 || ($this->special==0 && $this->type==0) || $this->special==1 ) ? $this->city_arr['province'] : $this->city_arr['city']; } $this->url .= $page.'.xml'; } // 根據pyName獲取單個城市信息 // china頁面和省份頁面經過pyName能夠獲取單個城市信息 // 返回該城市在節點中的順序 從0開始 // protected function get_list_num($attr_value,$attr_key='pyName'){ protected function get_list_num(){ $num = 0; for ($i = 0; $i < $this->city_len; $i++) { $item = $this->city->item($i); $cityname = $item->getAttribute('cityname'); $city_value = $this->type== 1 ? $cityname : $item->getAttribute('pyName'); $pyName = $this->special!=0 ? $this->city_arr['province'] : $this->city_arr['city']; $pyName = $this->type==1 ? $this->city_arr['area'] : $pyName; if ($pyName==$city_value || (strpos($city_value,$pyName)!==false)) { $num = $i; break; } continue; } return $num; } // 獲取所有屬性信息 protected function get_all_attr(){ $attr_list = $this->city->item(0)->attributes; $attr_len = $attr_list->length; for ($i = 0; $i < $attr_len; $i++) { $data[$i] = $attr_list->item($i)->nodeName; } return $data; } // 獲取天氣預報 public function get_weather(){ // 根據要抓取的類型判斷 if ($this->type==0 || $this->type==1) { // 獲取單個城市信息 $data = $this->get_one_city(); }else { $data = $this->get_city_list(); } return $data; } // 獲取單個城市預報信息 protected function get_one_city(){ $list_num = $this->get_list_num(); $item = $this->city->item($list_num); $data = $this->get_node_attr($item); return $data; } // 獲取單個區縣預報信息 protected function get_one_area(){ } // 獲取多個城市列表信息 protected function get_city_list(){ for ($i = 0; $i < $this->city_len; $i++) { $item = $this->city->item($i); $data[$i] = $this->get_node_attr($item); } return $data; } // 獲取節點的屬性值 protected function get_node_attr($node){ $data = array(); foreach ($this->attr_arr as $value) { $data[$value] = $node->getAttribute($value); } return $data; } } $city_arr = array('province'=>'hebei','city'=>'chengde','area'=>'承德'); $type = 1; $special = 0; $w = new Weather($city_arr,$type,$special); $data = $w->get_weather(); print_r($data); ?>