通过google weather api获得天气xml信息并用DOMDocument将其转换为数组(实例)

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值