954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

PHP Rss Reader

0
By Krstevski on Jun 1st, 2010 4:07 am

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

	<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; }
	</style>

</head>

<body>

<div class="content">

	<?php

		include("RssAgent.php");
		$RSS = new RssAgent( "http://www.daniweb.com/external.php?type=RSS2" );
		
		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> ';
			echo $RSS->description[$i] . "";
			echo '<i> Category: ' . $RSS->category[$i] . "</i> "; 
			echo '<a class="giud_link" href="' . $RSS->guid[$i] . '" target="_blank">' . $RSS->guid[$i] . "</a> ";
			echo "";
		}

	?>

</div>

</body>
</html>
<?php

	/*
	*	
	*	Free Library - RssAgent
	*	
	*	Name: RssAgent
	*	Description: RSS Reader (RSS v2.0, http://cyber.law.harvard.edu/rss/rss.html)
	*	-> 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  ******************************   */
	
?>

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.

Lapixx
Junior Poster in Training
52 posts since May 2010
Reputation Points: 12
Solved Threads: 2
 

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

$MyClass = new RssAgent("http://rss_feed_link.com");
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. :)

Krstevski
Junior Poster
110 posts since May 2009
Reputation Points: 17
Solved Threads: 5
 

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. http://protineretvest.blogspot.com/feeds/posts/default

What should I do?

ovidiu_b13
Light Poster
27 posts since Sep 2010
Reputation Points: 13
Solved Threads: 1
 

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: