<?php
class YoutubeVideos
{
private $xml_data;
public $channel;
public $max_results;
public $max_title_chars;
public $max_description_chars;
public $start_index;
public function __construct($channel, $max_results = 50, $start_index = 1)
{
if (!function_exists("simplexml_load_file"))
{
echo "The simplexml_load_file function was not found. Please upgrade to PHP 5.";
exit();
}
if (!empty($channel))
{
$this->channel = $channel;
$this->max_results = $max_results;
$this->start_index = $start_index;
$feed_url = "http://gdata.youtube.com/feeds/api/users/" . $this->channel . "/uploads?max-results=" . $this->max_results . "&start-index=" . $this->start_index;
$this->xml_data = simplexml_load_file($feed_url);
}
else
{
echo "There is no channel name. Please consult the documentation.";
exit();
}
}
public function display($max_title_chars = 30, $max_description_chars = 40, $embed = false, $embed_width = 180, $embed_height = 101)
{
echo '<ul class="ytcg_ul">';
foreach ($this->xml_data->entry as $entry)
{
$video_url = $this->get_video_url($entry);
$video_thumbnail = $this->get_thumbnail($entry);
$video_title = $this->get_title($entry);
$video_description = $this->get_description($entry);
$video_title = $this->shorten_text($video_title, $max_title_chars);
$video_description = $this->shorten_text($video_description, $max_description_chars);
echo '<li>';
if ($embed)
{
$this->player($this->watch_id($video_url), $embed_width, $embed_height);
}
else
{
echo '<a href="' . $video_url . '"><img src="' . $video_thumbnail . '" alt="' . $video_title . '" /></a>';
}
echo '<div><a href="' . $video_url . '"><span>' . $video_title . '</span></a><p>' . $video_description . '</p></div>';
echo '</li>';
}
echo '</ul>';
}
public function video_info($video_id)
{
$return_array = array();
$feed_url = "http://gdata.youtube.com/feeds/api/videos/" . $video_id . "?v=2&alt=json";
if ($json_data = @file_get_contents($feed_url))
{
$json_data = json_decode($json_data);
$views = $json_data->{'entry'}->{'yt$statistics'}->{'viewCount'};
$favorites = $json_data->{'entry'}->{'yt$statistics'}->{'favoriteCount'};
$dislikes = $json_data->{'entry'}->{'yt$rating'}->{'numDislikes'};
$likes = $json_data->{'entry'}->{'yt$rating'}->{'numLikes'};
$return_array['views'] = $views;
$return_array['favorites'] = $favorites;
$return_array['dislikes'] = $dislikes;
$return_array['likes'] = $likes;
return $return_array;
}
else
{
die("Your file_get_contents cannot open external URLs. You cannot use this function until it is enabled");
}
}
public function shorten_text($text, $max_chars)
{
if (strlen($text) > $max_chars)
$text = substr_replace($text, "...", $max_chars);
return $text;
}
public function get_video_url($entry)
{
$video_data = $entry->children('media', true);
return (string)htmlspecialchars($video_data->group->player->attributes()->url);
}
public function get_thumbnail($entry)
{
$video_data = $entry->children('media', true);
return (string)htmlspecialchars($video_data->group->thumbnail[0]->attributes()->url);
}
public function get_title($entry)
{
$video_data = $entry->children('media', true);
return (string)htmlspecialchars($video_data->group->title);
}
public function get_description($entry)
{
$video_data = $entry->children('media', true);
return (string)htmlspecialchars($video_data->group->description);
}
public function get_keywords($entry)
{
$video_data = $entry->children('media', true);
return (string)htmlspecialchars($video_data->group->keywords);
}
public function get_category($entry)
{
$video_data = $entry->children('media', true);
return (string)htmlspecialchars($video_data->group->category);
}
public function watch_id($url)
{
preg_match('/v=(.+?)&+/', $url, $watch_id);
return $watch_id[1];
}
public function player($watch_id, $width = 560, $height = 315)
{
echo '<iframe width="' . $width . '" height="' . $height . '" src="http://www.youtube.com/embed/' . $watch_id . '" frameborder="0"></iframe>';
}
public function get_xml()
{
return $this->xml_data;
}
}
?>