evo kod:
class.xml.praser.php
Code:
<?
//============================================================================
//============================================================================
// Script: PHP Class "xmlParser"
//============================================================================
// From: http://ch2.php.net/xml
// Autor: monte at NOT-SP-AM dot ohrt dot com
// Date: 14-Sep-2005 06:48
// License/
// Usage: Open Source / for free
//============================================================================
// DESCRIPTION:
// This is a class for XML parsing with an URL input. It does:
// - Get File from URL (XML/RSS-File)
// - Parsing the file into array
// - Return Array
//============================================================================
//============================================================================
class xmlParser{
// *** ----------------------------------------------------------------
// DECLARATION
var $xml_obj = null;
var $output = array();
// *** ----------------------------------------------------------------
// CONSTRUCTOR
function xmlParser(){
$this->xml_obj = xml_parser_create();
xml_set_object($this->xml_obj,$this);
xml_set_character_data_handler($this->xml_obj, 'dataHandler');
xml_set_element_handler($this->xml_obj, "startHandler", "endHandler");
}
// *** ----------------------------------------------------------------
function parse($path){
if (!($fp = fopen($path, "r"))) {
die("Cannot open XML data file: $path");
return false;
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($this->xml_obj, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml_obj)),
xml_get_current_line_number($this->xml_obj)));
xml_parser_free($this->xml_obj);
}
}
return true;
}
// *** ----------------------------------------------------------------
function startHandler($parser, $name, $attribs){
$_content = array('name' => $name);
if(!empty($attribs))
$_content['attrs'] = $attribs;
array_push($this->output, $_content);
}
// *** ----------------------------------------------------------------
function dataHandler($parser, $data){
if(!empty($data)) {
$_output_idx = count($this->output) - 1;
$this->output[$_output_idx]['content'] = $data;
}
}
// *** ----------------------------------------------------------------
function endHandler($parser, $name){
if(count($this->output) > 1) {
$_data = array_pop($this->output);
$_output_idx = count($this->output) - 1;
$this->output[$_output_idx]['child'][] = $_data;
}
}
// *** ----------------------------------------------------------------
function GetNodeByPath($path,$tree = false) {
if ($tree) {
$tree_to_search = $tree;
}
else {
$tree_to_search = $this->output;
}
if ($path == "") {
return null;
}
$arrPath = explode('/',$path);
foreach($tree_to_search as $key => $val) {
if (gettype($val) == "array") {
$nodename = $val[name];
if ($nodename == $arrPath[0]) {
if (count($arrPath) == 1) {
return $val;
}
array_shift($arrPath);
$new_path = implode($arrPath,"/");
return $this->GetNodeByPath($new_path,$val[child]);
}
}
}
}
} // class : end
?>
class.weather.php
Code:
<?
//============================================================================
//============================================================================
// Script: PHP Class "weather"
// Version: 2.0 / 12.12.2006
//
// Rewritten: Dec 2006 by Matt Brown
// fixed all PHP warnings
// added cachedir parameter
// changed structure of results
// more adaptive to changes in Yahoo RSS feed
//============================================================================
// From: http://www.voegeli.li
// Autor: Marco Voegeli, Switzerland >> www.voegeli.li >> fly forward! >>
// Date: 28-Oct-2005
// License/
// Usage: Open Source / for free
//============================================================================
// DEPENDENCIES:
// - It requires the class "xmlParser" (Be lucky: Also in the Archive file!)
//============================================================================
// DESCRIPTION:
// This Class gets Weather RSS from WEATHER.YAHOO.COM and parses it into
// a weather object with usable attributes. Use it for:
//
// - Actual Situation (temperature/sunrise/sunset/Image...)
// - Forecast Day 1 (temp low, high/text/date/day/image...)
// - Forecast Day 2 (temp low, high/text/date/day/image...)
//
// PUBLIC METHODS
// - parse() : Gets the XML File parses it and fills attributes
// - parsecahed() : Much quicker!!! Writes a cached version to a local
// file with expiry date! expiry date is calculated
// with the given input parameter
//
//============================================================================
// SAMPLE:
// - See the file "weather.test.php" in this archive for Santiago de Chile
//
// WEB GUI URL: http://weather.yahoo.com/forecast/CIXX0020_c.html?force_units=1
// RSS URL: http://xml.weather.yahoo.com/forecastrss?u=C&p=CIXX0020
//
// The class needs one Attribute in the Constructor Method:
//
// $weather_chile = new weather("CIXX0020", 60);
//
// "CIXX0020" is the Yahoo code for Santiago de Chile. See WEB GUI URL above!
//
// "60" means 60 seconds until the cache expires. If not needed set = 0.
//
// GO TO WEATHER.YAHOO.COM and search for your desired weather location. If
// found, click on the location link (must see the forecast). Now take
// the code from the URL in your browsers address field.
//
//============================================================================
// Changes:
// - 19.11.2005 MAV : XML Feed Structure from Yahoo changed. Adapted script.
//============================================================================
// Visit http://dowdybrown.com , the contributor of the new version. Thank you
// Matt for this great and better version of the yahoo weather class! You have
// done a good job!
//============================================================================
class weather
{
// -------------------
// ATTRIBUTES DECLARATION
// -------------------
// HANDLING ATTRIBUTES
var $locationcode; // Yahoo Code for Location
var $allurl; // generated url with location
var $parser; // Instance of Class XML Parser
var $unit; // F or C / Fahrenheit or Celsius
// CACHING ATTRIBUTES
var $cache_expires;
var $cache_lifetime;
var $source; // cache or live
var $forecast=array();
// -------------------
// CONSTRUCTOR METHOD
// -------------------
function weather($location, $lifetime, $unit, $cachedir)
{
// Set Lifetime / Locationcode
$this->cache_lifetime = $lifetime;
$this->locationcode = $location;
$this->unit = $unit;
$this->cachedir = $cachedir;
$this->filename = $cachedir . $location;
}
// -------------------
// FUNCTION PARSE
// -------------------
function parse()
{
$this->allurl = "http://xml.weather.yahoo.com/forecastrss";
$this->allurl .= "?u=" . $this->unit;
$this->allurl .= "&p=" . $this->locationcode;
// Create Instance of XML Parser Class
// and parse the XML File
$this->parser = new xmlParser();
$this->parser->parse($this->allurl);
$content=&$this->parser->output[0]['child'][0]['child'];
foreach ($content as $item) {
//print "<hr><pre>";
//print_r($item);
//print "</pre></p>";
switch ($item['name']) {
case 'TITLE':
case 'LINK':
case 'DESCRIPTION':
case 'LANGUAGE':
case 'LASTBUILDDATE':
$this->forecast[$item['name']]=$item['content'];
break;
case 'YWEATHER:LOCATION':
case 'YWEATHER:UNITS':
case 'YWEATHER:ASTRONOMY':
foreach ($item['attrs'] as $attr=>$value)
$this->forecast[$attr]=$value;
break;
case 'IMAGE':
break;
case 'ITEM':
foreach ($item['child'] as $detail) {
switch ($detail['name']) {
case 'GEO:LAT':
case 'GEO:LONG':
case 'PUBDATE':
$this->forecast[$detail['name']]=$detail['content'];
break;
case 'YWEATHER:CONDITION':
$this->forecast['CURRENT']=$detail['attrs'];
break;
case 'YWEATHER:FORECAST':
array_push($this->forecast,$detail['attrs']);
break;
}
}
break;
}
}
$this->source = 'live';
// FOR DEBUGGING PURPOSES
//print "<hr><pre>";
//print_r($this->forecast);
//print "</pre></p>";
}
// -------------------
// WRITE OBJECT TO CACHE
// -------------------
function writecache() {
unset($this->parser);
$this->cache_expires = time() + $this->cache_lifetime;
$fp = fopen($this->filename, "w");
fwrite($fp, serialize($this));
fclose($fp);
}
// -------------------
// READ OBJECT FROM CACHE
// -------------------
function readcache()
{
$content=@file_get_contents($this->filename);
if ($content==false) return false;
$intweather = unserialize($content);
if ($intweather->cache_expires < time()) return false;
$this->source = 'cache';
$this->forecast = $intweather->forecast;
return true;
}
// -------------------
// FUNCTION PARSECACHED
// -------------------
function parsecached() {
if ($this->readcache()) return;
$this->parse();
$this->writecache();
}
} // class : end
?>