Skip to content

Instantly share code, notes, and snippets.

@rakuishi
Created September 2, 2014 08:11
Show Gist options
  • Save rakuishi/75d2086d540136956c35 to your computer and use it in GitHub Desktop.
Save rakuishi/75d2086d540136956c35 to your computer and use it in GitHub Desktop.
<?php
class RSS2 {
private $channel = array();
private $item = array();
public function setTitle($value) {
$this->channel['title'] = $value;
}
public function setLink($value) {
$this->channel['link'] = $value;
}
public function setDescription($value) {
$this->channel['description'] = $value;
}
public function setLanguage($value) {
$this->channel['language'] = $value;
}
public function setLastBuildDate($value) {
$this->channel['lastBuildDate'] = $this->toRFC822($value);
}
public function setItem($title, $link, $description, $pubDate) {
$this->item[] = array(
'title' => $title,
'link' => $link,
'description' => $description,
'pubDate' => $this->toRFC822($pubDate),
'guid' => $link,
);
}
public function output() {
$dom = new DomDocument('1.0', 'UTF-8');
$rssNode = $dom->appendChild($dom->createElement('rss'));
$rssNode->setAttribute('version', '2.0');
$channelNode = $rssNode->appendChild($dom->createElement('channel'));
foreach ($this->channel as $key => $value) {
$channelNode->appendChild($dom->createElement($key, $value));
}
foreach ($this->item as $item) {
$itemNode = $channelNode->appendChild($dom->createElement('item'));
foreach ($item as $key => $value) {
$itemNode->appendChild($dom->createElement($key, $value));
}
}
$dom->formatOutput = true;
return $dom->saveXML();
}
private function toRFC822($value) {
return (string)date(DATE_RFC2822, strtotime($value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment