Johan Broddfelt

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_EOL
                  . '    ' . PHP_EOL
                  . '        ' . $this->title . '' . PHP_EOL
                  . '        ' . $this::BASE_URL . 'post_feed.rss' . PHP_EOL
                  . '        ' . $this->description . '' . PHP_EOL
                  . '        en-EN' . PHP_EOL
                  . '        ' . PHP_EOL
                  ;
        $rssFeed .= $this->getItems()
                  ;
        $rssFeed .= '    ' . PHP_EOL
                  . ''
                  ;
        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 . '
' . PHP_EOL . ' Read more...'; //$imgUrl = 'http://www.johanbroddfelt.se/graphics/my_img.jpg'; //$img = '
'; $items .= ' ' . PHP_EOL . ' <![CDATA[' . $itm->title . ']]>' . PHP_EOL . ' ' . PHP_EOL . ' ' . $itm->author .'' . PHP_EOL . ' '.$url.'' . PHP_EOL . ' '.$url.'' . PHP_EOL . ' '.date('r', strtotime($itm->pubDate)).'' . PHP_EOL . ' Learn how to code' . PHP_EOL //. ' ' . PHP_EOL //. ' ' . PHP_EOL //. ' ' . PHP_EOL //. ' ' . PHP_EOL //. ' ' . PHP_EOL . ' ' . PHP_EOL; } return $items; } }

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

// post_feed.php
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.



- framework, php, rss, software

<< Send mail on new comments
Follow using RSS
Topics to categorize posts >>

Comment

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