Skip to content

Instantly share code, notes, and snippets.

@JamesxX
Last active August 29, 2015 14:22
Show Gist options
  • Save JamesxX/5604d3b839130c3b9013 to your computer and use it in GitHub Desktop.
Save JamesxX/5604d3b839130c3b9013 to your computer and use it in GitHub Desktop.
A simple PHP class for generating simple RSS feeds.
<?php
abstract class RSSGenerator{
/** The RSS Feed information */
protected $title;
protected $link;
protected $description;
protected $language;
/** The function to which all output is passed */
abstract protected function OutputCallback( $output );
/** Called when the XML version has just been declared */
abstract protected function PostXMLHeader( );
/** Called when it's time to output the items */
abstract protected function BuildItems( );
/** Generic builder function for XML tags */
private function StartXMLTag( $tag , $attributes = array() ){
$this->OutputCallback( "<$tag" );
foreach ( $attributes as $Attribute=>$Value ){
$this->OutputCallback( " " );
$this->OutputCallback( $Attribute );
$this->OutputCallback( "=\"" );
$this->OutputCallback( (string)$Value );
$this->OutputCallback( "\"" );
}
$this->OutputCallback( ">" );
}
private function EndXMLTag( $tag ){
$this->OutputCallback( "</$tag>" );
}
private function BuildXML( $tag, $attributes = array(), $inner ){
$this->StartXMLTag( $tag, $attribute );
$this->OutputCallback( $inner );
$this->EndXMLTag( $tag );
}
/** Output the XML Header for the RSS feed */
private function GenerateXMLHeader( ){
$this->OutputCallback( '<?xml version="1.0" encoding="ISO-8859-1" ?>' );
$this->PostXMLHeader( );
}
/** Add a style sheet */
protected function GenerateXMLStyle( $style ){
$this->OutputCallback( '<?xml-stylesheet type="text/css" href="'.$style.'" ?>' );
}
/** RSS feed tags */
private function StartRSSFeed( ){
$this->StartXMLTag( "rss" , array( "version"=>"2.0" ) );
$this->StartXMLTag( "channel" );
}
private function EndRSSFeed( ){
$this->EndXMLTag( "channel" );
$this->EndXMLTag( "rss" );
}
/** Item tag generator */
public function Item( $Title, $Link, $Description ){
$this->StartXMLTag( "item" );
$this->BuildXML( "title", array(), $Title );
$this->BuildXML( "link", array(), $Link );
$this->BuildXML( "description", array(), "<![CDATA[" . $Description . "]]>" );
$this->EndXMLTag( "item" );
}
/** Image tag generator */
public function Image( $Title, $URL, $Link, $Width, $Height ){
$this->StartXMLTag( "image" );
$this->BuildXML( "title", array(), $Title );
$this->BuildXML( "url", array(), $URL );
$this->BuildXML( "link", array(), $Link );
$this->BuildXML( "width", array(), $Width );
$this->BuildXML( "height", array(), $Height );
$this->EndXMLTag( "image" );
}
public function BuildInformation( ){
$this->BuildXML( "title", array(), $this->title );
$this->BuildXML( "link", array(), $this->link );
$this->BuildXML( "description", array(), $this->description );
$this->BuildXML( "language", array(), $this->language );
}
/** Call to generate the feed entirely */
public function BuildRSS( ){
$this->GenerateXMLHeader( );
$this->StartRSSFeed( );
$this->BuildInformation( );
$this->BuildItems( );
$this->EndRSSFeed( );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment