Building RSS feed with PHP5 XMLWriter (How to make a RSS feed)

March 2, 2013 by Asbra — 2 Comments

This is a simple class that helps you construct an RSS feed for your website using PHP5’s XMLWriter.

If you just want to download the class, here you go PHP5 XMLWriter class for RSS feeds here!

<?php
/* RSS builder class
 * Author:  Johan / Asbra.net
 * Created: 2013-02-28
 */

@date_default_timezone_set( "GMT" );

class RSS
{
  private $xml;

  private $title;
  private $link;
  private $description;
  private $copyright;
  private $language;
  private $charset;

  function __construct( $title, $link, $description = '', $copyright = '', $language = 'en-us', $charset = 'UTF-8' )
  {
    $this->xml = new XMLWriter();

    $this->title       = $title;
    $this->link        = $link;
    $this->description = $description;
    $this->copyright   = $copyright;
    $this->language    = $language;
    $this->charset     = $charset;

    $xml->openMemory();
    $xml->startDocument( '1.0', 'UTF-8' );

    $xml->startElement( 'rss' );
    $xml->writeAttribute( 'version', '2.0' );
    $xml->writeAttribute( 'xmlns:atom', 'http://www.w3.org/2005/Atom' );

    $xml->startElement( 'channel' );
    $xml->writeElement( 'title', $this->title );
    $xml->writeElement( 'link', urlencode( $this->link ) );
    $xml->writeElement( 'description', $this->description );
    $xml->writeElement( 'pubDate', date( "D, d M Y H:i:s e" ) );
    $xml->writeElement( 'language', $this->language );
    $xml->writeElement( 'copyright', $this->copyright );
  }

  function addItem( $title, $link, $description, $guid = '', $pubDate = null )
  {
    $xml->startElement( 'item' );

    $xml->writeElement( 'title', $title );
    $xml->writeElement( 'link', urlencode( $link ) );
    $xml->writeElement( 'description', $description );

    if( $guid != '' )
    {
      $xml->writeElement( 'guid', $guid );
    }

    $xml->writeElement( 'pubDate', ( $pubDate == null ? date( "D, d M Y H:i:s e" ) : $pubDate ) );

    $xml->endElement();
  }

  function getOutput()
  {
    $writer->endElement(); // </channel>
    $writer->endElement(); // </rss>
    $xml->endDocument();

    return $xml->outputMemory();
  }

  function output()
  {
    header( 'Content-type: application/rss+xml; charset=' . $this->charset );

    echo $this->getOutput();
  }
}

PHP XML RSS feed PHP5 XMLWriter

Creating RSS feed with PHP XMLWriter

Create feed.php file:

require_once( 'rss.class.php' ); // replace with the filename of the class in this post
require_once( 'wp-load.php' );

$rss = new RSS( 'My Blog', 'http://asbra.net/', 'The latest 10 posts from my blog', 'Copyright (C) ' . date('Y') . ' Asbra.net' );

$args = array(
    'posts_per_page'  => 10,
    'offset'          => 0,
    'orderby'         => 'post_date',
    'order'           => 'DESC',
    'post_status'     => 'publish' );
$posts = get_posts( $args );

foreach( $posts as $post )
{
  setup_postdata( $post );
  $rss->addItem( get_the_title(), get_permalink( $post->ID ), $post->post_excerpt, $post->guid, get_the_date( "D, d M Y H:i:s e" ) );
}

$rss->output();

Add something like this .htaccess file:

RewriteRule ^feed/$ feed.php

You can get more information about PHP5 XMLWriter here http://www.php.net/manual/en/book.xmlwriter.php

You can download the PHP create XML/RSS feed class here!

Asbra

Posts Facebook

Blogging out of many years of experience with gamehacking, programming, reverse-engineering and general tomfoolery.

2 responses to Building RSS feed with PHP5 XMLWriter (How to make a RSS feed)

  1. definition of RSS class doesn’t look correct?

    $this->xml = new XMLWriter();

    .
    .
    .
    $xml->openMemory(); /* on line 31 */
    generate following errors.

    [Sat Jun 29 22:00:06 2013] [error] [client 65.186.207.122] PHP Notice: Undefined variable: xml in /var/www/html/include/rssClass.php on line 31

    and
    [Sat Jun 29 22:00:06 2013] [error] [client 65.186.207.122] PHP Fatal error: Call to a member function openMemory() on a non-object in /var/www/html/include/rssClass.php on line 31

  2. The link to down load doesn’t work.

Leave a Reply