Nothing in cyberspace is sacred
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 )
// 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).
$feed = new SimplePie(‘http://simplepie.org/blog/feed/’);
// 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).
// 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.
[<h1>][<a href=”[<?php echo $feed->]get_permalink(); ?>]”>][<?php echo $feed->]get_title(); ?>][</a>][</h1>]
[<p>][<?php echo $feed->]get_description(); ?>][</p>]
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):
[<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>]
[<?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.
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):
That should do it.