Get Youtube feed in your website search results

API, PHP 2 responses so far

All video websites likes to improve their search facility by searching for videos not only those which are in their databases but also on youtube.In this way users will get more results of their keyword search and will be able to play or store these youtube videos into their website’s account.

When you retrieve video feed or list of search results youtube returns an atom feed which is basically a xml response.

The above url is the basic query string which will be used to retrieve videos from youtube to your search page. Here $q refers to the search keyword.Remember to urlescape your search word if its a multi word string.For example: $q=Hardwell+songs. Orderby refers to the way you want the results be filtered and retrieved.It can be by relevance,published,viewcount or ratings. Max-results denotes the maximum number of videos you want to retrieve at once.

Below is the full code to get and parse youtube xml feed:

$feedURL = "{$q}&orderby=relevance&max-results=10";
$sxml = simplexml_load_file($feedURL);

// get summary counts from opensearch: namespace
$counts = $sxml-> children('');

 $total = $counts->totalResults;
 $startOffset = $counts->startIndex;
 $endOffset = ($startOffset-1) + $counts->itemsPerPage;

 // iterate over entries in resultset
 foreach ($sxml->entry as $entry) {
 // get nodes in media: namespace for media information
 $media = $entry->children('');

// get video player URL
 $attrs = $media->group->player->attributes();
 $watch = $attrs['url'];


 // get video thumbnail
 $attrs = $media->group->thumbnail[0]->attributes();
 $thumbnail = $attrs['url'];

// get <yt:duration> node for video length
 $yt = $media->children('');
 $attrs = $yt->duration->attributes();
 $length = $attrs['seconds'];

 if(($length - floor($length/60) * 60) <10){
 $time= floor($length/60). ':0'.($length - floor($length/60) * 60);
 $time= floor($length/60). ':'.($length -floor($length/60) * 60);

 // get <gd:rating> node for video ratings
 $gd = $entry->children('');
 if ($gd->rating) {
 $attrs = $gd->rating->attributes();
 $rating = $attrs['average'];
 } else {
 $rating = 0;
 <div style="float:left; margin:0px 10px 5px 20px; width:492px; height:100px; ">
 <div style="float:left; position:relative; width:100px; height:100px;">
 <img src="<? echo($thumbnail)?>" width="100" height="100" style="cursor:pointer;" onclick='changefunc("<? echo($link);?>")'>
 <div style="float:left; position:relative; width:380px; height:auto; font-size:15px; margin-left:5px; font-weight:bold; color:#000;">
 <a href="javascript:void(null)" onclick='changefunc("<? echo($link);?>")' style="color:#000;"><? echo($title);?></a>

 <div style="float:left;width:380px; height:auto; font-size:12px; margin:25px 0 0 5px; font-weight:bold; color:#000;">
 Duration: <? echo($time);?>

ok, so from above codes we are able to get the thumbnail,youtube video link,ratings of that video and duration of the video in minutes format

Now you can use all these info to store these videos in your database and even play video by using $link and display it using iframe:

<iframe src=”$link” width=”300″ height=”250″></iframe>

Similarly you can extract other info like number of likes ,comments etc from the xml feed

Hope you like this post and will use it in your video website .


The following two tabs change content below.
I am a core php developer and web designer working as a freelancer in elance,odesk etc.

Latest posts by Mudit (see all)

  • You will discover some interesting points in time in this article but I do not know if I see all of them center to heart. There is certainly some validity but I will take hold opinion until I appear into it further. Fine post , thanks and we want even more! Added to FeedBurner at the same time


  • Hi friend, Please help me. From the above script we can search youtube videos. But i want to play each videos as a video page in my site without storing those it to the database. Please help me on this.