PHP script for extract youtube video info from it's video link
Hello Guys,
Recently i've added a new feature in my site( 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);
$urlData["host"] == ''
// Move ahead 

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.

Video link:
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 
 // "" 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)
"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);

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 '' $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  node for video length
$yt $media->children('');
$attrs $yt->duration->attributes();
$obj->length $attrs['seconds']; 
// get  node for viewer statistics
$yt $entry->children('');
$attrs $yt->statistics->attributes();
$obj->viewCount $attrs['viewCount']; 
// get  node for video ratings
$gd $entry->children(''); 
       if (
$attrs $gd->rating->attributes();
$obj->rating $attrs['average']; 
$obj->rating 0;         
// get  node for video comments
$gd $entry->children('');
       if (
$attrs $gd->comments->feedLink->attributes();
$obj->commentsURL $attrs['href']; 
$obj->commentsCount $attrs['countHint']; 


$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:

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:

IBM developerworks site:

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 :

6th Dec 2010 at 12:30 PM   25232 Views

Thanks a lot Suresh, you’re awesome.

7th Dec 2010 at 3:40 PM  

Thx Matteo, for appreciating.

7th Dec 2010 at 4:19 PM  

Hi Suresh. This is a great work from ur side. Here you have told about how to extract meta information from a youtube video. Would you please tell me how can I fetch meta information(specifically 'description' tag) from a video file placed at a remote server using php.
31st Mar 2011 at 11:57 AM  

Are u asking about, how to extract youtube video description tag... ?
1st Apr 2011 at 11:44 PM  

No Suresh. Forget about you tube video. I have a mp4 video placed at my local ftp server. I want to extract "description" tag from that video file's info. How can I do that in php.
2nd Apr 2011 at 11:15 AM  

Telling the exact code is difficult to say but for that u can use XML feeds method or any JSON data parsing method. By XML/JSON method u just need to create the data in respective format and from receiving end, u can use those info & extract u'r data(description tags).
3rd Apr 2011 at 10:04 PM  

Hi Suresh, what if I only know the video title name? How do i return a video ID or a list of video IDs? For example, in your case, if I want to find video ID for "TRON LEGACY"? (Result output should be "-xxLw7S6EaA"). Feel free to leave me a message or email me at
22nd Mar 2012 at 11:58 AM  

Hi suresh.. First of all thanks to sharing your knowledge..
But i received warning like this
Warning: simplexml_load_file( [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
11th Mar 2016 at 10:58 AM