Youtube API

Youtube API

Youtube API

Today I stumbled upon a little known tool that YouTube offer. Using the following snippet of code developers can embed YouTube search results, playlists and subscription boxes directly into their webpages, all through the YouTube API.

This handy utility allows developers to promote a business’s complete online presence from their website.

For those that are interested, here is how it works: Using PHP, a server based programming language the server sends a HTTP query to the YouTube servers through the URI below.{Search Query}&orderby=relevance&max-results=10

This returns a large XML file which contains all the information that is obtained from the search. The rest of this code snippet then parses this XML file into HTML which can be interpreted by web browsers and viewed by visitors.

Below is the PHP code which I have created that queries and returns YouTube search results.

$q = 'Search Query';
$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);?>
<script type="text/javascript">
function changefunc(loc){
	window.location = loc;

The YouTube API has many more features, to see what it has to offer visit the Youtube API page here.

Website Design Services

Copyright © 2004 - 2018 Designaweb (BSE) Ltd, All rights reserved