PHP Rss Reader

Krstevski 0 Tallied Votes 906 Views Share

Hello friends, this code/snippet is nothing special, just a simple rss reader class written in PHP.

Many times in my projects I use this script to get an RSS feed from some website and now i decided to share with you.

This script can be useful for your projects or your education.

If you have any suggest or better solution you can share or you can correct me. :)

Here an example "How to use this script":

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">

	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title> PHP Library - RssAgent, RSS Reader </title>
    <style type="text/css">
		body{font-family: Arial, Helvetica, sans-serif; font-size: 14px; width: 980px; margin: 0 auto; padding-top: 30px; background-color: #EEEEEE;}
		._link { font-size: 16px; }
		.pDate { color: #888888; }
		.giud_link { text-decoration: none; color: #00AA00; font-size: 12px; }
		.content { background-color: #FFFFFF; padding: 25px; border: 1px #BBBBBB solid; }



<div class="content">


		$RSS = new RssAgent( "" );
		for( $i = 0; $i < sizeof($RSS->title); $i++ )
			echo '<a class="_link" href="' . $RSS->link[$i] . '" target="_blank">' . $RSS->title[$i] . '</a> - '; 
			echo '<span class="pDate">' . $RSS->pubDate[$i] . '</span> <br />';
			echo $RSS->description[$i] . "<br />";
			echo '<i> Category: ' . $RSS->category[$i] . "</i> <br />"; 
			echo '<a class="giud_link" href="' . $RSS->guid[$i] . '" target="_blank">' . $RSS->guid[$i] . "</a> <br />";
			echo "<br />";




	*	Free Library - RssAgent
	*	Name: RssAgent
	*	Description: RSS Reader (RSS v2.0,
	*	-> Reading an RSS feed from a website
	*	Author: Damjan Krstevski - SkyDriver
	*	License: Freeware, you can use, copy, edit, redistribute and etc...
	/*   ******************************  Start Class  ******************************   */
	class RssAgent {
	/* ***  Declaring variables  *** */
	var $title = array(),
		$link = array(),
		$description = array(),
		$category = array(),
		$guid = array(),
		$pubDate = array();
	/* ***  End of Declaring variables  *** */
		/* ***  Construct *** */
	   	function __construct( $urlFeed = NULL ) {
			$content = @file_get_contents( $urlFeed ); // Get the content from URL
			$x = new SimpleXmlElement( $content ); // Parse XML using SimpleXmlElement Class
			foreach( $x->channel->item as $entry ) // Fill the arrays with the rss feed
				array_push( $this->title, $entry->title ); // Append title
				array_push( $this->link, $entry->link ); // Append link
				array_push( $this->description, strip_tags( $entry->description ) ); // Strip HTML tags and append description
				array_push( $this->category, $entry->category ); // // Append category
				array_push( $this->guid, $entry->guid ); // // Append guid
				array_push( $this->pubDate, $entry->pubDate ); // Append pubDate
		/* ***  End of Construct *** */
	/*   ******************************  End Class  ******************************   */
Lapixx 2 Junior Poster in Training

Simple and clean script. One suggestion though, instead of all the array_push functions, why not loop trough every attribute from $entry? I've seen some RSS feeds that use something other than 'description' for some reason.

Krstevski 7 Junior Poster

The script is written according to standards of RSS 2.0 to allow simple using:

$MyClass = new RssAgent("");
echo $MyClass->element[$index];

If someone element is not in RSS then they attributes will be filled with empty string.

e.g if the rss contains just title and link, then the others elements (description, category, guid, pubDate) will have same number of element like a 'title' but their value will be empty string.

However, you can edit the script as you want according to your needs and thanks for your suggestion, maybe will be useful for someone. :)

Member Avatar for ovidiu_b13

Hello, I'm a beginner in PHP, and I don't understand all this just yet, but I've tried to copy the code exactly and just input my url, and it returns blank. to text what so ever. This is my URL.

What should I do?

SarahSt 0 Newbie Poster

Very useful script and it could be powerful with a few modifications.

1) Character limit of the description (ending with a full word). Many RSS feeds are just endless...
2) Adding an image tag, thus grabbing either a) the first available img (from the description) or b) the first enclosure img (rss feeds are different as to where they place their img) or c) if no img then display a custom img - logo - or nothing.

Would that be possible?
If so you would have created one of the most powerful and easy-to-use RSS parsers.
Even so, thanks for the work so far. The script is great...

SarahSt 0 Newbie Poster

3) Also specific words or phrases (like "readmore", "follow us on twitter", etc.) should be taken out from the feed therefore a str_replace method would be necessary. That would also help us with 2) since in order to pass an img tag we need to delete strip_tags from the existing code, therefore str_replace would be useful to remove tags like <strong>, <div> etc. that we may not need.

Member Avatar for diafol


Krstevski wrote this script 3 years ago and was last seen on Daniweb a year ago.

SarahSt 0 Newbie Poster

I want to believe that there are other people too that are interested in a good and simple RSS parser, like myself. I am looking right now to optimize Krstevski's code and I will make publicly available whatever I manage to do. But since I am not an expert PHP programmer anyone who can help is more than welcome...Krstevski or no Krstevski...

Member Avatar for diafol

That's fine - just giving you a heads up that k doesn't seem to be around any more. Good luck with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.