<?php
error_reporting(E_ALL);
// for pagination
session_start();
////////////////////////////////////////
// youtube.class.php v1.0 /////////
// released 3/8/2007 ///////////////
// for waxjelly.wordpress.com ////
// the internet is for users ////////
////////////////////////////////////////
// this class is to be used free ////
// of charge so long as this line //
// remains intact, and untouched/
////////////////////////////////////////
define('API_KEY', 'ARgbYoNTCbs'); // YOUR YOUTUBE DEVELOPERS API KEY
//-------------------------------//
// DO NOT EDIT
//-------------------------------//
/////////////////////
// CONSTANTS //
////////////////////
define('API_URL', 'http://www.youtube.com/');
// CALL GATEWAYS
define('API_REST_CALL', API_URL . 'api2_rest?');
define('API_XMLRPC_CALL', API_URL . 'api2_xmlrpc?');
// METHODS FOR VIDEOS
define('BY_TAG', 'list_by_tag');
// create youtube class
class youtube {
// CLASS PROPERTIES
public
// VARIABLE TO HOLD THE SIMPLE XML ELEMENT WITH RETURNED DATA
$xml,
// HOLDS CONFIG OPTIONS
$config = array(),
// VAR TO STORE THE QUERY STRING FOR YOUTUBE
$api_call,
// RETURNED DATA FROM XML CALL
$return,
$tag,
// IF WE ENCOUNTER ERRORS ALONG OUR JOURNEY
$errors;
// CONSTRUCTION VARIABLES
public function __construct($api_key = API_KEY, $page = 1, $per_page = 25) {
// JUST TO SETUP SOME DEFAULT VALUES FOR A NOMINAL CALL.
// WE WANTED TO MAKE IT EASY TO JUST USE THE CLASS, NOT CONFIGURE IT.
youtube::set('api_key', API_KEY);
youtube::set('page', $page);
youtube::set('per_page', $per_page);
// modify the call
youtube::modify_call(API_REST_CALL . 'method=youtube.');
}
function xml_to_array($xml) {
$fils = 0;
$tab = false;
$array = array();
foreach($xml->children() as $key => $value) {
$child = youtube::xml_to_array($value);
//To deal with the attributes
//foreach ($node->attributes() as $ak => $av) {
//$child[$ak] = (string)$av;
//}
//Let see if the new child is not in the array
if ($tab == false && in_array($key, array_keys($array))) {
//If this element is already in the array we will create an indexed array
$tmp = $array[$key];
$array[$key] = NULL;
$array[$key][] = $tmp;
$array[$key][] = $child;
$tab = true;
} elseif($tab == true) {
//Add an element in an existing array
$array[$key][] = $child;
} else {
//Add a simple element
$array[$key] = $child;
}
$fils++;
}
if($fils==0) {
return (string)$xml;
}
return $array;
}
// SET A CONFIG VARIABLE
public function set($item, $value) {
$this->config[$item] = $value;
}
// ACCESS CONFIG
public function config($item) {
return $this->config[$item];
}
// MODIFY THE CALL CLASS PROPERTY
public function modify_call($params) {
$this->api_call .= $params;
}
// ECHO THE CALL FOR DEBUGGING
public function return_call() {
return $this->api_call;
}
// YOUTUBE VIDEO METHODS
public function videos($data = 'mutemath', $method = BY_TAG) {
//---------------------------------------------//
$this->api_call .= 'videos.'; ///////////////////////
//---------------------------------------------//
switch ($method) {
case BY_TAG:
if (isset($_GET['tag'])) {
$keyword = str_replace(" ", "+", $_GET['tag']);
$this->tag = '&tag=' . $keyword;
} else {
$this->tag = '&tag='. $data;
}
youtube::modify_call($method . '&dev_id=' . $this->config['api_key'] . $this->tag);
break;
default:
return 'The WaxJelly YouTube API Class only supports retrieving videos by tag.';
break;
}
// CALL DISPLAY
return youtube::display();
}
public function tags_for_video($tags) {
$tag_links = '';
// explode
$x = explode(' ', $tags);
if (count($x) > 0) {
foreach ($x as $k => $v) {
$tag_links .= "<a href=\"{$_SERVER['PHP_SELF']}?tag=$v\">$v</a> ";
}
} else {
$tag_links = "<a href=\"{$_SERVER['PHP_SELF']}?tag=mutemath\">mutemath</a>";
}
return $tag_links;
}
// DISPLAY RESULTS
public function display() {
// CHECK FOR A NEW PAGE & PER PAGE
if (isset($_GET['page'])) {
youtube::set('page', $_GET['page']);
}
if (isset($_GET['per_page'])) {
youtube::set('per_page', $_GET['per_page']);
}
// per page, page, prev, next
// total videos, total pages
youtube::modify_call('&page=' . youtube::config('page') . '&per_page=' . youtube::config('per_page'));
// SEND REQUEST
$this->return = file_get_contents(youtube::return_call());
$this->xml = new SimpleXMLElement($this->return);
$this->xml = youtube::xml_to_array($this->xml);
// CHECK IF THEY'RE ASKING FOR A VIDEO ID
if (isset($_GET['video_id'])) {
define('YOUTUBE_VIDEO_ID', $_GET['video_id']);
// loop through videos to get videos index
foreach ($this->xml['video_list']['video'] as $indx => $array) {
if ($array['id'] == $_GET['video_id']) {
$index = $indx;
echo $index;
break;
} else {
$index = '';
}
}
// define index
define('YOUTUBE_VIDEO_INDEX', $index);
} else {
define('YOUTUBE_VIDEO_ID', $this->xml['video_list']['video'][0]['id']);
define('YOUTUBE_VIDEO_INDEX', 0);
}
//////////////////////////////////////////
// CREATE DISPLAY CONSTANTS //
/////////////////////////////////////////
define('YOUTUBE_PAGE', youtube::config('page'));
define('YOUTUBE_PER_PAGE', youtube::config('per_page'));
$prev = (youtube::config('page') - 1 == 0) ? 1 : youtube::config('page') - 1;
define('YOUTUBE_PREV_PAGE', $prev . $this->tag);
define('YOUTUBE_NEXT_PAGE', youtube::config('page') + 1 . $this->tag);
define('YOUTUBE_TOTAL_VIDEOS', $this->xml['video_list']['total']);
define('YOUTUBE_TOTAL_PAGES', ceil($this->xml['video_list']['total'] / youtube::config('per_page')));
// CONVERT FROM SIMPLEXML OBJECT TO ARRAY
return $this->xml;
}
}
?>