Fishbone22
Hallo zusammen :)
- 20 April 2006
- 253
- 4
Hallo,
Ich würde gerne RSS Feeds in meine Seite einbauen. Wie geht das?
Mfg, Fishbone22
Ich würde gerne RSS Feeds in meine Seite einbauen. Wie geht das?
Mfg, Fishbone22
Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
<?php
/**
* rdfparser
* class to parse newsfeeds into arrays
* @author clemens krack
* @link <a href="https://www.tripdown.de" target="_blank">https://www.tripdown.de</a>
* @access public
**/
class rdfparser {
var $_items; // array the items
var $_may; // array what may be done
var $_act; // string current active
var $_index; // integer current index
var $_url; // url to open
/**
* rdfparser::rdfparser()
*
* @param $url url of the feed
* @return void
**/
function rdfparser($url)
{
$this->_url = $url;
}
/**
* rdfparser::parse()
* parses a newsfeed an returns an array containing the items.
* @return array
**/
function parse()
{
$this->_items = array();
$this->_index = 0;
$this->_may['parse'] = false;
$parser = xml_parser_create();
xml_set_object($parser, $this);
xml_set_element_handler($parser, "_startElement", "_endElement");
xml_set_character_data_handler($parser, "_charHandler");
$fp = fopen($this->_url, "r");
while(!feof($fp)) {
$line = fgets($fp, 4096);
xml_parse($parser, $line);
}
fclose($fp);
xml_parser_free($parser);
return $this->_items;
}
function _startElement($parser, $name, $attrs)
{
// allow parsing chardata as soon as an element is opened
$this->_may['char'] = true;
if ($name=="ITEM") {
// allow parsing as soon as an item element was opened
$this->_may['parse'] = true;
// one more item -> increment index
$this->_index++;
$this->_items[$this->_index] = Array('title' => '', 'link' => '', 'description' => '');
} else if ($name=="TITLE") {
// current active: title
$this->_act = "TITLE";
} else if($name=="LINK") {
// current active: link
$this->_act = "LINK";
} else if($name=="DESCRIPTION") {
// current active: description
$this->_act = "DESCRIPTION";
} else {
// unknown tag, don't allow adding chardata
$this->_may['char'] = false;
}
$this->_act = strtolower($this->_act);
}
function _endElement($parser, $name)
{
if($name=="ITEM") {
// item tag closed: parsing not allowed
$this->_may['parse'] = false;
} elseif($name=="TITLE" || $name=="LINK" || $name="DESCRIPTION") {
// datatag closed, we don't want different chardata
$this->_may['char'] = false;
}
}
function _charHandler($parser, $data)
{
$data = trim($data);
if(!$this->_may['char'] OR !$this->_may['parse']) {
return;
}
if (isset($this->_items[$this->_index][$this->_act])) {
$this->_items[$this->_index][$this->_act] .= $data;
} else {
$this->_items[$this->_index][$this->_act] = $data;
}
}
}
?>
<?php
Header('Content-Type: text/plain');
require('rdfparser.class.php');
$rdf = new rdfparser('https://www.n24.de/rss/index.php?rubrik=home');
$items = $rdf->parse();
print_r($items);
?>