Johan Broddfelt
/* Comments on code */

RSSfeed for the masses

Not all visitors want to submit their mail address. But many use an RSS-feed to follow what is new on the web. So of course we should provide one of those for our posts. We start out by doing some uppdates to our database.

// We want to be able to show the user who made the post
ALTER TABLE `post` ADD `user_id` INT NOT NULL AFTER `message`;

// We also need to add the real name of that user in order to show it...
ALTER TABLE `user` ADD `name` VARCHAR(20) NOT NULL AFTER `password`, ADD `surname` VARCHAR(20) NOT NULL AFTER `name`;

Now we can move on with the RSS class. Basically this class should render a valid RSS-feed for us. I admit as it is now it has some half implemented code for handling multiple feeds. I might try to clean that up later. But right now I'll just let it be like this. It works for posts and that is the goal for today.

// classes/Rss.php
<?php
class Rss {
    public $title = 'Learn how to code RSS';
    public $description = 'Johan Broddfelt show tips and trix about development of business applications for the web.';
    public $source = 'post';
    
    const BASE_URL = 'http://www.johanbroddfelt.se/';
    
    
    function getFeed() {
        $rssFeed  = '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">' . PHP_EOL
                  . '    <channel>' . PHP_EOL
                  . '        <title>' . $this->title . '</title>' . PHP_EOL
                  . '        <link>' . $this::BASE_URL . 'post_feed.rss</link>' . PHP_EOL
                  . '        <description>' . $this->description . '</description>' . PHP_EOL
                  . '        <language>en-EN</language>' . PHP_EOL
                  . '        <atom:link href="' . $this::BASE_URL . 'post_feed.rss" rel="self" type="application/rss+xml" />' . PHP_EOL
                  ;
        $rssFeed .= $this->getItems()
                  ;
        $rssFeed .= '    </channel>' . PHP_EOL
                  . '</rss>'
                  ;
        return $rssFeed;
    }
    
    private function getItems() {
        $objArr = array();
        switch ($this->source) {
            case 'post':
                $p = new Post();
                $pArr = $p->fetchArray(' WHERE publish <> '0000-00-00 00:00:00' ORDER BY id DESC');
                foreach ($pArr as $itm) {
                    $obj = new stdClass();
                    $obj->id = $itm->id;
                    $obj->title = $itm->title;
                    $obj->linkTitle = $itm->linkTitle();
                    $obj->text = $itm->summary;
                    $obj->pubDate = $itm->publish;
                    $u = new User((int)$itm->userId);
                    $obj->author = $u->mail . ' (' . $u->name . ' ' . $u->surname . ')';
                    $objArr[] = $obj;
                }
                break;
            default:
                return false;
        }
        foreach ($objArr as $itm) {
            $url = $this::BASE_URL . $itm->linkTitle . '-' . $itm->id;
            $description = $itm->text . '<br>' . PHP_EOL
                         . '                <a href="'.$url.'">Read more...</a>';
            
            //$imgUrl = 'http://www.johanbroddfelt.se/graphics/my_img.jpg';
            //$img = '<img src="' . $imgUrl . '"/><br>';
            $items .= '        <item>' . PHP_EOL
                    . '            <title><![CDATA[' . $itm->title . ']]></title>' . PHP_EOL
                    . '            <description><![CDATA[' . $img . $description .']]></description>' . PHP_EOL
                    . '            <author>' . $itm->author .'</author>' . PHP_EOL
                    . '            <link>'.$url.'</link>' . PHP_EOL
                    . '            <guid>'.$url.'</guid>' . PHP_EOL
                    . '            <pubDate>'.date('r', strtotime($itm->pubDate)).'</pubDate>' . PHP_EOL
                    . '            <source url="http://johanbroddfelt.com/post_feed.rss">Learn how to code</source>' . PHP_EOL
                    //. '            <enclosure url="' . $imgUrl . '" length="5000" type="image/jpeg"/>' . PHP_EOL
                    //. '            <media:content url="' . $imgUrl . '" medium="image" height="396" width="704">' . PHP_EOL
                    //. '                <media:description type="plain"><![CDATA[-]]></media:description>' . PHP_EOL
                    //. '                <media:copyright><![CDATA[...]]></media:copyright>' . PHP_EOL
                    //. '            </media:content>' . PHP_EOL
                    . '        </item>' . PHP_EOL;
        }

        return $items;
    }
}

Now we just need to create a file that uses this class and outputs the feed.

// post_feed.php
<?php
header("Content-type: application/rss+xml");
require_once('global.php');

$rss = new Rss();
echo $rss->getFeed();

But it looks a lot better if we have the rss-feed named like post_feed.rss. We could solve this in two ways. Either tell the web server that .rss also should be executed as .php files or we add a Rule to our .htaccess file. I choose the later.

//.htaccess
RewriteRule ^post_feed.rss post_feed.php

Now we can add the link to our web page so that our users can find it, perhaps with a rss-icon image.


<a href="http://johanbroddfelt.se/post_feed.rss"><img src="graphics/rss.png" title="RSS-feed"></a>

- Framework, PHP, RSS

Follow using RSS

<< Send mail on new comments Topics to categorize posts >>

Comment

Name
Mail (Not public)
Send mail uppdates on new comments
0 comment