Creating a PHP rss feed parser class


If you thought how to build rss feed parser you should think first think about what php xml parse you should use. If we examine the parser that we have built in then you will find expat, simple xml and xml dom manupulation. In order to parse a feed from a url you should first validate the feed url , get the contents of the xml through file_get_contents, and loop through nodes of xml and get the nodes values.
So here are steps the in order to build our class:
1. Validate url
2. Get contents using file_get_contents

3.Set the limit of how many items to return
4. Parse results. We should replace the spaces of the node values.

Class Description
$url – the url of the feed that will be parsed.
$doc – the DOM Document that will load the xml parsed from url.
$limit – Limits the number of items to be retrieved.
$isValidUrl – Verifies if the link is valid.
$items – the RSS items that would be returned.

Initialize the url of the feed and all class variables with empty variables. We initialize the limit to 5 items.
SetLimit – sets the limit of the items to be parsed
ValidateUrl – validates the url of the rss feed. It checks if the feed url begins with ‘https://’
ExecuteRssParser – fetches the feed url and loads into a php variable. After that it loads the xml and parses the rss nodes values and returns the results into the variable ‘feeds’.

class RssFeedParser
private $url, $doc, $limit;
public $isValidUrl, $items;
public function __construct($url) {
$this->url = $url;
$this->items = array();
$this->doc = new DOMDocument();
$this->limit = 5;
private function ValidateUrl()
if (stristr( $this->url, 'https://') === FALSE)
return false;
return true;
public function SetLimit($limit)
$this->limit = $limit;
public function ExecuteRssParser()
$this->isValidUrl = $this->ValidateUrl();
if ($this->isValidUrl)
  $xmlText = preg_replace("/>\s+</", "><", file_get_contents($this->url));
  if ($this->doc->getElementsByTagName('item'))
	  $count = 0;
	  foreach ($this->doc->getElementsByTagName('item') as $node) {
		$itemRSS = array ( 
		  'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
		  'description' => $node->getElementsByTagName('description')->item(0)->nodeValue,
		  'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
		  'pubDate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
		array_push($this->items, $itemRSS);
		if ($count>=$this->limit)

You can easy modificate this class in order to fit your needs. Good luck!!!

Recent Posts
Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt

Start typing and press Enter to search