天气预报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 = '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);
?>
天气预报php xml接口,根据中国气象网xml数据返回天气预报相关推荐
- 天气预报接口(中国天气网)
天气预报接口(中国天气网)转载地址 发表于 2011 年 10 月 20 日 由wish 中国天气weather.com http://m.weather.com.cn/data/101110101. ...
- python爬取本地天气信息_用Python写一个爬取中国天气网的终端版天气预报爬虫
导语 前几篇文章介绍了爬取静态网站的主要方法.今天写一个小项目实践一下.本项目可以在终端窗口查询全国3400多个区县的当日天气信息和近七天天气信息. 实现效果 [Python爬虫]写一个爬取中国天气网 ...
- weather中国气象网的天气城市编码获取地址
weather中国气象网的天气城市编码获取地址: http://www.weather.com.cn/data/city3jdata/china.html http://www.weather.com ...
- 中国气象网气象数据后台获取方法
项目开发中需要用到天气信息,但有不适合直接放置气象插件,于是自己找到这个方法:周期性从中国气象网抓取信息保存进数据库,然后显示. 最主要是这几个链接地址,其他的不重要.相信连接地址的规律不用我说也明白 ...
- 四站气象对比(ECMWF+NOAA+中国气象网+GFS)——大气数据分析研究
一.ECMWF(方正附近,46,128.75,张志浩) from netCDF4 import Dataset from netCDF4 import num2date nc_obj = Datase ...
- 中国天气网爬虫数据可视化
目录 中国天气网爬虫数据可视化 爬虫功能 网页分析 以华北地区为例分析网页源代码 1.以谷歌浏览器为例分析 2. 提取特征标签 3.分析源代码 利用requests库获取目标网页源代码 利用Beaut ...
- python爬虫获取中国天气网天气数据 requests BeautifulSoup re
python获取中国天气网天气数据:http://www.weather.com.cn/textFC/henan.shtml main.py # -*- coding: utf-8 -*- impor ...
- android往天气接口里面传城市,Android使用中国天气网API数据通过城市名称获取天气情况...
项目要求在应用首页面展示本地当日天气的概况,首先想到的是google和雅虎,前者很久之前接触过,听说后来用不了了,后者由于邮箱事件的缘故个人不喜欢(虽然貌似苹果也用雅虎的天气预报),之后又想到了前段时 ...
- C#实现中国天气网XML接口测试
点击链接查看中国天气网接口说明,最近想研究一下接口测试,源于最近一次和某公司的技术总监(交大校友)谈话,发现接口测试的需求是比较大的,于是想要研究一下. 好不容易在网上找到了一个关于中国天气网的接口说 ...
最新文章
- 14.VisualVM使用详解、15.VisualVM堆查看器使用的内存不足、19.class文件--文件结构--魔数、20.文件结构--常量池、21.文件结构访问标志(2个字节)、22.类加载机制概
- C#多线程编程之:lock使用注意事项
- 太平洋大西洋水流问题如何解决?一文了解图在前端中的应用
- 快钱接口php,快钱支付接口
- java 中equals和==的区别
- LinuxCentos7 防火墙开放端口,查看状态,查看开放端口
- 带薪休假,运维汪的春天来了?
- sscanf_s函数
- 路由与交换技术(复习知识点-全)
- 石墨烯气凝胶的3D打印-气凝胶(Aerogels)是世界上最轻的材料之一,石墨烯气凝胶(Graphene aerogel)又是该品类中最轻的一种
- 启发函数 (Heuristic Function) —Octile
- Windows 取证之$MFT
- 计算机修图教程,电脑修图太复杂 3步教你学会用手机做肤色后期
- NONMEM软件自定义模型
- 《绝地求生》玩家排名预测(2万5千字~大型综合实战)
- “5G消息”应用案例!七大银行试商用情况汇总!
- python中的下划线_讲解
- 511遇见易语言程序集模块和类模块的区别
- gimp的中文化,汉化安装
- 3DES的算法原理浅析