Weather. php Page code
<? 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 (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;
}
}
Call Page code
$ City = 'beijing ';
$ Weather = new evebit_weather ();
$ Weather-> getweather ($ City );
I used Google weather API address: http://www.google.com/ig/api? Hl = en & weather = Beijing