require_once './EasyRss.class.php';
$rss = EasyRss::create()->
title('GitHub')->
link('http://github.com/')->
description('GitHub is the best place to build software together. Over 4 million people use GitHub to share code.')->
pubDateByTimestamp(time())->
category('IT')->
setNamespace('xmlns:dc="http://purl.org/dc/elements/1.1/"')->
addItem( RssItem::create()->
title(EasyRss::wrapWithCDATA( htmlentities('News: ' . $title, ENT_COMPAT, 'UTF-8') ))->
link('http://github.com/blog/' . $link)->
guid($guid)->
description(EasyRss::wrapWithCDATA($description))->
custom('<dc:creator>' . $author . '</dc:creator>')->
pubDateByTimestamp($time)
)->
show(); // set rss+xml content type and prints data
Last active
April 12, 2017 13:25
-
-
Save aNNiMON/6fb9397db6404187466f to your computer and use it in GitHub Desktop.
Small library to create rss feed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once './RssItem.class.php'; | |
/** | |
* Rss-feed generator. | |
* | |
* @author aNNiMON | |
*/ | |
class EasyRss { | |
/** Create neww rss from scratch or fill by config */ | |
public static function create($channel = array()) { | |
return new EasyRss($channel); | |
} | |
/** Wrap HTML text with CDATA */ | |
public static function wrapWithCDATA($string) { | |
return '<![CDATA[' . $string . ']]>'; | |
} | |
/** Namespace of feed */ | |
private $namespace; | |
/** Channel config */ | |
private $channel; | |
/** Feeds array */ | |
private $items; | |
private function __construct($channel) { | |
$this->channel = $channel; | |
$this->items = array(); | |
$this->namespace = ''; | |
} | |
/** Optional. Defines one or more categories for the feed */ | |
public function category($category, $domain = '') { | |
$this->channel['category'][] = array('category' => $category, 'domain' => $domain); | |
return $this; | |
} | |
/** Required. Describes the channel */ | |
public function description($description) { | |
$this->channel['description'] = $description; | |
return $this; | |
} | |
/** Optional. Specifies the language the feed is written in */ | |
public function language($language) { | |
$this->channel['language'] = $language; | |
return $this; | |
} | |
/** Required. Defines the hyperlink to the channel */ | |
public function link($link) { | |
$this->channel['link'] = $link; | |
return $this; | |
} | |
/** Optional. Defines the last publication date for the content of the feed */ | |
public function pubDate($pubDate) { | |
$this->channel['pubDate'] = $pubDate; | |
return $this; | |
} | |
/** Optional. Defines the last publication date for the content of the feed */ | |
public function pubDateByTimestamp($timestamp) { | |
$this->channel['pubDate'] = date(DATE_RSS, $timestamp); | |
return $this; | |
} | |
/** Required. Defines the title of the channel */ | |
public function title($title) { | |
$this->channel['title'] = $title; | |
return $this; | |
} | |
/** Adds custom element to rss channel */ | |
public function custom($custom) { | |
$this->channel['custom'] = $custom; | |
return $this; | |
} | |
public function addItem(RssItem $feed) { | |
$this->items[] = $feed; | |
return $this; | |
} | |
public function setNamespace($namespace) { | |
$this->namespace = ' ' . $namespace; | |
return $this; | |
} | |
public function show() { | |
header("Content-Type: application/rss+xml"); | |
echo $this->build(); | |
} | |
public function build() { | |
// Add required elements | |
$out = '<?xml version="1.0" encoding="utf-8"?>' | |
. '<rss version="2.0"' . $this->namespace . '>' | |
. '<channel>' | |
. '<title>' . $this->channel['title'] . '</title>' | |
. '<link>' . $this->channel['link'] . '</link>' | |
. '<description>' . $this->channel['description'] . '</description>' | |
. '<generator>EasyRss http://annimon.com/</generator>'; | |
// Fill optional items if exists | |
if (!empty($this->channel['category'])) { | |
foreach ($this->channel['category'] as $category) { | |
$out .= '<category'; | |
if (!empty($category['domain'])) { | |
$out .= ' domain="' . $category['domain'] . '"'; | |
} | |
$out .= '>' . $category['category'] . '</category>'; | |
} | |
} | |
if (!empty($this->channel['language'])) { | |
$out .= '<language>' . $this->channel['language'] . '</language>'; | |
} | |
if (!empty($this->channel['pubDate'])) { | |
$out .= '<pubDate>' . $this->channel['pubDate'] . '</pubDate>'; | |
} | |
if (!empty($this->channel['custom'])) { | |
$out .= $this->channel['custom']; | |
} | |
// Add items | |
foreach ($this->items as $item) { | |
$out .= $item->build(); | |
} | |
$out .= '</channel></rss>'; | |
return $out; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Rss-feed item. | |
* | |
* @author aNNiMON | |
*/ | |
class RssItem { | |
/** Create rss item from scratch or fill by config */ | |
public static function create($item = array()) { | |
return new RssItem($item); | |
} | |
/** Item config */ | |
protected $item; | |
private function __construct($item) { | |
$this->item = $item; | |
} | |
/** Optional. Specifies the e-mail address to the author of the item */ | |
public function author($author) { | |
$this->item['author'] = $author; | |
return $this; | |
} | |
/** Optional. Defines one or more categories the item belongs to */ | |
public function category($category, $domain = '') { | |
$this->item['category'][] = array('category' => $category, 'domain' => $domain); | |
return $this; | |
} | |
/** Optional. Allows an item to link to comments about that item */ | |
public function comments($comments) { | |
$this->item['comments'] = $comments; | |
return $this; | |
} | |
/** Required. Describes the item */ | |
public function description($description) { | |
$this->item['description'] = $description; | |
return $this; | |
} | |
/** Optional. Allows a media file to be included with the item */ | |
public function enclosure($url, $length, $type) { | |
$this->item['enclosure'] = array( | |
'url' => $url, | |
'length' => $length, | |
'type' => $type | |
); | |
return $this; | |
} | |
/** Optional. Defines a unique identifier for the item */ | |
public function guid($guid, $isPermaLink = true) { | |
$this->item['guid'] = array( | |
'guid' => $guid, | |
'isPermaLink' => $isPermaLink | |
); | |
return $this; | |
} | |
/** Required. Defines the hyperlink to the item */ | |
public function link($link) { | |
$this->item['link'] = $link; | |
return $this; | |
} | |
/** Optional. Defines the last-publication date for the item */ | |
public function pubDate($pubDate) { | |
$this->item['pubDate'] = $pubDate; | |
return $this; | |
} | |
/** Optional. Defines the last-publication date for the item */ | |
public function pubDateByTimestamp($timestamp) { | |
$this->item['pubDate'] = date(DATE_RSS, $timestamp); | |
return $this; | |
} | |
/** Optional. Specifies a third-party source for the item */ | |
public function source($url, $title = '') { | |
$this->item['source'] = array( | |
'url' => $url, | |
'title' => $title | |
); | |
return $this; | |
} | |
/** Required. Defines the title of the item */ | |
public function title($title) { | |
$this->item['title'] = $title; | |
return $this; | |
} | |
/** Adds custom element to rss item */ | |
public function custom($custom) { | |
$this->item['custom'] = $custom; | |
return $this; | |
} | |
public function build() { | |
// Add required elements | |
$out = '<item>' | |
. '<title>' . $this->item['title'] . '</title>' | |
. '<link>' . $this->item['link'] . '</link>' | |
. '<description>' . $this->item['description'] . '</description>'; | |
// Fill optional items if exists | |
if (!empty($this->item['author'])) { | |
$out .= '<author>' . $this->item['author'] . '</author>'; | |
} | |
if (!empty($this->item['category'])) { | |
foreach ($this->item['category'] as $category) { | |
$out .= '<category'; | |
if (!empty($category['domain'])) { | |
$out .= ' domain="' . $category['domain'] . '"'; | |
} | |
$out .= '>' . $category['category'] . '</category>'; | |
} | |
} | |
if (!empty($this->item['comments'])) { | |
$out .= '<comments>' . $this->item['comments'] . '</comments>'; | |
} | |
if (!empty($this->item['pubDate'])) { | |
$out .= '<pubDate>' . $this->item['pubDate'] . '</pubDate>'; | |
} | |
if (!empty($this->item['guid'])) { | |
$guid = $this->item['guid']; | |
$out .= '<guid'; | |
if ($guid['isPermaLink'] === false) { | |
$out .= ' isPermaLink="false"'; | |
} | |
$out .= '>' . $guid['guid'] . '</guid>'; | |
} | |
if (!empty($this->item['source'])) { | |
$source = $this->item['source']; | |
$out .= '<source url="' . $source['url'] . '">' . $source['title'] . '</source>'; | |
} | |
if (!empty($this->item['enclosure'])) { | |
$enclosure = $this->item['enclosure']; | |
$out .= '<enclosure url="' . $enclosure['url'] | |
. '" length="'. $enclosure['length'] | |
. '" type="' . $enclosure['type'] . '" />'; | |
} | |
if (!empty($this->item['custom'])) { | |
$out .= $this->item['custom']; | |
} | |
$out .= '</item>'; | |
return $out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment