失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 天气预报php xml接口 根据中国气象网xml数据返回天气预报

天气预报php xml接口 根据中国气象网xml数据返回天气预报

时间:2023-09-26 18:10:02

相关推荐

天气预报php xml接口 根据中国气象网xml数据返回天气预报

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 = '/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);

?>

如果觉得《天气预报php xml接口 根据中国气象网xml数据返回天气预报》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。