Youtube API v3 返回的结果少于请求的结果 [英] Youtube API v3 returning less then requested results

查看:39
本文介绍了Youtube API v3 返回的结果少于请求的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 YouTube API 请求一组视频,我将 maxResults 设置为 50,在返回的对象中我得到以下信息:[totalResults] => 271 [resultsPerPage] => 50.一切都很好,但是该对象仅包含 3 个视频.当我使用具有更多结果的搜索词时,它确实会显示更多但仍然不是它应该显示的 50,只有当返回大量视频(比如 1000+)时,我才会在对象中得到 50,这是怎么回事?

I'm using the YouTube API to request a set of videos I have maxResults set to 50 and in the returned object I get the following information: [totalResults] => 271 [resultsPerPage] => 50. All fine, but the object only holds 3 videos. When I use a search term with more results it does show more but still not the 50 that it should, only when a lot of videos are returned (say 1000+) then I get 50 in the object, what is going on here?

这是我的请求代码:

$searchResponse = $youtube->search->listSearch('id,snippet', array(
'q' => "Let's Play The Legend of Zelda",
'maxResults' => 50,
'type' => 'video',
'videoEmbeddable' => 'true',
'publishedAfter' => date("Y-m-d\TH:i:sP",strtotime('-2 days')),
'videoDuration' => 'short',
'videoDefinition' => 'high',
'order' => 'viewCount',
));

这是我返回的对象:

Array
(
    ...
    [pageInfo] => Array
        (
            [totalResults] => 972
            [resultsPerPage] => 50
        )
    [items] => Array
        (
            [0] => Array
            ...
            [7] => Array
        )
)

正如你所看到的,它只有 8 个视频,而它有 972 个结果,应该给我 50 个.

As you can see it only hold 8 videos while it has 972 results and should give me 50.

推荐答案

这是 YouTube API 的问题.totalResults 值可能返回不准确的数字,不应用于检查响应中有多少结果.

This is an issue with YouTube API. The totalResults value may returns not accurate numbers and it shouldn't be used to check how many results are in the response.

不幸的是,这是一个已知问题,总的来说,我建议将 totalResults 视为一个非常粗略的指南,并确保您不会围绕它构建任何分页逻辑.响应中是否存在 nextPageToken 应该是唯一表明是否有其他可用结果的因素.

it's unfortunately a known issue, and in general, I would recommend treating totalResults as a very rough guide and ensure that you don't build any paging logic around it. The presence/absence of a nextPageToken in a response should be the only thing which indicates whether there are additional results available.

来源:https://code.google.com/p/gdata-issues/issues/detail?id=6125&can=1

在这种情况下你可以做的是使用 count() 函数来检查你有多少项目.

What can you do in this situation is to use count() function to check how many items you've got.

这篇关于Youtube API v3 返回的结果少于请求的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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