InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Writing RSS Feeds in PHP
PHP

Writing RSS Feeds in PHP

InfinityCoder December 8, 2016

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

Use this class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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();

RSS is XML, so you can leverage all the XML-generation features of the DOM extension. The code in the Solution extends the DOMDocument class to build up a DOM tree by creating elements and appending them in the appropriate structure.
The class constructor sets up the <rss> and <channel> elements. It takes three arguments— the channel title, link, and description:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
}

Inside the method, you call the parent::__construct() method to invoke the actual DOMDocument::__construct(). Now you can begin building up the document.

First, set the formatOutput attribute to true. This adds indentation and carriage returns to the output, so it’s easy to read.
From there, create the document’s root element, rss, and set its version attribute to 2.0, because this is an RSS 2.0 feed.
All the actual data lives inside a channel element underneath the rss node, so the next step is to make that element and also to set its title, link, and description child elements.
That data comes from the arguments passed to the constructor. It’s set using a handy feature of the createElement() method, which lets you specify both an element’s name and a text node with data in one call.

This is a PHP extension to the DOM specification. Last, the channel element is saved for easy access later on.
With the main content defined, use the addItem() method to add item entries:

1
2
3
4
5
6
7
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);
}

Because item elements contain the same data as the channel, this code is almost identical to what appears in the constructor.
Although a title, link, and description are required elements of the channel, they are actually optional in the item. The only requirement of an item is that it contains either a title or a description.

That’s it. For simplicity, this code requires all three elements.

Likewise, it doesn’t provide a way to add in additional channel or item elements, such as the date the item was published or a GUID that uniquely identifies the item.
But 43 lines later, the basic RSS 2.0 class is finished. Use it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$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();
 
<?xml version="1.0"?>
<rss version="2.0">
  <channel>
   <title>Channel Title</title>
   <link>http://www.example.org</link>
   <description>Channel Description</description>
   <item>
     <title>Item 1</title>
     <link>http://www.example.org/item1</link>
     <description>Item 1 Description</description>
   </item>
   <item>
     <title>Item 2</title>
     <link>http://www.example.org/item2</link>
     <description>Item 2 Description</description>
   </item>
</channel>
</rss>

Create a new instance of the rss2 class and pass along the channel data. Then call its addItem() method to add individual items to the channel.

Once you’re finished, you can convert the class to XML by using the parent DOMDocument::saveXML() method.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Checking If an Object Is an Instance of a Specific Class in PHP
You want to check if an object is an instance …

Checking If an Object Is an Instance of a Specific Class in PHP

Tuning Error Handling in PHP
You want to alter the error-logging sensitivity on a particular …

Tuning Error Handling in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap