  <?php
  		function getYoutubeId($ytURL) 
		{
			$urlData = parse_url($ytURL);
			//echo '<br>'.$urlData["host"].'<br>';
			if($urlData["host"] == 'www.youtube.com') // Check for valid youtube url
 			{
				$ytvIDlen = 11;	// This is the length of YouTube's video IDs
 
				// The ID string starts after "v=", which is usually right after 
				// "youtube.com/watch?" in the URL
				$idStarts = strpos($ytURL, "?v=");
 
				// In case the "v=" is NOT right after the "?" (not likely, but I like to keep my 
				// bases covered), it will be after an "&":
				if($idStarts === FALSE)
					$idStarts = strpos($ytURL, "&v=");
				// If still FALSE, URL doesn't have a vid ID
				if($idStarts === FALSE)
					die("YouTube video ID not found. Please double-check your URL.");
 
				// Offset the start location to match the beginning of the ID string
				$idStarts +=3;
 
				// Get the ID string and return it
				$ytvID = substr($ytURL, $idStarts, $ytvIDlen);
 
				return $ytvID;
			}
			else
			{
				//echo 'This is not a valid youtube video url. Please, give a valid url...';
				return 0;
			}
 
		}
   
   
    // function to parse a video <entry>
    function parseVideoEntry($youtubeVideoID) 
    {      
      $obj= new stdClass;
      
      // set video data feed URL
    	$feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $youtubeVideoID;

    	// read feed into SimpleXML object
    	$entry = simplexml_load_file($feedURL);
      
      // get nodes in media: namespace for media information
      $media = $entry->children('http://search.yahoo.com/mrss/');
      $obj->title = $media->group->title;
      $obj->description = $media->group->description;
      
      // get video player URL
      $attrs = $media->group->player->attributes();
      $obj->watchURL = $attrs['url']; 
      
      // get video thumbnail
      $attrs = $media->group->thumbnail[0]->attributes();
      $obj->thumbnailURL = $attrs['url']; 
            
      // get <yt:duration> node for video length
      $yt = $media->children('http://gdata.youtube.com/schemas/2007');
      $attrs = $yt->duration->attributes();
      $obj->length = $attrs['seconds']; 
      
      // get <yt:stats> node for viewer statistics
      $yt = $entry->children('http://gdata.youtube.com/schemas/2007');
      $attrs = $yt->statistics->attributes();
      $obj->viewCount = $attrs['viewCount']; 
      
      // get <gd:rating> node for video ratings
      $gd = $entry->children('http://schemas.google.com/g/2005'); 
      if ($gd->rating) { 
        $attrs = $gd->rating->attributes();
        $obj->rating = $attrs['average']; 
      } else {
        $obj->rating = 0;         
      }
        
      // get <gd:comments> node for video comments
      $gd = $entry->children('http://schemas.google.com/g/2005');
      if ($gd->comments->feedLink) { 
        $attrs = $gd->comments->feedLink->attributes();
        $obj->commentsURL = $attrs['href']; 
        $obj->commentsCount = $attrs['countHint']; 
      }

      return $obj;      
    }   
    
	

	$youtubeVideoLink = $_REQUEST['videoID'];
	//$youtubeVideoLink = 'http://tutsbucket.com/2010/11/css3-drop-down-menu/'; 
	$videoId = getYoutubeId($youtubeVideoLink); 
	
	if($videoId == '0')
	{
	echo 'This is not a valid youtube video url. Please, give a valid url...';
	} 
	else
	{
		$videoInfo = parseVideoEntry($videoId);
		
		// display main video record
    	//echo "</table>";
    
    	//echo '<br><br>';
    	// read 'video comments' feed into SimpleXML object
    	// parse and display each comment
    	//if ($videoInfo->commentsURL && $videoInfo->commentsCount > 0) 
    	//{
      	//$commentsFeed = simplexml_load_file($videoInfo->commentsURL);    
      	//echo "<tr><td colspan=\"2\"><h3>" . $commentsFeed->title . 
      	//"</h3></td></tr>\n";
      	//echo "<tr><td colspan=\"2\"><ol>\n";
      	//foreach ($commentsFeed->entry as $comment) 
   	 }
    	echo ''.$videoInfo->title;
    ?>
