I made a quick little class to read a YouTube user’s video feed that really easy to use. This class has 2 functions and its constructor needs a user name.
The first function showFullFeed(), shows the full feed with the titles of the videos as links to the videos. The second function showTheFirst($limit), shows the first number of videos you specify as the limit. So for example if you say $object->showTheFirst(5), the function will echo links and descriptions to the first 5 videos.
<?php
class WebHole_Youtube_Feed{
function __construct($username)
{
$this->username=$username;
$this->feedUrl=$url='http://gdata.youtube.com/feeds/api/users/'.$this->username.'/uploads';
$this->feed=simplexml_load_file($url);
}
public function showFullFeed()
{
foreach($this->feed->entry as $video){
echo '<p>';
echo "<a href='{$video->link['href']}'>".$video->title.'</a><br />';
echo $video->content;
echo '</p>';
}
}
public function showTheFirst($limit)
{
$i=0;
while(($video=$this->feed->entry[$i]) && ($i++!=$limit)){
echo '<p>';
echo "<a href='{$video->link['href']}'>".$video->title.'</a><br />';
echo $video->content;
echo '</p>';
}
}
};
$youtube=new WebHole_Youtube_Feed('username');
// will show the last 5 videos you uploaded
$youtube->showTheFirst(5);
And this is the result
YouTube Feed Parsed Using WebHole_Youtube_Feed
How are you planning to use this class? let us know in the comments below.