05 May

List/Display YouTube Videos With MagpieRSS

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["title"]) ?></h3>
			<a href="video.php?url=<?= $videoID ?>"><img src="http://img.youtube.com/vi/<?= $videoID ?>/0.jpg" alt="<?= $video["title"] ?>" class="left_image" /></a>
			<p><strong>Date :</strong> <?= date("d-m-Y", strtotime($video['pubdate']) ) ?></p>
			<p><?= $video->description ?></p>
			<div style="clear:both">
				<a href="video.php?url=<?= $videoID ?>">Watch it here</a><br />
				<a href="<?= $video['link'] ?>" 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["title"]) ?></h3>
		<p><strong>Date :</strong> <?= date("d-m-Y", strtotime($video['pubdate']) ) ?></p>
		<p>
			<a href="http://www.youtube.com?v=<?= $id ?>">View it on YouTube</a><br />
		</p>
		<p><?= $video["description"] ?></p>
		<div class="object">
			<object width="425" height="350">
				<param name="movie" value="http://www.youtube.com/v/<?= $id ?>">
				</param>
				<embed src="http://www.youtube.com/v/<?= $id ?>" 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.

If you liked this post, why not share it?

  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Reddit
  • RSS
  • StumbleUpon
  • Technorati
  • Twitter

Related Posts

  • Read Wordpress RSS Feeds, YouTube Video Lists, and more…
  • Linux Commands : Argument list too long
  • crazy.works
    hello ,

    i wanna make a small video sharing script , i need to show the videos in my script from youtube and google with the all data (thumbnail, the title and description),
    so i wrote a code to put the video link in the $url then i limit the video type if its youtube or google to show the video in the script and i succeed on that
    but i cant output the video thumbnail, the title and description and i dont know how can i do it

    so if that is my code

    ----------------------------------------------------------------------------------------------------------------------

    youtube ...... site_type=2 --> google*/

    $url = "http://www.youtube.com/watch?v=9IOpuGVoyCk"; /*put here the video link*/
    $parts = explode("=", $url);
    $video_id = $parts[1];

    if ($site_type == '1') {
    ?>


    <object type="application/x-shockwave-flash" width="444" height="333"
    data="http://www.youtube.com/v/">




    <object type="application/x-shockwave-flash" width="444" height="333"
    data="http://video.google.com/googleplayer.swf?docId=&hl=en&stop=true&playerMode=simple">




    ---------------------------------------------------------------------------------------------------------------------------------

    if u opened it on your browser it will show the video only without the other needed data,
    just some body help me to add function or code in it to show the thumbnail, the title and description under the video
    , resend me the code back after the editing on it and i will be thankful for that

    thanks
  • You made some good points there. I did a search on the topic and found most people will agree with your blog.
blog comments powered by Disqus