'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);?>