Skip to content

Instantly share code, notes, and snippets.

@tcelestino
Last active October 8, 2020 22:48
Show Gist options
  • Save tcelestino/5119782 to your computer and use it in GitHub Desktop.
Save tcelestino/5119782 to your computer and use it in GitHub Desktop.
Gallery YouTube videos using PHP and jQuery

Gallery YouTube Videos

Example with usage api YouTube with jQuery and PHP.

if(Browser == undefined){
var Browser = {
isIE: function(){ return (window.ActiveXObject && document.all && navigator.userAgent.toLowerCase().indexOf("msie") > -1 && navigator.userAgent.toLowerCase().indexOf("opera") == -1) ? true : false; }
}
}
var Flash = function(movie, id, width, height, initParams){
this.html = "";
this.attributes = this.params = this.variables = null;
this.variables = new Array();
this.attributes = {
"classid": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
"codebase": "http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab#version=8,0,22,0",
"type": "application/x-shockwave-flash"
}
this.params = { "pluginurl": "http://www.macromedia.com/go/getflashplayer_br" };
if(movie) {
this.addAttribute("data", movie);
this.addParameter("movie", movie);
}
if(id && id != null) this.addAttribute("id", id);
if(width) this.addAttribute("width", width);
if(height) this.addAttribute("height", height);
if(initParams != undefined){
for(var i in initParams){
this.addParameter(i.toString(), initParams[i]);
}
}
}
Flash.version = "1.2b";
Flash.getObjectByExceptions = function(obj, excep){
var tempObj = {};
for(var i in obj){
var inclui = true;
for(var j=0; j<excep.length; j++)
if(excep[j] == i.toString()) { inclui = false; break; };
if(inclui) tempObj[i] = obj[i];
}
return tempObj;
}
Flash.prototype.addAttribute = function(prop, val){ this.attributes[prop] = val; }
Flash.prototype.addParameter = function(prop, val){ this.params[prop] = val; }
Flash.prototype.addVariable = function(prop, val){ this.variables.push([prop, val]); }
Flash.prototype.getFlashVars = function(){
var tempString = new Array();
for(var i=0; i<this.variables.length; i++)
tempString.push(this.variables[i].join("="));
return tempString.join("&");
}
Flash.prototype.toString = function(){
this.params.flashVars = this.getFlashVars();
if(Browser.isIE()){
//IE
this.html = "<ob" + "ject";
var attr = Flash.getObjectByExceptions(this.attributes, ["type", "data"]);
for(var i in attr) if(i.toString() != "extend") this.html += " " + i.toString() + " = \"" + attr[i] + "\"";
this.html += "> ";
var params = Flash.getObjectByExceptions(this.params, ["pluginurl", "extend"]);
for(var i in params) if(i.toString() != "extend") this.html += "<param name=\"" + i.toString() + "\" value=\"" + params[i] + "\" /> <param name='wmode' value='transparent' /> <param name='allowFullScreen' value='true' />";
this.html += " </obj" + "ect>";
} else {
//non-IE
this.html = "<!--[if !IE]> <--> <obj" + "ect";
var attr = Flash.getObjectByExceptions(this.attributes, ["classid", "codebase"]);
for(var i in attr) if(i.toString() != "extend") this.html += " " + i.toString() + " = \"" + attr[i] + "\"";
this.html += "> ";
var params = Flash.getObjectByExceptions(this.params, ["extend"]);
for(var i in params) if(i.toString() != "extend") this.html += "<param name=\"" + i.toString() + "\" value=\"" + params[i] + "\" /> <param name='wmode' value='transparent' /> <param name='allowFullScreen' value='true' />";
this.html += " </obj" + "ect> <!--> <![endif]-->";
}
return this.html;
}
Flash.prototype.write = Flash.prototype.outIn = Flash.prototype.writeIn = function(w){
if(typeof w == "string" && document.getElementById) var w = document.getElementById(w);
if( w != undefined && w ) w.innerHTML = this.toString();
else document.write( this.toString() );
}
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js"></script>
<script type="text/javascript" src="flash.js"></script>
<script src="youtube.js" type="text/javascript"></script>
<title>Galeria de Videos</title>
</head>
<body>
<div id="galeria">
<h1>Galeria de Vídeos com YouTube</h1>
<div id="player"></div>
<ul id="thumbVideos" class="overview">
<?php
// URL do Feed
$feedURL = 'http://gdata.youtube.com/feeds/api/users/tcelestino/uploads?max-results=8';
// leio o xml dentro de um objeto SimpleXML
$sxml = simplexml_load_file($feedURL);
?>
<?php
// inicio o loop dos resultados
foreach ($sxml->entry as $entry) {
// Pego as informações do vídeo
$media = $entry->children('http://search.yahoo.com/mrss/');
// pelo a URL do player do vídeo
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
// pego o thumbnail
$attrs = $media->group->thumbnail[1]->attributes();
$thumbnail = $attrs['url'];
// pego a duração <yt:duration> do vídeo
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$minutos = $attrs['seconds'] / 60;
$segundos = $attrs['seconds'] % 60;
// pego as estatísticas <yt:stats>
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
// pego a nota <gd:rating>
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->rating) {
$attrs = $gd->rating->attributes();
$rating = $attrs['average'];
} else {
$rating = 0;
}
?>
<li><a href="<?php echo $watch; ?>" target="_blank" rel="<?php echo substr(($watch),31,11); ?>" title="<?php echo $media->group->title; ?>"><img src="<?php echo $thumbnail;?>" alt="" /></a> </li>
<?php
}
?>
</ul>
</div>
</body>
</html>
body {
margin:20px 0;
font-family:Arial, Helvetica, sans-serif;
font-size:13px;
}
h1 {
font-size:20px;
background:#f5f5f5;
text-align:center;
}
#galeria {
width:510px;
margin:0 auto;
}
#galeria player {
height480px
}
#galeria ul, #galeria ul li {
float:left;
margin:0;
padding:0;
}
#galeria ul li {
list-style:none;
margin:0 10px 10px 0;
}
#galeria ul li:nth-child(4n) {
margin-right:0
}
function player(v_id) {
var vid = new Flash("http://www.youtube.com/v/"+v_id, "player", "510", "292", "10");
vid.addParameter("scale", "noscale");
vid.addParameter("wmode", "transparent");
$('#player').html(vid.toString());
}
$(function() {
var video = $('ul#thumbVideos li:first a').attr('rel');
player(video);
$('ul#thumbVideos li a').click(function(e) {
player($(this).attr('rel'));
e.preventDefault();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment