Earlier I had released an RSS Feed Reader (
Check here)... But it had some problems
This time I have totally recoded it and it also uses
SimpleXML for parsing the feed, so it shouldn't have many compatibility issues with different types of RSS feeds.
Here is an example of how to use it:
<?php
require_once('Reader.class.php');
$reader = new Reader('http://mschat.net/rss.php?feed=blog', './cache');
echo '<pre>';
print_r($reader->return_items());
echo '</pre>';
?>
Then you will get an array with the RSS/Atom feed stuff
Enjoy
Update: 9-26-2009
I have updated the Reader class to version 3.0, which adds support for Atom feeds. Here is a list of changes:
- User agent changed to PHP/Class Feed Reader v3
- New parameter added to Reader::Reader() (Constructor) and method Reader::read_feed() called $force_update
- Added new methods Reader::is_atom() and Reader::is_rss()
DocumentationConstructorReader Object Reader([string $feed_url = null[, string $cache_dir = null[, int $cache_length = 3600[, bool $force_update = false]]]]);
string $feed_url - The feed url you want to have read into an array (Can be RSS or Atom).
string $cache_dir - The path to the directory where you want feeds to be cached.
int $cache_length - How long the feed should be cached before being updated, in seconds, defaults to 1 hour.
bool $force_update - If you want to force the feed to be updated even if its cache hasn't timed out.
public void set_feed(string $feed_url);
string $feed_url - The feed url you want to have read into an array (Can be RSS or Atom).
public bool set_cache(string $cache_dir);
string $cache_dir - The path to the directory where you want feeds to be stored. If it doesn't exist, it will attempt to create it.
returns bool - Returns true if the caching directory exists or false if it doesn't and couldn't create it.
private bool cache_exists([string $cache_dir = null]);
string $cache_dir - The directory to be checked for caching. If none is supplied, it will use the one internally, if any.
returns bool - Returns whether or not the directory exists/created successfully, false if it failed or wasn't a directory.
public int set_cache_length([int $cache_length = 3600]);
int $cache_length - How long you want the cache to be used until it is considered expired. Defaults to 3600 seconds (1 hour).
returns int - Returns the new cache length.
public bool read_feed([bool $force_update = false]);
bool $force_update - If you want to force the feed to be updated whether or not it is expired, set to true.
returns bool - If the feed was successfully fetched/read from file and parsed without error, true is returned, otherwise, false.
private string get_feed(string $url[, int $num_redirects = 0[, bool $force_update = false]]);
string $url - The url of the feed which you want to be fetched (Either from the web or a cached file, if enabled).
int $num_redirects - The number of redirects which have already occurred. This parameter is used internally and should not be set. It is
only used when fsockopen is used.
bool $force_update - Whether or not you want to force the feed to be fetched again even if the cache has yet to expire.
returns string - Returns the feed which has either been fetched via the web, or cache.
public array return_items([int $num_items = 0]);
int $num_items - The number of items (RSS) or entries (Atom) to be read from the feed. If set to 0, all will be read and returned.
returns array - Returns an array containing all the items/entries of the feed. The indices of the items/entries totally depend on the feed which was read.
public bool is_cached();
returns bool - Returns true if the feed you want read is cached or not (Changed only after read_feed() is called, either directly or through the constructor).
public bool is_read();
returns bool - Tells you whether or not the there is a feed which is read internally from the feed url you have supplied to read_feed() or the constructor.
public int cache_length();
returns int - Returns how long (in seconds) the cache for the feed can not be updated before being expired.
public bool is_atom();
returns bold - Whether or not the feed you supplied was an Atom feed. Returns null if you have yet to call read_feed() or if you gave no feed url to the constructor.
public bool is_rss();
returns bold - Returns the opposite of is_atom() (Unless null, of course) and has the same prerequisites as is_atom();