The YouTube Data API allows client applications to retrieve and update YouTube content in the form of Google Data API feeds. The client application can use the YouTube Data API to fetch video feeds, comments, responses, and playlists, as well as modify this information and to upload new video content to the site.

There is a awesome Client Library in PHP distributed by Zend is a part of zend framework which is very effective and powerful class to retrieve that from Google. This can be easily integrated with Codeigniter.
Here I share a Class which is very useful to Grad data from Youtube using Gdata class with Codeigniter.

<?php

class Youtube extends Controller
{
	public $layout="default";
	
	function __construct()
	{
		parent::Controller();
		$this->load->library("zend");
		$this->zend->load("Zend/Gdata/YouTube");
		
		$youtube = new Zend_Gdata_YouTube();
		$this->lang->load("resource");
		$this->load->helper("text");
	}

	function index()
	{
	
		//$youtube = new Zend_Gdata_YouTube();
		
		//Grab Video data
		$videoEntry = $youtube->getVideoEntry('video_code');
		
		//Grab Channel data
		$this->getAndPrintUserUploads("youtube_channel_name");
		
		$this->load->view('youtube_view', $data);
	}
	
	function getAndPrintUserUploads($userName)                    
	{     
		  $youtube = new Zend_Gdata_YouTube();
		  $youtube->setMajorProtocolVersion(2);
		  $this->printVideoFeed($youtube->getuserUploads($userName),$page=1,$page_counter=1);
	}  
		
	function printVideoFeed($videoFeed)
	{
		  $data['no_sidebar'] = true;
		  $count = 1;
		  
		  foreach ($videoFeed as $videoEntry) 
		  {
			
				//$this->printVideoEntry($videoEntry);
				$video_info[$count]['title'] = $videoEntry->getVideoTitle();
				$video_info[$count]['id']    = $videoEntry->getVideoId();
				$video_info[$count]['desc']  = $videoEntry->getVideoDescription();
				$video_info[$count]['tags']  = implode(", ", $videoEntry->getVideoTags());
				$video_info[$count]['updated']  = $videoEntry->getUpdated();
				$video_info[$count]['category']  = $videoEntry->getVideoCategory();				
				
				$count++;
		  }
		//pre($video_info);
		
		$data['video_info'] = $video_info;
		$this->load->view("resources/youtube_list_view",$data);
	}
	
		
	
	function printVideoEntry($videoEntry) 
	{
	  // the videoEntry object contains many helper functions
	  // that access the underlying mediaGroup object
	  echo 'Video: ' . $videoEntry->getVideoTitle() . "<br>";
	  echo 'Video ID: ' . $videoEntry->getVideoId() . "<br>";
	  echo 'Updated: ' . $videoEntry->getUpdated() . "<br>";
	  echo 'Description: ' . $videoEntry->getVideoDescription() . "<br>";
	  echo 'Category: ' . $videoEntry->getVideoCategory() . "<br>";
	  echo 'Tags: ' . implode(", ", $videoEntry->getVideoTags()) . "<br>";
	  echo 'Watch page: ' . $videoEntry->getVideoWatchPageUrl() . "<br>";
	  echo 'Flash Player Url: ' . $videoEntry->getFlashPlayerUrl() . "<br>";
	  echo 'Duration: ' . $videoEntry->getVideoDuration() . "<br>";
	  echo 'View count: ' . $videoEntry->getVideoViewCount() . "<br>";
	  echo 'Rating: ' . $videoEntry->getVideoRatingInfo() . "<br>";
	  echo 'Geo Location: ' . $videoEntry->getVideoGeoLocation() . "<br>";
	  echo 'Recorded on: ' . $videoEntry->getVideoRecorded() . "<br>";
	  
	  // see the paragraph above this function for more information on the 
	  // 'mediaGroup' object. in the following code, we use the mediaGroup
	  // object directly to retrieve its 'Mobile RSTP link' child
	  foreach ($videoEntry->mediaGroup->content as $content) {
		if ($content->type === "video/3gpp") {
		  echo 'Mobile RTSP link: ' . $content->url . "<br>";
		}
	  }
  
  echo "Thumbnails:<br>";
  $videoThumbnails = $videoEntry->getVideoThumbnails();

		  foreach($videoThumbnails as $videoThumbnail) {
			echo $videoThumbnail['time'] . ' - ' . $videoThumbnail['url'];
			echo ' height=' . $videoThumbnail['height'];
			echo ' width=' . $videoThumbnail['width'] . "<br>";
		  }
	}

}

?>

Zend provides some cool library classes for Youtube.

There is another way to grab youtube data from youtube Channel that is grabbing from API feed. Which is another easy process.


/**
 * Function: youtube data grabber
 *
 * @description :
 * @param  $ : video code, url type (embed/url)
 * @return : data array
 * @author : Mamun.
 * @last -modified-by: Mamun.
 */
if (! function_exists('youtube_data_grabber' ))
{

		function youtube_data_grabber($video_code, $link_type = "embed")
		{
				if ($video_code != '')
				{
					if ($link_type == "embed")
					{
						$splited_data = explode("=",$video_code);
						$video_unique_code = substr(strrchr($splited_data[4],"/"),1,-strlen(strrchr($splited_data[4],"&")));

					}
					else if ($link_type == "url")
					{
						$splited_data = explode("=",$video_code);
						$video_unique_code = substr($splited_data[1],0,-strlen(strrchr($splited_data[1],"&")));
					}
					else
					{
						return;
					}

						// set feed URL
						$feedURL = 'http://gdata.youtube.com/feeds/api/videos/'.$video_unique_code;

						// read feed into SimpleXML object
						$sxml = simplexml_load_file($feedURL);

					return $sxml;
				}

		}
}

Advertisements