class.rss.inc.php

PHP-Klasse um einen simplen validen RSS-Feed zu erstellen.

Beispiel

$rss = new rss("Test RSS", "http://example.com/rss", "Nur ein Test");
$rss->item("Hallo Welt", "http://example.com/rss#hallo-welt", "Hallo Otto", 1350416443);
$rss->item("Hallo Welt2", "http://example.com/rss#hallo-welt2", $rss->cData("Hallo<br>Otto2"), 1350416450);
$rss->output();

Klasse

class.rss.inc.php
<?php
	/**
	 * class.rss.inc.php 
	 *
	 * @version	0.1 build 20121012
	 * @license	GPL 2 (http://www.gnu.org/licenses/gpl.html)
	 * @author	Heiko Barth <https://www.heiko-barth.de/contact>
	 */
 
	class rss {
		private $header;
		private $footer;
		private $item;
		private $inputEncoding;
 
		/**
		 * @param string $title RSS title
		 * @param string $link RSS link
		 * @param string $description RSS description
		 * @param string $additional optional channel additions
		 * @param string $inputEncoding optional encoding of input strings
		 */
		public function __construct($title, $link, $description, $additional = '', $inputEncoding = 'ISO-8859-1') {
			$scriptURL  = ($_SERVER["HTTPS"] == "on") ? "https" : "http";
			$scriptURL .= "://" . $_SERVER["HTTP_HOST"] . $_SERVER["SCRIPT_NAME"];
			$this->header  = '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n\n";
			$this->header .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">' . "\n";
			$this->header .= "<channel>\n";
			$this->header .= "<title>" . $title . "</title>\n";
			$this->header .= "<link>" . $link . "</link>\n";
			$this->header .= "<description>" . $description . "</description>\n";
			$this->header .= ($additional != "") ? $additional . "\n" : '';	# optional
			$this->header .= "<pubDate>" . date("r") . "</pubDate>\n";	# optional
			$this->header .= '<atom:link href="' . $scriptURL . '" rel="self" type="application/rss+xml" />' . "\n"; # optional
			$this->footer  = "</channel>\n</rss>";
			$this->inputEncoding = $inputEncoding;
		}
 
		/**
		 * Use this function, to sanitize markup or other things that would brick xml.
		 *
		 * @param string $markup 
		 * @return string sanitized markup
		 */
		public function cData($markup) {
			$cd = '<![CDATA[';	// CDATA start
			$dc = ']]>';		// CDATA stop
			$markup = str_ireplace(array($cd, $dc), "", $markup);
			return $cd . $markup . $dc;
		}
 
		/**
		 * Add item to RSS feed
		 *
		 * @param string $title item title
		 * @param string $link item link
		 * @param string $description item description
		 * @param string $date item date as unix timestamp
		 * @param string $additional optional item additions
		 */
		public function item($title, $link, $description, $date, $additional = '') {
			$item  = "<item>\n";
			$item .= "<title>" . $title . "</title>\n";			# optional
			$item .= "<description>" . $description . "</description>\n";
			$item .= "<link>" . $link . "</link>\n";			# optional
			$item .= "<pubDate>" . date("r", $date) . "</pubDate>\n"; 	# optional
			$item .= "<guid>" . $link . "</guid>\n";			# optional
			$item .= ($additional != "") ? $additional. "\n" : '';		# optional			
			$item .= "</item>\n";
			$this->item[] = $item;
		}
 
		/**
		 * Get generated RSS feed
		 *
		 * @return string UTF-8 encoded RSS feed
		 */
		public function get() {
			foreach ($this->item as $item) {
				$items .= $item;
			}
			return iconv($this->inputEncoding, "UTF-8//IGNORE", $this->header . $items . $this->footer);
		}
 
		/**
		 * Output generated RSS feed
		 */		
		public function output() {
			header("Content-Type: text/xml;charset=utf-8");
			echo $this->get();
		}		
	}