Get Youtube all information

Posted: December 15, 2012 in Uncategorized

<!DOCTYPE html
PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml&#8221; xml:lang=”en” lang=”en”>
<head>
<title>Retrieving video details</title>
<style>
img {
padding: 2px;
margin-bottom: 15px;
border: solid 1px silver;
}
td {
vertical-align: top;
}
</style>
</head>
<body>
<?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/&#8217; . $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/&#8217;);
$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&#8217;);
$attrs = $yt->duration->attributes();
$obj->length = $attrs[‘seconds’];

// get <yt:stats> node for viewer statistics
$yt = $entry->children(‘http://gdata.youtube.com/schemas/2007&#8217;);
$attrs = $yt->statistics->attributes();
$obj->viewCount = $attrs[‘viewCount’];

// get <gd:rating> node for video ratings
$gd = $entry->children(‘http://schemas.google.com/g/2005&#8217;);
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&#8217;);
if ($gd->comments->feedLink) {
$attrs = $gd->comments->feedLink->attributes();
$obj->commentsURL = $attrs[‘href’];
$obj->commentsCount = $attrs[‘countHint’];
}

return $obj;
}

$youtubeVideoLink = ‘http://www.youtube.com/watch?v=HLhPSpFkgB4&#8217;;
//$youtubeVideoLink = ‘http://tutsbucket.com/2010/11/css3-drop-down-menu/&#8217;;
$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 “<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>
<br/>\n”;
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 .
“</h3></td></tr>\n”;
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;
}
?>
</body>
</html>

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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