Get complete playlist listing for a YouTube User via API

poolnoodl picture poolnoodl · Aug 15, 2011 · Viewed 7.5k times · Source

So, here's my code for getting a youtube user's public playlists:

function getyoutubeplaylists($userName) {
$yt = connectyoutube();
$yt->setMajorProtocolVersion(2);
$playlistListFeed = $yt->getPlaylistListFeed($userName);
foreach ($playlistListFeed as $playlistListEntry) {
    $playlist['title'] = $playlistListEntry->title->text;
    $playlist['id'] = $playlistListEntry->getPlaylistID();
    $playlists[] = $playlist;
    $playlistVideoFeed = $yt->getPlaylistVideoFeed($playlistListEntry->getPlaylistVideoFeedUrl());
    foreach ($playlistVideoFeed as $videoEntry) {
        $playlist_assignment['youtube_id'] = substr($videoEntry->getVideoWatchPageUrl(),31,11);
        $playlist_assignment['id'] = $playlist['id'];
        $playlist_assignments[] = $playlist_assignment;
    }
}
$everything['playlists'] = $playlists;
$everything['playlist_assignments'] = $playlist_assignments;
return $everything;
}

Problem is, this only gets the first page or results. Any ideas on how to use the Zend Gdata to retrieve the next page of results?

The raw gdata XML shows the URLs needed:

<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/pennstate/playlists?start-index=1&amp;max-results=25"/>
<link rel="next" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/pennstate/playlists?start-index=26&amp;max-results=25"/>

However, getPlaylistListFeed doesn't seem to have any parameters to specify "start-index" or "max-results".

Answer

vaene picture vaene · Aug 27, 2012

This is a useful Zend function for retrieving not only the next page but all available entries for a feed.

 $playlistListFeed = $yt->retrieveAllEntriesForFeed($yt->getPlaylistListFeed($userName));