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

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



转载于:https://my.oschina.net/u/946060/blog/292923

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值