Archive for May, 2011

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
Variables
$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.

Constructor
Initialize the url of the feed and all class variables with empty variables. We initialize the limit to 5 items.
Methods
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 ‘http://’
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, 'http://') === FALSE)
{
return false;
}
else
{
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));
  $this->doc->loadXML($xmlText);
 
  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);
		$count++;
		if ($count>=$this->limit)
		{
			break;
		}
	  }
  }
}
}
}
?>

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

In order to parse the youtube video id you can use the parse_url and parse_str php functions. You should first parse the url and all it’s components using the php function parse_url. The components we are interested in for parsing the youtube video id you should use the ‘query’ components where are stored all the query strings parameters. We then have to use the parse_str function in order to parse the query string in variables.

The input parameter of the function is the youtube url.
The ouput paramter is the youtube video id.
The php function that returns the youtube video id is:

function GetYouTubeVideoId($youtubeUrl)
{
$link = parse_url($youtubeUrl);
parse_str($link['query'], $qs);
$youtubeId = $qs['v'];
return $youtubeId;
}
Back to top