通过google weather api获得天气xml信息并用DOMDocument将其转换为数组(实例)
2011-03-28 18:00
489 查看
weather.php页面代码
<?php
/**
* NOTICE OF LICENSE
*
* THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
*
* DO NOT USE THIS FILE IN OTHER PLACE.
*
* @category EveBit_Library
* @package Application
* @author Chen Qiao <chen.qiao@evebit.com>
* @version $$Id: Event.php 130 2011-03-18 03:10:02Z cheng.wei $$
* @copyright Copyright (c) 2011 Evebit Inc. China (http://www.evebit.com)
*/
/**
* Weather class
*
* access to google weather api get the weather condition xml and decode it array
*
* @docinfo
*
* @package Application
* @author Chen Qiao <chen.qiao@evebit.com>
* @version $$Id: Event.php 130 2011-03-18 03:10:02Z cheng.wei $$
*/
class Evebit_Weather {
/**
* @var $_apiUrl string
*/
private $_apiUrl;
/**
* @var $_cachePath string
*/
private $_cachePath;
/**
* @var $_hostUrl string
*/
private $_hostUrl;
/**
* __construct of the class
*/
public function __construct() {
$this->_hostUrl = 'http://www.google.com';
$this->_apiUrl = 'http://www.google.com/ig/api?hl=en&weather=';
$this->_cachePath = './../var/cache/weather/';
}
/**
* get weather xml with api url and decode it
*
* @param string $city
* @return array
*/
public function getWeather($city) {
$weatherArr = array();
$city = ucwords($city);
$fileName = date('Ymd',strtotime('now')).$city.'.xml';
$weatherArr = $this->cacheXml($city,$fileName);
return $weatherArr;
}
/**
* decode the weather xml
*
* @param string $cacheXml
* @param string $url
* @return array|string
*/
private function decodeXml($cacheXml,$url) {
$result = '';
$dom = new DOMDocument();
if(!$dom->load($cacheXml)) {
if(!$dom->load($url)) {
$result = 'Weather data failed to load, please refresh the page to try again';
}
}
if(!$result) {
$currentDom = $dom->getElementsByTagName('current_conditions');
$current = array('condition'=>'condition','tempF'=>'temp_f','tempC'=>'temp_c',
'humidity'=>'humidity','icon'=>'icon','wind'=>'wind_condition');
$result['current'] = $this->getWeatherArray($currentDom,$current);
$forecastDom = $dom->getElementsByTagName('forecast_conditions');
$forecast = array('week'=>'day_of_week','low'=>'low','high'=>'high',
'icon'=>'icon','condition'=>'condition');
$result['forecast'] = $this->getWeatherArray($forecastDom,$forecast);
}
return $result;
}
/**
* get the array with the weather dom
*
* @param object $weatherDom
* @param array $xmlArr
* @return array
*/
private function getWeatherArray($weatherDom,$xmlArr) {
$weatherArr = array();
foreach($weatherDom as $k=>$weather){
foreach ($xmlArr as $key=>$value) {
$weatherAttribute = $weather->getElementsByTagName($value);
$weatherArr[$key][$k] = $weatherAttribute->item(0)->attributes->item(0)->nodeValue;
if($key == 'tempF') {
$weatherArr[$key][$k] .= '°F';
} else if($key == 'tempC' || $key == 'low' || $key == 'high') {
$weatherArr[$key][$k] .= '°C';
} else if($key == 'icon') {
$weatherArr[$key][$k] = $this->_hostUrl.$weatherArr[$key][$k];
}
}
}
return $weatherArr;
}
/**
* get weather xml with api url and decode it
*
* @param string $city
* @param string $fileName
* @return array
*/
private function cacheXml($city,$fileName) {
$weathArr = array();
if(!is_dir($this->_cachePath)) {
mkdir($this->_cachePath);
}
$cachePath = chop($this->_cachePath);
if($cachePath != ''){
if(substr($cachePath,strlen($cachePath)-1,strlen($cachePath)) != '/') {
$cachePath .= '/';
}
}
$cachePath .= $fileName;
if(!file_exists($cachePath)) {
$url = $this->_apiUrl.$city;
$message = file_get_contents($url);
file_put_contents($cachePath,$message);
}
$weathArr = $this->decodeXml($cachePath,$url);
return $weathArr;
}
}
调用页面代码
$city = 'beijing';
$weather = new Evebit_Weather();
$weather->getWeather($city);
我用的google weather api的地址为:http://www.google.com/ig/api?hl=en&weather=Beijing
<?php
/**
* NOTICE OF LICENSE
*
* THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
*
* DO NOT USE THIS FILE IN OTHER PLACE.
*
* @category EveBit_Library
* @package Application
* @author Chen Qiao <chen.qiao@evebit.com>
* @version $$Id: Event.php 130 2011-03-18 03:10:02Z cheng.wei $$
* @copyright Copyright (c) 2011 Evebit Inc. China (http://www.evebit.com)
*/
/**
* Weather class
*
* access to google weather api get the weather condition xml and decode it array
*
* @docinfo
*
* @package Application
* @author Chen Qiao <chen.qiao@evebit.com>
* @version $$Id: Event.php 130 2011-03-18 03:10:02Z cheng.wei $$
*/
class Evebit_Weather {
/**
* @var $_apiUrl string
*/
private $_apiUrl;
/**
* @var $_cachePath string
*/
private $_cachePath;
/**
* @var $_hostUrl string
*/
private $_hostUrl;
/**
* __construct of the class
*/
public function __construct() {
$this->_hostUrl = 'http://www.google.com';
$this->_apiUrl = 'http://www.google.com/ig/api?hl=en&weather=';
$this->_cachePath = './../var/cache/weather/';
}
/**
* get weather xml with api url and decode it
*
* @param string $city
* @return array
*/
public function getWeather($city) {
$weatherArr = array();
$city = ucwords($city);
$fileName = date('Ymd',strtotime('now')).$city.'.xml';
$weatherArr = $this->cacheXml($city,$fileName);
return $weatherArr;
}
/**
* decode the weather xml
*
* @param string $cacheXml
* @param string $url
* @return array|string
*/
private function decodeXml($cacheXml,$url) {
$result = '';
$dom = new DOMDocument();
if(!$dom->load($cacheXml)) {
if(!$dom->load($url)) {
$result = 'Weather data failed to load, please refresh the page to try again';
}
}
if(!$result) {
$currentDom = $dom->getElementsByTagName('current_conditions');
$current = array('condition'=>'condition','tempF'=>'temp_f','tempC'=>'temp_c',
'humidity'=>'humidity','icon'=>'icon','wind'=>'wind_condition');
$result['current'] = $this->getWeatherArray($currentDom,$current);
$forecastDom = $dom->getElementsByTagName('forecast_conditions');
$forecast = array('week'=>'day_of_week','low'=>'low','high'=>'high',
'icon'=>'icon','condition'=>'condition');
$result['forecast'] = $this->getWeatherArray($forecastDom,$forecast);
}
return $result;
}
/**
* get the array with the weather dom
*
* @param object $weatherDom
* @param array $xmlArr
* @return array
*/
private function getWeatherArray($weatherDom,$xmlArr) {
$weatherArr = array();
foreach($weatherDom as $k=>$weather){
foreach ($xmlArr as $key=>$value) {
$weatherAttribute = $weather->getElementsByTagName($value);
$weatherArr[$key][$k] = $weatherAttribute->item(0)->attributes->item(0)->nodeValue;
if($key == 'tempF') {
$weatherArr[$key][$k] .= '°F';
} else if($key == 'tempC' || $key == 'low' || $key == 'high') {
$weatherArr[$key][$k] .= '°C';
} else if($key == 'icon') {
$weatherArr[$key][$k] = $this->_hostUrl.$weatherArr[$key][$k];
}
}
}
return $weatherArr;
}
/**
* get weather xml with api url and decode it
*
* @param string $city
* @param string $fileName
* @return array
*/
private function cacheXml($city,$fileName) {
$weathArr = array();
if(!is_dir($this->_cachePath)) {
mkdir($this->_cachePath);
}
$cachePath = chop($this->_cachePath);
if($cachePath != ''){
if(substr($cachePath,strlen($cachePath)-1,strlen($cachePath)) != '/') {
$cachePath .= '/';
}
}
$cachePath .= $fileName;
if(!file_exists($cachePath)) {
$url = $this->_apiUrl.$city;
$message = file_get_contents($url);
file_put_contents($cachePath,$message);
}
$weathArr = $this->decodeXml($cachePath,$url);
return $weathArr;
}
}
调用页面代码
$city = 'beijing';
$weather = new Evebit_Weather();
$weather->getWeather($city);
我用的google weather api的地址为:http://www.google.com/ig/api?hl=en&weather=Beijing
相关文章推荐
- QT通过API获取天气信息
- 通过IP地址获得用户所在城市的额天气信息的整理
- Android 获取Google Weather API 并通过Xml和JSON解析数据
- 百度天气api开发 全解析xml和json格式 使用json-lib jsonObject、 Gson 方式把json 转换成 java对象
- 想在自己的android应用中获得当天的天气情况,这该怎么做呢?不用担心。中国国家气象局提供了获取所在城市天气预报信息接口。通过这个接口,我们就可以获取天气信息了。
- android通过google api获取天气信息示例
- Android 获取Google Weather API 并通过Xml和JSON解析数据
- Qt通过api获取天气信息
- 问题记录--通过jaxb转换的xml增加namespace信息
- 验证11gR2 RAC中ASM实例通过gpnp profile获得spfile信息来启动ASM实例
- Tp3.2 RESTFul 根据地区查询生成xml、html、json的Api接口的天气信息
- QT用API获得文件的版本信息
- 将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)
- 通过调用系统API获得网卡真实MAC地址
- 用钩子获得按键信息实例
- Google API 天气数据缓存到一个XML中
- Android根据内容提供者获得联系人信息实例
- 通过两个汇编实例对高级语言数组实现原理的发现
- 获得训练集和XML通过opencv自带程序opencv_createsamples.exe&opencv_traincascade.exe的问题
- [置顶] 一位数组返回id和pid通过这两个参数转换为树形结构数据,和树形结构的渲染