PHP Cookbook by David Sklar & Adam Trachtenberg

PHP Cookbook by David Sklar & Adam Trachtenberg

Author:David Sklar & Adam Trachtenberg
Language: eng
Format: epub
ISBN: 9781449363758
Publisher: O’Reilly Media, Inc.
Published: 2014-10-26T16:00:00+00:00


Figure 12-2. php.announce RSS channel information

See Also

The MagpieRSS homepage; more information on RSS at Wikipedia.

Writing RSS Feeds

Problem

You want to generate RSS feeds from your data. This will allow you to syndicate your content.

Solution

Use this class:

class rss2 extends DOMDocument { private $channel; public function __construct($title, $link, $description) { parent::__construct(); $this->formatOutput = true; $root = $this->appendChild($this->createElement('rss')); $root->setAttribute('version', '2.0'); $channel= $root->appendChild($this->createElement('channel')); $channel->appendChild($this->createElement('title', $title)); $channel->appendChild($this->createElement('link', $link)); $channel->appendChild($this->createElement('description', $description)); $this->channel = $channel; } public function addItem($title, $link, $description) { $item = $this->createElement('item'); $item->appendChild($this->createElement('title', $title)); $item->appendChild($this->createElement('link', $link)); $item->appendChild($this->createElement('description', $description)); $this->channel->appendChild($item); } } $rss = new rss2('Channel Title', 'http://www.example.org', 'Channel Description'); $rss->addItem('Item 1', 'http://www.example.org/item1', 'Item 1 Description'); $rss->addItem('Item 2', 'http://www.example.org/item2', 'Item 2 Description'); print $rss->saveXML();



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.