Posted by: Graeme Sutherland | 15 October, 2007

Adding RSS blog posts to a web page

If you have a blog and a regular website, often it is really nice to get headlines or a little bit of blog info into the regular website. You can put a news sidebar on a page, and give people links to go further into the blog.

Alert! This is pretty technical and assumes you know a bit about using php and html to build web pages. You don’t need to know this to be an authentic blogger.

So, here I’m going to show how to embed RSS headlines and content from your blog in another webpage. I’ll use my presencelabs.com blog as the source of the RSS and embed some headlines and content in a page on grasuth.com.

To do this, I’m going to write a few lines of PHP and use the free Magpie RSS library to parse the RSS into an easy-to-display form and then send it out as html.

Firstly, let’s sort out the feed we need. The RSS feed for presencelabs.com is http://presencelabs.com/feed/. That will produce RSS for us. You can test that works by loading it in your browser. It will either show you the RSS itself, a list of articles from the feed, or offer to subscribe you.

Now, we need to install MagpieRSS on the server, in a folder/directory where we can include it for our PHP script.

Magpie RSS can be downloaded from sourceforge where the project is hosted. Extract it onto your web server in a folder called, magpie. I’m going to make a folder called rsstools and put magpie in a folder off that called magpie.

Now, let’s make a test page. Here’s the basic html we need before we add the feed in:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>RSS Test Page</title>
</head>
<body>
<h1>RSS Test Page</h1>
<p>There will be a feed in here soon.</p>
</body>
</html>

Save this in the rsstools folder as rsstest.php. Okay.

Now, to add in the feed, we can do something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>RSS Test Page</title>
</head>
<body>
<h1>RSS Test Page</h1>
<?php
// include the magpie libs to fetch/decode RSS
require('magpie/rss_fetch.inc');
// the feed address we want to fetch
$feed = 'http://presencelabs.com/feed';
// read the feed into an object called $rss
$rss = fetch_rss($feed);
?>
<h2>
<?php
// display blog title from the rss object
print $rss->channel['title'];
?>
</h2>
<ul>
<?php
// loop through the feed items and display them
foreach ($rss->items as $item ) {
    $title = $item['title'];
    $url   = $item['link'];
    $desc = $item['description'];
    print “<li><a href=\”$url\”>$title</a> — $desc</li>”;
}
?>
</ul>
</body>
</html>

We’ve added php code to read the feed in, and a loop to pull out items from the RSS and display their title and desciption and a link back to the each of the items.

See the result here.

And that’s the basics. Magpie has a lot more available, but this will get you going. See the links below to find out more.

Useful links:

Responses

Cool Gra - I bet this comes in handy for some. Gra’s going to add juicy bits like this from time to time, so enjoy! Let us know your blogging questions and we will try and sort them. But check our FAQ first. If you email me, we can put the question and the answer up as a post. Cheers - Libby

Leave a response

Your response:

Categories