PHP Youtube Tools

Current Version: 1.1
Price: FREE – $1.99

PHP Youtube Tools is a set of php functions designed to aid in integrating Youtube with a website. If you have a lot of videos and use Youtube as your streaming provider you’ve likely taken one of two approaches; 1) Manually maintain an on-site archive of videos through manually created pages or posts. 2) Directing visitors to your Youtube channel. Neither approach is desirable as option 1 creates a tedious process where you not only post videos and their details to Youtube but you have to manually create and maintain video content on your site, while option 2 directs users away from the site you’ve spent so much time creating and building a user base for. PHP Youtube Tools eliminates the issue by providing a framework to easily retrieve your Youtube stream (and individual video details). It even includes a function which automatically retrieves your Youtube stream, generates the html/layout for display on a website, and paginates results so as not to overwhelm end users.


Personal Edition
Free!
  • Free for personal use.
  • Freely extended or modified.
  • Copyright notice must remain intact.
  • Commercial use prohibited.
Premium Edition
$1.99
  • Commercial use permitted.
  • Freely extended or modified.
  • Supports continued development!
  • No copyright notice.


Quantity:


Features
  • Function to retrieve Youtube video stream with individual video details.
  • Function to retrieve individual Youtube video details by Video ID.
  • Function to retrieve Youtube video stream and generate HTML/layout for paginated display.
  • Example’s for using the functions and viewing videos are included.

For a complete list of changes to PHP Youtube Tools click here



Not interested in the whole package? No problem! See below for the core functions — yours to use in any application!

/* #####################################################
	FUNCTION get_feed_data();
	
	Variables
		$username - Value should be the username of the youtube user's feed you wish to retrieve

	Returned Data
		Data type: Array of Array

		Access
			[index][key]

		Keys / Definitions
		'id' = Youtube video ID
                'title' = Video title
                'description' = Video description
                'date' = Video post date
                'length' = Video length in seconds
                'rating' = Average video rating
                'viewcount' = Total number of video views
                'commentsCount' = Total number of comments
                'watchURL' = URL to view the video on Youtube
                'commentsURL' = API URL for the videos comments
   ##################################################### */

function get_feed_data($userName){

	//get youtube video feed array
	$sxml=simplexml_load_file('http://gdata.youtube.com/feeds/api/users/'.$userName.'/uploads?orderby=updated');
	$youtubeFeed = array();

	foreach ($sxml->entry as $entry) {

		//get media: nodes
		$media = $entry->children('http://search.yahoo.com/mrss/');

		//get video url
		$watchURL = $media->group->player->attributes();
		$watchURL = $watchURL['url'];

		//get yt:duration node for video length (in seconds)
		$length = $media->children('http://gdata.youtube.com/schemas/2007');
		$length = $length->duration->attributes();
		$length = $length['seconds'];

		//get yt:stats node for viewer statistics
		$viewCount = $entry->children('http://gdata.youtube.com/schemas/2007');
		$viewCount = $viewCount->statistics->attributes();
		$viewCount = $viewCount['viewCount'];

		//get gd:comments node for comment count & comments url
		$gd = $entry->children('http://schemas.google.com/g/2005');
		if ($gd->comments->feedLink) {
			$gd = $gd->comments->feedLink->attributes();
			$commentsURL = $gd['href'];
			$commentsCount = $gd['countHint'];
		}

		//get gd:rating node for avg video ratings
		$rating = $entry->children('http://schemas.google.com/g/2005');
		if ($rating->rating) {
			$rating = $rating->rating->attributes();
			$rating = $rating['average'];
		} else {
			$rating = 0;
		}

		//create video array to return to user
		$videoEntry = array (
		  'id' => end(explode("/", $entry->id)),
		  'title' => $entry->title,
		  'description' => $entry->content,
		  'date' => explode("-", $entry->published),
		  'length' => $length,
		  'rating' => $rating,
		  'viewcount' => $viewCount,
		  'commentsCount' => $commentsCount,
		  'watchURL' => $watchURL,
		  'commentsURL' => $commentsURL
		  );
		$videoEntry['date'][2]=substr($videoEntry['date'][2], 0, 2);
		array_push($youtubeFeed, $videoEntry);
	}

	//return videos array
	return $youtubeFeed;

}
/* #####################################################
        FUNCTION get_video_data();   
                 
        Variables
		$videoID - The ID of the Youtube video you wish to retrieve data for
                
        Returned Data
                Data type: Array
                  
                Keys / Definitions
                'title' = Video title 
                'description' = Video description
                'date' = Video post date
                'length' = Video length in seconds
                'rating' = Average video rating
                'commentsCount' = Total number of comments
                'watchURL' = URL to view the video on Youtube
                'commentsURL' = API URL for the videos comments
   ##################################################### */
function get_video_data($videoID){

	//get youtube video feed array
	$sxml=simplexml_load_file('https://gdata.youtube.com/feeds/api/videos/'.$videoID.'?v=2');

	//get media: nodes
	$media = $sxml->children('http://search.yahoo.com/mrss/');

	//get video url
	$watchURL = $media->group->player->attributes();
	$watchURL = $watchURL['url'];

	//get yt:duration node for video length (in seconds)
	$length = $media->children('http://gdata.youtube.com/schemas/2007');
	$length = $length->duration->attributes();
	$length = $length['seconds'];

	//get gd:comments node for comment count & comments url
	$gd = $sxml->children('http://schemas.google.com/g/2005');
	if ($gd->comments->feedLink) {
		$gd = $gd->comments->feedLink->attributes();
		$commentsURL = $gd['href'];
		$commentsCount = $gd['countHint'];
	}

	//get gd:rating node for avg video ratings
	$rating = $sxml->children('http://schemas.google.com/g/2005');
	if ($rating->rating) {
		$rating = $rating->rating->attributes();
		$rating = $rating['average'];
	} else {
		$rating = 0;
	}

	//create video array to return to user
	$videoEntry = array (
          'title' => $sxml->title,
          'description' => $media->group->description,
          'date' => explode("-", $sxml->published),
          'length' => $length,
          'rating' => $rating,
          'commentsCount' => $commentsCount,
          'watchURL' => $watchURL,
          'commentsURL' => $commentsURL
        );
	$videoEntry['date'][2]=substr($videoEntry['date'][2], 0, 2);

	//return videos array
	return $videoEntry;
}