Skip to content

Instantly share code, notes, and snippets.

@jeffmicklos
Created April 2, 2010 01:33
Show Gist options
  • Save jeffmicklos/352627 to your computer and use it in GitHub Desktop.
Save jeffmicklos/352627 to your computer and use it in GitHub Desktop.
<?php
//A short and simple controller/end-point for Mootube AJAX calls...
//This example uses cURL to fetch data but you can use file_get_contents or whatever you please...
//This example also uses PHP's json features, which are only available in PHP5+
//LASTLY, i love you.
$ids=explode('|',$_GET['ids']);
array_pop($ids);
$holder=array();
foreach($ids as $key=>$value) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://oohembed.com/oohembed/?url=http://youtube.com/watch?v='.$value);
$json = curl_exec($ch);
curl_close($ch);
$array=json_decode($json, true);
$holder[]=$array;
}
$object=json_encode($holder);
//echo stripslashes($object);
echo $object;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Mootube by Jeff Micklos</title>
<!--*******************************************************-->
<!-- This is the class needs both Core & More to run -->
<script type="text/javascript" src="http://onwebtape.com/js/mootools-1.2.3-core-yc.js"></script>
<script type="text/javascript" src="http://onwebtape.com/js/mootools-1.2.3.1-more.js"></script>
<script type="text/javascript" src="mootube.js"></script>
<!--*******************************************************-->
<script type="text/javascript">
window.addEvent('domready',function() {
// *******************************************************
// This is where we instantiate the class, the only param is an object.
// Here we are telling it to look for links WHICH have the rel="mootube" and once it is found, remove the link.
// The third parameter is the URL to make your AJAX request, this request will get data from YouTube.
var moostance = new Mootube({
which: 'mootube',
removeLink: false,
controller: 'http://api.onwebtape.com/mootube/controller.php'
});
// *******************************************************
});
</script>
</head>
<body>
<!--*******************************************************-->
<!-- Just tie rel="mootube" onto any Youtube links and you will be good! -->
<a href="http://www.youtube.com/watch?v=GoLJJRIWCLU" rel="mootube">jigsaw</a>
<!--*******************************************************-->
</body>
</html>
// Yo, this is a Mootools class for embedding YouTube videos just by setting links to YouTube.
// I, Jeff Micklos, built it so please let me know if you need help! jeffmicklos at gmail dot com
// Feel free to check out my portfolio too, www.onwebtape.com/me
var Mootube = new Class({
Implements: Options,
options: {
which: 'mootube',
removeLink: true,
details: true,
controller: 'controller.php'
},
initialize: function(options){
this.setOptions(options);
if(this.options.details===true) { this.getData(); }
else { this.renderEmbed(); }
},
getData: function() {
var links=$$('a[rel='+this.options.which+']');
var ids='';
var that=this;
for(var i=0;i<links.length;i++){
if(links[i].getProperty('href').contains('watch?v=')) { var id=links[i].getProperty('href').split('watch?v='); }
else if(links[i].getProperty('href').contains('v/')) { var id=links[i].getProperty('href').split('v/'); }
ids+=id[1]+'|';
}
var myRequest = new Request({
url: that.options.controller,
method: 'get',
onSuccess: function(responseText) { that.ajax_renderEmbed(JSON.decode(responseText)); }
}).send('ids='+ids);
},
ajax_renderEmbed: function(object) {
var links=$$('a[rel='+this.options.which+']');
for(var i=0;i<links.length;i++){
var description = new Element('div', {id: 'mootube_description'});
var rel=$(document.body).getElement('a[href='+links[i].getProperty('href')+']');
var el = new Element('div',{'html':object[i].html}).inject(rel,'after');
description.inject(el,'after');
var descrip=
object[i].title
+ ' on YouTube by '
+ object[i].author_name
description.set('text',descrip);
if(this.options.removeLink===true) { rel.destroy(); }
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment