Get Youtube all information

Posted: December 15, 2012 in Uncategorized

<!DOCTYPE html
PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN””&gt;
<html xmlns=”; xml:lang=”en” lang=”en”>
<title>Retrieving video details</title>
img {
padding: 2px;
margin-bottom: 15px;
border: solid 1px silver;
td {
vertical-align: top;
function getYoutubeId($ytURL)
$urlData = parse_url($ytURL);
//echo ‘<br>’.$urlData[“host”].'<br>’;
if($urlData[“host”] == ‘’) // 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
// “” 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;
//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 = ‘; . $youtubeVideoID;

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

// get nodes in media: namespace for media information
$media = $entry->children(‘;);
$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(‘;);
$attrs = $yt->duration->attributes();
$obj->length = $attrs[‘seconds’];

// get <yt:stats> node for viewer statistics
$yt = $entry->children(‘;);
$attrs = $yt->statistics->attributes();
$obj->viewCount = $attrs[‘viewCount’];

// get <gd:rating> node for video ratings
$gd = $entry->children(‘;);
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(‘;);
if ($gd->comments->feedLink) {
$attrs = $gd->comments->feedLink->attributes();
$obj->commentsURL = $attrs[‘href’];
$obj->commentsCount = $attrs[‘countHint’];

return $obj;

$youtubeVideoLink = ‘;;
//$youtubeVideoLink = ‘;;
$videoId = getYoutubeId($youtubeVideoLink);

if($videoId == ‘0’)
echo ‘This is not a valid youtube video url. Please, give a valid url…’;
$videoInfo = parseVideoEntry($videoId);

// display main video record
echo “<br><br><table>\n”;
echo “<tr>\n”;
echo “<td><a href=\”{$videoInfo->watchURL}\”>
<img src=\”$videoInfo->thumbnailURL\”/></a></td>\n”;
echo “<td><a href=\”{$videoInfo->watchURL}\”>{$videoInfo->title}</a>
echo sprintf(“%0.2f”, $videoInfo->length/60) . ” min. | {$videoInfo->rating}
user rating | {$videoInfo->viewCount} views<br/>\n”;
echo $videoInfo->description . “</td>\n”;
echo “</tr>\n”;
//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 .
echo “<tr><td colspan=\”2\”><ol>\n”;
foreach ($commentsFeed->entry as $comment)
echo “<li>” . $comment->content . “</li>\n”;
echo “</ol></td></tr>\n”;
echo “</table>”;

echo “<h2>Basic Video Information</h2>”;
echo ‘<br>’;
echo ‘<b>Title of video:</b> ‘.$videoInfo->title;
echo ‘<br><br>’;
echo ‘<b>Thumbnail link of video:</b> ‘.$videoInfo->thumbnailURL;
echo ‘<br><br>’;
echo ‘<b>Youtube video link:</b> ‘.$videoInfo->watchURL;
echo ‘<br><br>’;
echo ‘<b>Video description:</b> ‘.$videoInfo->description;
echo ‘<br><br>’;
echo ‘<b>How many times video have seen:</b> ‘.$videoInfo->viewCount;
echo ‘<br><br>’;
echo ‘<b>Video length(in seconds):</b> ‘.$videoInfo->length;
echo ‘<br><br>’;
echo ‘<b>Video rating:</b> ‘.$videoInfo->rating;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s