Build a MySpace video widget

Build a MySpace video widget

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on October 16, 2009

In this tutorial we’re going to take an RSS feed from the top videos page on MySpace and make a widget to display the latest videos using jQuery pagination, ajax and a spot of PHP to parse the feed. The basic logic of what is going on is simple. We parse an XML file echo all the results, then we use the quick paginate plugin to display a select number of results with the option to browse pages.

Lets first take a look at the PHP page where we use CURL to parse the feed. You’ll need to make sure that your server has PHP5. Right at the top of the code we have the RSS feed source file stored in the variable $file, then the process section of code deals with the CURL request saving the result in the variable $data. We then use the Simple XmlElement to display the specific xml nodes in the right place.

vids2

The PHP

	//source
	$file = 'http://mediaservices.myspace.com/services/rss.ashx?fuseaction=vids.charts&RSSAction=VideoCharts';

	//process
	$ch = curl_init($file);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	$data = curl_exec($ch);
	curl_close($ch);

	//execute
	try
	{

	$i=0;

		$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
		function parseRSS($xml) {

			foreach($xml->channel->item as $entry) {
				$namespaces = $entry->getNameSpaces(true);
				$media = $entry->children($namespaces['media']);
				$myspace = $entry->children($namespaces['myspace']);
				$description = $entry->description;

	    $length = strlen($description);

	   if($length > 20){
	   $description = substr($description, 0, 20)."...";
	}

				if($entry->title) {
						if($odd = $i%2){$class='odd';}else{$class='even';}
					echo "<div class='vids ".$class."'>";
					echo '<div class="left"><a class="ms-video" target="_top" href="'.$entry->guid.'"><img width="100" border="0" src="'.$media->thumbnail->attributes()->url.'" alt="'.$entry->title.'" /></a></div>';
					echo '<div class="right"><div class="video-title"><a class="ms-video" target="_top" href="'.$entry->guid.'"><span>'.$media->title.'</span></a></div>';

					echo '<div class="video-description">'.$description.'</div>';
				    echo  $myspace->statistics->attributes()->plays.' Plays</div>';

					echo '<div class="clear"></div>';
						echo "</div>";
				}

					$i++;
			//end foreach
			}

		}

		if(isset($doc->channel->item)) {
			parseRSS($doc);
		} else {
			#no videos;
			exit;
		}
	} catch (Exception $e) {
		echo 'Bad XML file.';
		exit;
	}

The JavaScript

This must be the simplest JavaScript widget I’ve ever written. The basis of this code is to load in the results of an ajax request in to a div then with the help of the quick paginate jQuery plugin.You can see below that you if you want to adjust the number of results on each page just change the 3 to what ever you like.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script src="quickpaginate.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">

$(document).ready(function(){
$('#content').hide();
$('#content').load('videofeed.php', function(){
	$('#content').fadeIn('slow');
$('.vids').quickpaginate({ perpage: 3, pager : $("#pagination") });
 });
});
</script>

You can use any RSS feed for this widget, you’ll just need to adjust the PHP file to accept a different feed.

demodownload


Recent shares

More tutorials from Papermashup

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.