Youtube API - 如何限制分页结果? [英] Youtube API - How to limit results for pagination?

查看:24
本文介绍了Youtube API - 如何限制分页结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想抓取用户的上传内容(即:BBC)并将输出限制为每页 10 个.

I want to grab a user's uploads (ie: BBC) and limit the output to 10 per page.

虽然我可以使用以下网址:http://gdata.youtube.com/feeds/api/users/bbc/uploads/?start-index=1&max-results=10

Whilst I can use the following URL: http://gdata.youtube.com/feeds/api/users/bbc/uploads/?start-index=1&max-results=10

以上工作正常.

我想改用查询方法:

Zend 框架文档:http://framework.zend.com/manual/en/zend.gdata.youtube.html

声明我可以检索用户上传的视频,但理想情况下我想使用查询方法来限制分页的结果.

State that I can retrieve videos uploaded by a user, but ideally I want to use the query method to limit the results for a pagination.

查询方法在 Zend 框架文档中(与之前标题按元数据搜索视频"相同的页面)并且与此类似:

The query method is on the Zend framework docs (same page as before under the title 'Searching for videos by metadata') and is similar to this:

<代码>打印 <代码>'

$yt = new Zend_Gdata_YouTube(); $query = $yt->newVideoQuery(); $query->setTime('today'); $query->setMaxResults(10); $videoFeed = $yt->getUserUploads( NULL, $query );

    ';foreach($videoFeed 为 $video):打印 '
  1. '.$video->title .'</li>';Endforeach;打印'</ol>';

print '<ol>'; foreach($videoFeed as $video): print '<li>' . $video->title . '</li>'; endforeach; print '</ol>';

问题是我不能做 $query->setUser('bbc').

The problem is I can't do $query->setUser('bbc').

我尝试过 setAuthor 但这返回了完全不同的结果.

I tried setAuthor but this returns a totally different result.

理想情况下,我想使用查询方法以分页方式获取结果.

Ideally, I want to use the query method to grab the results in a paginated fashion.

如何使用 $query 方法设置分页限制?

How do I use the $query method to set my limits for pagination?

谢谢.

推荐答案

我基本上用和 worchyld 一样的方式解决了这个问题,只是稍微有点改动:

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;
    }

Zend YouTube API 看起来很愚蠢,因为包含的 getUserUploads 方法在实际获取提要之前从不返回 VideoQuery 实例,虽然您可以将位置对象作为第二个参数传递,但这是一种非此即彼"的情况 - 它将只使用用户名参数来构建一个基本的 uri 或者只使用位置,你必须自己构建整个东西(如上).

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).

这篇关于Youtube API - 如何限制分页结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆