Netflix LogoA friend of mine just called and asked if I could throw together a quick piece of code for him that could get the new releases list from Netflix and display them on his website using PHP. As always, I was willing to help. Since this was something that he needed (for whatever reason), I assume that there might be others out there that would like to do the same thing. So, I’m going to share with you the same code I shared with my friend.

The first thing you need to know about getting the new releases list from Netflix is “where” to get the list. Lucky for us, Netflix makes this easy as they provide a simple RSS feed that lists all of their new releases. Here is the URL for that list.

http://rss.netflix.com/NewReleasesRSS

Well, that’s simple enough. What about Blockbuster? I know they’ve been going out of business for a while. However, they were recently bought out by Dish Network. So, if there’s a chance that they’re going to stick around a bit longer, we might as well get a list of their new releases while we’re at it. So, here is the URL for Blockbuster’s new releases list.

http://www.blockbuster.com/rss/newRelease

Now that we have the RSS feeds for both Netflix and Blockbuster’s new releases, how do we display them using PHP? A while back, I came across a nice little PHP file that included all of the required functions for handling RSS. So, by using the functions in that file, we can easily get the list of new releases from both Netflix and Blockbuster and do with them whatever we want. For my friend, he simply wanted to print the new releases on his website. So, we’re going to do the same here.

First off, you’ll need the RSS functions I just mentioned. Just like in many of my other code examples, the RSS file contains a simple function called “getTextBetweenTags”. This is a great little utility function to have. It works by passing in a text string and the tag you want to get the text from in between. For example, if you had the text “<div>Some Text</div>”, you could call the getTextBetweenTags method and pass it the complete text along with the tag “div” like this.

$output = getTextBetweenTags(“<div>Some Text</div>”, “div”);

This would return the text “Some Text”. Here is the function that makes that possible.

function getTextBetweenTags($text, $tag) {
	$StartTag = "<$tag";
	$EndTag   = "</$tag";
	
	$StartPosTemp = strpos($text, $StartTag);
	$StartPos = strpos($text, '>', $StartPosTemp);
	$StartPos = $StartPos + 1;
	
	$EndPos = strpos($text, $EndTag);
	
	if($EndPos > $StartPos)	{
		$text	= substr ($text, $StartPos, ($EndPos - $StartPos));
	} else {
		$text = '';
	}
	
	$text = str_replace('<![CDATA[', '', $text);
	$text = str_replace(']]>', '', $text);
	
	return trim($text);
}

Pretty simple to understand, huh? Next, the RSS file contains a function called “RSStoArray”. This too is a basic function, one that will excessively use the “getTextBetweenTags” function above. It basically loops thru every “<item>” in the RSS feed and calls the “getTextBetweenTags” function to get the title, link, description, author, category, comments, enclosure, guid, pubDate, and source. It then adds every occurrence of those items to an array which gets returned from the function. Here is the code for that function.

function RSStoArray($feed) {
	$content = file_get_contents ($feed);	
	$items = explode ('<item>', $content);
	array_shift($items);
	$i = 0;
	
	foreach ($items as $item) {
		$array[$i]['title']		= getTextBetweenTags($item, 'title');
		$array[$i]['link']		= getTextBetweenTags($item, 'link');
		$array[$i]['description']	= getTextBetweenTags($item, 'description');
		$array[$i]['author'] 		= getTextBetweenTags($item, 'author');
		$array[$i]['category'] 		= getTextBetweenTags($item, 'category');
		$array[$i]['comments'] 		= getTextBetweenTags($item, 'comments');
		$array[$i]['enclosure'] 	= getTextBetweenTags($item, 'enclosure');
		$array[$i]['guid'] 		= getTextBetweenTags($item, 'guid');
		$array[$i]['pubDate'] 		= getTextBetweenTags($item, 'pubDate');
		$array[$i]['source'] 		= getTextBetweenTags($item, 'source');
		$i++;
	}
	
	return $array;
}

So far so good. The next thing you are going to need to do is to put those 2 functions to use. To begin, you will need to create a variable which will store the array returned from the RSStoArray function which we will call by passing it the URL to the RSS feed we are wanting to get the list of new releases from.

$rssfeed = “http://www.blockbuster.com/rss/newRelease”;
$rssfeed = “http://rss.netflix.com/NewReleasesRSS”;

Once you have the array of feed items, you will need to loop thru those and print out the pieces you want. This piece is easy, so I’m going to leave it for the final source code below. And, that’s all folks. You now have a way to print the new releases from both Netflix and Blockbuster using PHP. Until next time, happy coding.

<?php
function getTextBetweenTags($text, $tag) {
	$StartTag = "<$tag";
	$EndTag   = "</$tag";
	
	$StartPosTemp = strpos($text, $StartTag);
	$StartPos = strpos($text, '>', $StartPosTemp);
	$StartPos = $StartPos + 1;
	
	$EndPos = strpos($text, $EndTag);
	
	if($EndPos > $StartPos)	{
		$text	= substr ($text, $StartPos, ($EndPos - $StartPos));
	} else {
		$text = '';
	}
	
	$text = str_replace('<![CDATA[', '', $text);
	$text = str_replace(']]>', '', $text);
	
	return trim($text);
}

function RSStoArray($feed) {
	$content = file_get_contents ($feed);	
	$items = explode ('<item>', $content);
	array_shift($items);
	$i = 0;
	
	foreach ($items as $item) {
		$array[$i]['title']		= getTextBetweenTags($item, 'title');
		$array[$i]['link']		= getTextBetweenTags($item, 'link');
		$array[$i]['description']	= getTextBetweenTags($item, 'description');
		$array[$i]['author'] 		= getTextBetweenTags($item, 'author');
		$array[$i]['category'] 		= getTextBetweenTags($item, 'category');
		$array[$i]['comments'] 		= getTextBetweenTags($item, 'comments');
		$array[$i]['enclosure'] 	= getTextBetweenTags($item, 'enclosure');
		$array[$i]['guid'] 		= getTextBetweenTags($item, 'guid');
		$array[$i]['pubDate'] 		= getTextBetweenTags($item, 'pubDate');
		$array[$i]['source'] 		= getTextBetweenTags($item, 'source');
		$i++;
	}
	
	return $array;
}

#$rssfeed = "http://www.blockbuster.com/rss/newRelease";
$rssfeed = "http://rss.netflix.com/NewReleasesRSS";
$rsslinks = RSStoArray($rssfeed);
foreach ($rsslinks as $rsslink){
	print "<small><a href=\"".$rsslink['link']."\">".$rsslink['title']."</a></small><br>".$rsslink['description']."<br><br>\n";
}
?>

Related Posts

Tagged with:  

Leave a Reply