您的位置:首页 > 编程语言 > Go语言

通过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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息