Complete Geek Boutique

Nothing in cyberspace is sacred

Use SimplePie as a standalone without a module in Drupal 5/6

This is an update to a post I did last year about using SimplePie as a standalone entity in Drupal.

Why would you want to do that?

Well, modules that use SimplePie as part of their coding reconfigure it to make it work with their module. Some even publish feed items as nodes – and that’s fine if that’s what you want to do.

But what if you just want to put a simple feed in a block or a page without all the baggage from a module? Sorry, folks, those modules won’t help you do that. Their sole reason for existence is to take SimplePie and mold it into their code – not use SimplePie by itself.

However, you can get around that.

It only takes a few minor adjustments to make SimplePie work for you in Drupal as a standalone entity.

This SimplePie method outlined here is, of course, separate from the feed aggregator that comes with Drupal, so it will NOT clash with that module. Please note: You don’t have to disable Drupal’s feed aggregator to use the standalone method we’re about to discuss.

Let’s get started. Go ahead and download SimplePie, and extract it to your desktop, and leave it there for now. Let’s take some steps now:

First, inside your main Drupal folder, create two folders: “cache” and “php”.  Give the “cache” folder 777 permissions so it is web-writable. This folder will be used, of course, to cache RSS feeds.

Inside the php folder, whose folder permissions can be 755, put the “simplepie.inc” file that you downloaded. That’s it for “installing”SimplePie in your Drupal folder.

In Drupal, you have a couple of choices now: Either create a block to hold the RSS feed, or create a page to hold the RSS feed. No matter your choice, this is where your RSS feed will come in.

Let’s assume you want to create a block filled with an RSS feed (either your OWN feed, or a feed from someone like, say, the official SimplePie blog thingy).

Now that you’ve created a “cache” folder in the base Drupal folder, we’ll now need to “add a new block”, and fill the “Body” block with PHP code that defines the RSS feed. Remember to choose “php” as the “input format.”

Below is some example code, which supposes you have created the folders in the paragraphs above (and, yes, it works as-is). The feed can include formatting, so change it if you wish. If you copy and paste the code below, do NOT include the words “CODE” and “END OF CODE”.

NOTE: Remove the square brackets throughout the code (just use search and replace) before pasting the code where you want it. Don’t even get me started by asking me why I used the square brackets here. ( You can get an unbracketed version here )

CODE

[<?php

// Make sure SimplePie is included. You may need to change this to match the location of simplepie.inc (See note 1 below, after this code).
require_once(‘../../php/simplepie.inc’);

$feed = new SimplePie(‘http://simplepie.org/blog/feed/&#8217;);

// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn’t change it).
$feed->]handle_content_type();

// Let’s begin our XHTML webpage code.  The DOCTYPE is supposed to be the very first thing, so we’ll keep it on the same line as the closing-PHP tag.
?>][<div class=”header”>]
[<h1>][<a href=”[<?php echo $feed->]get_permalink(); ?>]”>][<?php echo $feed->]get_title(); ?>][</a>][</h1>]
[<p>][<?php echo $feed->]get_description(); ?>][</p>]
[</div>]

[<?php
/*
Here, we’ll loop through all of the items in the feed, and $item represents the current item in the loop.
*/
foreach ($feed->]get_items() as $item):
?>]

[<div class=”item”>]
[<h2>][<a href=”[<?php echo $item->]get_permalink(); ?>]”>][<?php echo $item->]get_title(); ?>][</a>][</h2>]
[<p>][<?php echo $item->]get_description(); ?>][</p>]
[<p>][<small>]Posted on [<?php echo $item->]get_date(‘j F Y | g:i a’); ?>][</small>][</p>]
[</div>]

[<?php endforeach; ?>]

END OF CODE

If all you want is a quick-and-dirty feed, the code above will work right out of the box. If it doesn’t, see Note 1 below. See the SimplePie Wiki documentation pages for tips and tricks.

You’re done.

Note 1: This code has been thoroughly tested, but your host provider’s server may throw you a curve. You may, for example, see an error like this: Fatal error: require_once() [function.require]: Failed opening required ‘../../php/simplepie.inc’ (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in xxxxx.xxx/xxx/xxx.

In that case, here’s the solution: In the line of code in the example above, changethe location of where the server will be sure to “see” simplepie.inc. For example: /home/yourusername/public_html/php/simplepie.inc.

Thus, in our code snippet higher up in this article, you would change the location to read (this is only an example, but you get the idea):

require_once(‘/home/kittylitter/php/simplepie.inc)’

That should do it.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Information

This entry was posted on January 2, 2009 by in Content Management Systems, Free RSS programs, Php Scripts and tagged , , , .
%d bloggers like this: