Home    Articles

 

How to get youtube video details

 

Getting details of a video from youtube is quite simple.
All you have to do is to get the video url (example: http://www.youtube.com/watch?v=d3GgFvm29Ig ).

In this video url you can see that all videos have an ID. http://www.youtube.com/watch?v=d3GgFvm29Ig
Let’s get this id using the following php code:

<?php
   parse_str( parse_url( $videoUrl, PHP_URL_QUERY ), $vars );
   echo $vars['v'];
?>

Now the ID is stored in $vars['v'].
Using json we need to call a youtube api that will return details about our video based on video ID.

 $json = json_decode( file_get_contents("http://gdata.youtube.com/feeds/api/videos/".$vars['v']."?v=2&prettyprint=true&alt=jsonc") );

$json variable is an object that will store all data that we need about this video, like name, description, lenght.

Let’s have displayed all this data

    $string = '<div class="youtubeVideo" >
		<a class="fancybox-media" href="'.$videoUrl.'">
		  <img src="http://img.youtube.com/vi/'.$vars['v'].'/0.jpg" height="72" align="left" >
		</a>
		<a class="fancybox-media" href="'.$videoUrl.'">'.$json->data->title.'</a><br/><br/>
		<span>'.substr($json->data->description,0,60).'...</span>
	      </div>';
   echo $string;

This is how will look our code

video-youtube
Bellow is the style for this video.

.youtubeVideo
{
	float:left;
}
.youtubeVideo a
{
	color:#fff;
	font-size: 12px;
	text-decoration: none;
}
.youtubeVideo a:hover
{
	color: #08E;
}
.youtubeVideo img
{
	margin-right:7px;
}
.youtubeVideo span
{
	font-style: italic;
	font-size: 10px;
	color:#ccc;
	margin-top: 4px;
 
}

 

by