Youtube API - How to limit results for pagination?

≡放荡痞女 提交于 2019-12-03 15:01:04

I basically solved this in the same way as worchyld with a slight twist:

    $username = 'ignite';
    $limit = 30;  // Youtube will throw an exception if > 50
    $offset = 1;  // First video is 1 (silly non-programmers!)
    $videoFeed = null;
    $uploadCount = 0;
    try {
        $yt = new Zend_Gdata_YouTube();
        $yt->setMajorProtocolVersion(2);
        $userProfile = $yt->getUserProfile($username);
        $uploadCount = $userProfile->getFeedLink('http://gdata.youtube.com/schemas/2007#user.uploads')->countHint;

        // The following code is a dirty hack to get pagination with the YouTube API without always starting from the first result
        // The following code snippet was copied from Zend_Gdata_YouTube->getUserUploads();
        $url = Zend_Gdata_YouTube::USER_URI .'/'. $username .'/'. Zend_Gdata_YouTube::UPLOADS_URI_SUFFIX;
        $location = new Zend_Gdata_YouTube_VideoQuery($url);
        $location->setStartIndex($offset);
        $location->setMaxResults($limit);
        $videoFeed = $yt->getVideoFeed($location);
    } catch (Exception $e) {
        // Exception handling goes here!
        return;
    }

The Zend YouTube API seems silly as the included getUserUploads method never returns the VideoQuery instance before it actually fetches the feed, and while you can pass a location object as a second parameter, it's an "either-or" situation - it'll only use the username parameter to construct a basic uri or only use the location, where you have to construct the whole thing yourself (as above).

I've decided just to use the user uploads feed as a way of getting pagination to work. http://gdata.youtube.com/feeds/api/users/bbc/uploads/?start-index=1&max-results=10

If there is a way to use the query/search method to do a similar job would be interesting to explore.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!