PHP script for extract youtube video info from it's video link
Hello Guys,
Recently i've added a new feature in my site(http://www.viewsboard.com/) by which any user can add youtube video to any Discussion Board by just using youtube video link. This is really a pain less procedure to add any youtube video & i really love it. So today, i'm going to share my trick which i've use to accomplish this.
There are generally two methodes to extract youtube video info by it's link. One is, by using it's API & another is XML feed technique. Before, adding this feature i'd searched a lot of tutorial & made little experiment in my lab to use youtube API to extract youtube video info. But, at the end i didn't feel comfortable enough with youtube API for my work. Because, they've released a bunch of PHP files under Zend Framework but not for plain PHP. But, as my experience with Zend frameowrk is not so sound, i didn't want to move in that way. After, this i search for some XML trick to do this & got a really nice article from IBM developerworks site which really make my work too easy. I just needed to do little modification over that to work it according to my need.
The script is generally works in 3 steps. These are...
STEP-1: Link validation
In this step we need to validate the video link so that we can know it's original youtube video link rather than any other site link. For this, you can use htis peice of code..
STEP-2: Extract of video ID from it's link
Youtube use 11 characters(alphanumeric) ID for it's all video. In this step we need to extract this video ID from it's link.
Example:
Video link: http://www.youtube.com/watch?v=-xxLw7S6EaA
Video ID of above link: -xxLw7S6EaA
So, for this here is the function which you can use..
STEP-3: Extract of video info from it's video ID
This is the core part in which we extract video info in the form of XML feeds by using it's video ID. For this you can use this following function..
Now you've everythings in this "$videoInfo" variable. For extracting all the video info from this variable you can use following information..
Title of video: $videoInfo->title
Thumbnail link of video: $videoInfo->thumbnailURL
Youtube video link: $videoInfo->watchURL
Video description: $videoInfo->description
How many times video have seen: $videoInfo->viewCount
Video length(in seconds): $videoInfo->length
Video rating: $videoInfo->rating
Now, here is the demo file in which i've included all these above codes. Just execute it in your development enviroement & explore the codes to get more details including extraction of comments of each video. if you've any query regarding this you give your comments in the comment section.
Download demo file: http://www.box.net/shared/jop1v82cp4
NOTE: If you use codeigniter framework for PHP then you take help of my this post to extract the video thumbnail from youtube server to your server by using youtube thumbnail link.
Post Link: http://www.viewsboard.com/index.php/dboard/viewthread/28/229
Reference:
IBM developerworks site: http://www.ibm.com/developerworks/xml/library/x-youtubeapi/
NOTE - Guys, if anyone wants to include this feature please use following composer package. Right now, it's in the testing phase. But, I think it's working fine. Soon, it'll get release.
Link : https://github.com/mi6crazyheart/youtube-extract
Recently i've added a new feature in my site(http://www.viewsboard.com/) by which any user can add youtube video to any Discussion Board by just using youtube video link. This is really a pain less procedure to add any youtube video & i really love it. So today, i'm going to share my trick which i've use to accomplish this.
There are generally two methodes to extract youtube video info by it's link. One is, by using it's API & another is XML feed technique. Before, adding this feature i'd searched a lot of tutorial & made little experiment in my lab to use youtube API to extract youtube video info. But, at the end i didn't feel comfortable enough with youtube API for my work. Because, they've released a bunch of PHP files under Zend Framework but not for plain PHP. But, as my experience with Zend frameowrk is not so sound, i didn't want to move in that way. After, this i search for some XML trick to do this & got a really nice article from IBM developerworks site which really make my work too easy. I just needed to do little modification over that to work it according to my need.
The script is generally works in 3 steps. These are...
STEP-1: Link validation
In this step we need to validate the video link so that we can know it's original youtube video link rather than any other site link. For this, you can use htis peice of code..
$urlData = parse_url($ytURL);
if($urlData["host"] == 'www.youtube.com')
{
// Move ahead
}
else
{
return 0;
}
STEP-2: Extract of video ID from it's link
Youtube use 11 characters(alphanumeric) ID for it's all video. In this step we need to extract this video ID from it's link.
Example:
Video link: http://www.youtube.com/watch?v=-xxLw7S6EaA
Video ID of above link: -xxLw7S6EaA
So, for this here is the function which you can use..
function getYTid($ytURL)
{
$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;
}
STEP-3: Extract of video info from it's video ID
This is the core part in which we extract video info in the form of XML feeds by using it's video ID. For this you can use this following function..
// function to parse a video
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 node for video length
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$obj->length = $attrs['seconds'];
// get node for viewer statistics
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
$obj->viewCount = $attrs['viewCount'];
// get 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 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;
}
$videoInfo = parseVideoEntry($videoId);
Now you've everythings in this "$videoInfo" variable. For extracting all the video info from this variable you can use following information..
Title of video: $videoInfo->title
Thumbnail link of video: $videoInfo->thumbnailURL
Youtube video link: $videoInfo->watchURL
Video description: $videoInfo->description
How many times video have seen: $videoInfo->viewCount
Video length(in seconds): $videoInfo->length
Video rating: $videoInfo->rating
Now, here is the demo file in which i've included all these above codes. Just execute it in your development enviroement & explore the codes to get more details including extraction of comments of each video. if you've any query regarding this you give your comments in the comment section.
Download demo file: http://www.box.net/shared/jop1v82cp4
NOTE: If you use codeigniter framework for PHP then you take help of my this post to extract the video thumbnail from youtube server to your server by using youtube thumbnail link.
Post Link: http://www.viewsboard.com/index.php/dboard/viewthread/28/229
Reference:
IBM developerworks site: http://www.ibm.com/developerworks/xml/library/x-youtubeapi/
NOTE - Guys, if anyone wants to include this feature please use following composer package. Right now, it's in the testing phase. But, I think it's working fine. Soon, it'll get release.
Link : https://github.com/mi6crazyheart/youtube-extract
Thanks a lot Suresh, you’re awesome.
Thx Matteo, for appreciating.
But i received warning like this
Warning: simplexml_load_file(http://gdata.youtube.com/feeds/api/videos/-xxLw7S6EaA) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 410 Gone in E:\xampp\htdocs\youtube\sample2.php on line 66