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.
“http://gdata.youtube.com/feeds/api/videos/?q={$q}&orderby=relevance&max-results=10″;

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:

<?php
$feedURL = "http://gdata.youtube.com/feeds/api/videos/?q={$q}&orderby=relevance&max-results=10";
$sxml = simplexml_load_file($feedURL);

// get summary counts from opensearch: namespace
$counts = $sxml-> children('http://a9.com/-/spec/opensearchrss/1.0/');

 $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('http://search.yahoo.com/mrss/');
 $title=$media->group->title;

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

 $linkarr=explode('watch?v=',$watch);
 $linkarr1=explode('&',$linkarr[1]);
 $link="https://www.youtube.com/embed/".$linkarr1[0];

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

// get <yt:duration> node for video length
 $yt = $media->children('http://gdata.youtube.com/schemas/2007');
 $attrs = $yt->duration->attributes();
 $length = $attrs['seconds'];

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

 // get <gd:rating> node for video ratings
 $gd = $entry->children('http://schemas.google.com/g/2005');
 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>
 <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>

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

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 .

Thanks

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)

  • http://christianlouboutin4cheapa.totalh.net HipleFenpep

    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

    }

    http://topredbottomsa.22web.org

  • http://www.lordvine.com Alex

    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.

x
Loading...