SimplePie 1.3.1 is now available. Upgrade now!

SimplePie Documentation.  Learn how to use this thing.  It's way better than going to school.

You are here: Documentation » Tips, Tricks, Tutorials, and Screencasts » How do I get a thumbnail from a YouTube feed?

How do I get a thumbnail from a YouTube feed?

SimplePie 1.0 introduces full support for Media RSS – a popular set of tags geared for media files, and are added to RSS and Atom feeds.

:!: This tutorial assumes that you're already familiar with using SimplePie, including looping through items. This is only sample code, and you should not create real pages using the (horrid) HTML generated by this example.

Compatibility

  • Supported in SimplePie 1.0.
  • Code in this tutorial should be compatible with PHP 4.3 or newer, and should not use PHP short tags, in order to support the largest number of PHP installations.

Code source

<?php
// Make sure that SimplePie is loaded
require_once('simplepie.inc');
 
// Let's simplify this with the shorter syntax. http://simplepie.org/wiki/reference/simplepie/start
$feed = new SimplePie('http://youtube.com/rss/global/top_favorites.rss');
 
// Make sure the content is being served out to the browser properly.
$feed->handle_content_type();
 
foreach ($feed->get_items() as $item)
{
	// As long as an enclosure exists...
	if ($enclosure = $item->get_enclosure())
	{
		// Display the thumbnail as an image and link it back to the YouTube page, and adding the video's title as a tooltip for the link.
		echo '<a href="' . $item->get_permalink() . '" title="' . $item->get_title() . '"><img src="' . $enclosure->get_thumbnail() . '" /></a>';
	}
}
?>

tutorial/how_do_i_get_a_thumbnail_from_a_youtube_feed.txt · Last modified: 2011/03/05 19:56 (external edit)