ubuntuusers.de

_lokalen_ RSS-Feed benutzen

Status: Ungelöst | Ubuntu-Version: Nicht spezifiziert
Antworten |

dauerbaustelle

Avatar von dauerbaustelle

Anmeldungsdatum:
2. Juli 2007

Beiträge: 1936

Hallo,
ich hab mir ein Script gebaut, das "Neue Antworten"-Mails aus dem Forum automatisch in einen RSS-Feed einträgt und diese danach löscht.
Damit will ich ein überfülltes Mailkonto vermeiden.

Alerdings habe ich jetzt mit bekommen, dass ich RSS (=XML)-Dateien parsen muss - allerdings hält mein PC keinen "DauerApache" aus.

Welche Alternativen gibts oder kann ich das einfach via PHP parsen?

Gruß

Isegrim

Avatar von Isegrim

Anmeldungsdatum:
29. Dezember 2006

Beiträge: 535

Hallo Dauerbaustelle,

hab das hier im Einsatz:

<?php

include('rdf_parser.php');

//Parsen, 5 Minuten lang Cachen

$parser = new rdf_parser('http://www.phpbb.de/rdf/rdf-news.php', 'simple_news.cache.xml', (5 * 60));

$parser->parse();



//Ausgabe

echo '<h1>' . $parser->channel['title'] . '</h1>';

echo 'URL: <a href="' . $parser->channel['link'] . '">' . $parser->channel['link'] . '</a><br>';

echo $parser->channel['description'];



foreach($parser->items as $item) {

	echo '<h2>' . $item['title'] . '</h2>';

	echo 'URL: <a href="' . $item['link'] . '">' . $item['link'] . '</a><br>';

	echo $item['description'];

}

?>

Der Code für den Parser rdf_parser.php:

<?php

function cache_file($url, $cachefile, $cachetime = 900) {

    $stat = @stat($cachefile);

    if ($stat[9] < (time() - $cachetime)) {

		$fp_url = fopen($url, 'r');

		$fp_cache = fopen($cachefile, 'w');

		while($str = fread($fp_url, 1024)) {

			fwrite($fp_cache, $str);

		}

		fclose($fp_url);

		fclose($fp_cache);

		return false;

	}

	else {

		return true;

	}

}



class rdf_parser {

	var $file;

	var $cache_file = '';

	var $cache_time = 0;

	var $cache_used = false;



	var $content = '';



	var $channel = array();

	var $items = array();



	function rdf_parser($file, $cache_file='', $cache_time=0) {

		$this->file = $file;

		$this->cache_file = $cache_file;

		$this->cache_time = $cache_time;

		//$this->parse();

	}



	function match($text, $elements) {

		$ret = array();

		foreach($elements as $element) {

			if(preg_match("#<$element>(.*?)</$element>#si", $text, $matches)) {

				$ret[$element] = $matches[1];

			}

		}

		return $ret;

	}



	function parse() {

		$file = $this->file;

		if($this->cache_file && $this->cache_time) {

			$this->cache_used = cache_file($this->file, $this->cache_file, $this->cache_time);

			$file = $this->cache_file;

		}

		$this->content = implode('', file($file));



		if(preg_match('#<channel>(.*?)</channel>#si', $this->content, $matches)) {

			$this->channel = $this->match($matches[1], array('title', 'description', 'link'));

		}

		preg_match_all('#<item>(.*?)</item>#si', $this->content, $matches, PREG_PATTERN_ORDER);

		foreach($matches[1] as $item) {

			$this->items[] = $this->match($item, array('title', 'description', 'link'));

		}

	}



	function template_output($block_name) {

		global $template;

		$row = 0;

	    foreach($this->items as $item) {

	        $template->assign_block_vars($block_name, array(

	            'ROW_CLASS' => ($row++%2 == 0 ? 'row1' : 'row2'),

	            'TITLE' => $item['title'],

	            'TEXT' => $item['description'],

	            'URL' => $item['link']

	        ));

	    }

	}

}



function show_rdf($url, $block_name, $cache_file='', $cache_time=0) {

	$parser = new rdf_parser($url, $cache_file, $cache_time);

	$parser->parse();

	$parser->template_output($block_name);

}

?>

Viele Grüße

dauerbaustelle

(Themenstarter)
Avatar von dauerbaustelle

Anmeldungsdatum:
2. Juli 2007

Beiträge: 1936

Danke!
Aber ist das dann auch so, dass ich das als dynamisches Lesezeichen im Firefox einbinden kann?

dauerbaustelle

(Themenstarter)
Avatar von dauerbaustelle

Anmeldungsdatum:
2. Juli 2007

Beiträge: 1936

*push*

Antworten |