List/Display YouTube Videos With MagpieRSS

Posted on May 5, 2008 in PHP | 3 comments


Late last year, Google made a switch from the old YouTube API and integrated it into it’s GData API. While an understandable move, it does somewhat annoy me that the old REST method is being depreciated in August 2008 because I don’t particularly feel like updating my code 🙂

Alas, it will happen soon, and so I decided to upgrade some pages that used it. As I don’t heavily rely on the video services, and just use it in it’s simplest form for displaying a list of Videos, I made something quick, rather than install the Zend GData Library Google recommends.

Since GData outputs RSS, I used MagpieRSS to painlessly port my old code and have an updated code up and running. Here’s two scripts that may help with your changeover:

The following displays a list of videos posted by a particular user. To change list of videos, you can simply change the URL of page according to the GData API, and as long as it returns valid RSS, a list will be displayed.

// get the feed using magpieRSS
    include('rss_fetch.inc');
	$feed = fetch_rss("http://gdata.youtube.com/feeds/api/users/username/uploads?alt=rss");

?>
<?php	
if(count($feed->items)>0){
	foreach ($feed->items as $video){
		//Get the video ID
			preg_match("/http://gdata.youtube.com/feeds/api/videos/(([a-zA-Z0-9]|-|_)*)/", $video['guid'], $videoIdMatches);
			$videoID = $videoIdMatches[1];
		?>
		<div class="video">
			<h3><?= ucfirst($video&#91;"title"&#93;) ?></h3>
			<a href="video.php?url=<?= $videoID ?>"><img src="http://img.youtube.com/vi/<?= $videoID ?>/0.jpg" alt="<?= $video&#91;"title"&#93; ?>" class="left_image" /></a>
			<p><strong>Date :</strong> <?= date("d-m-Y", strtotime($video&#91;'pubdate'&#93;) ) ?></p>
			<p><?= $video->description ?></p>	
			<div style="clear:both">			
				<a href="video.php?url=<?= $videoID ?>">Watch it here</a><br />
				<a href="<?= $video&#91;'link'&#93; ?>" target="_blank">Watch it on YouTube</a><br />
			</div>	
			<br />	
		</div>			
		<?php
	}
}else{
	echo "<h3>No Videos Found</h3>";
}

The following shows one video and embeds the video so you can play it. This could be the code that the previous code links to.

$id= $_GET['youtube_vid'];
// get the feed using magpieRSS
include('rss_fetch.inc');
$feed = fetch_rss("http://gdata.youtube.com/feeds/api/users/username/uploads?alt=rss");
$found = false;
foreach ($feed->items as $item){
	if(strstr($item['guid'], $id)){
		$found = true;
		break;
	}
}

if($found){
	$video = $item;
	?>
	<div class="video">
		<h3><?= ucfirst($video&#91;"title"&#93;) ?></h3>
		<p><strong>Date :</strong> <?= date("d-m-Y", strtotime($video&#91;'pubdate'&#93;) ) ?></p>
		<p>
			<a href="http://www.youtube.com?v=<?= $id ?>">View it on YouTube</a><br />
		</p>
		<p><?= $video&#91;"description"&#93; ?></p>			
		<div class="object">
			<object width="425" height="350">
				<param name="movie" value="http://www.youtube.com/v/<?= $id ?>">
				</param>
				" type="application/x-shockwave-flash" width="425" height="350"></embed>
			</object>
		</div>
	</div>			
	<?php
}else{
	echo "<h3>No Videos Found</h3>";	    	
}

There is one major limitation with MagpieRSS is that attributes aren’t supported, so quite a few pieces of information aren’t retreivable (Video Durations, Image Locations, Ratings). Hopefully MagpieRSS will soon have that sorted, as it’s on their list. For that you need to either use the Zend GData Library, or parse the XML manually with DOMDocument or SimpleXML. For more information, I suggest you check out the Developer’s Guide on Google.