PHP XML Writing RSS Feeds - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP XML Writing RSS Feeds - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, June 16, 2019

PHP XML Writing RSS Feeds

PHP XML


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();

Discussion

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:

         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:

         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:

         $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.



No comments:

Post a Comment

Post Top Ad