YouTube数据API V3未按预期工作

4

我正在使用YouTube搜索API:

https://developers.google.com/youtube/v3/docs/search/list

我一共获取到40191条记录,每页最多设置为50条。

但是每次只能得到17个结果。

以下是我用于获取记录的选项:

// SET OPTIONS FOR API
$optionsArray = array(
        'q' => $_GET['q'],
        'maxResults' => 50,
        'order' => 'date',
        'type' => 'video'
    );

// Send request
$searchResponse = $youtube->search->listSearch('id, snippet', $optionsArray);

同时设置pageToken也不起作用。即使应用了nextPageToken,记录仍然相同。我是否漏掉了什么?


你传递给 q 的是什么? - itzmukeshy7
@itzmukeshy7 关键字。 - Shreejibawa
q 的确切值是多少?您能展示完整的代码(已删除 API 密钥)吗? - Sebastian Brosch
"hardwell%20singles" 是确切的值。 - Shreejibawa
1个回答

2
你只能得到17个结果的原因是你请求中的order属性。如果你移除这个属性,你会得到更多的结果。order='date'并没有按照预期工作。
一个小的解决方法(仅供参考):
$DEVELOPER_KEY = 'THEKEY';
$client = new Google_Client();
$client->setDeveloperKey($DEVELOPER_KEY);

//Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);

try {
    // Call the search.list method to retrieve results matching the specified
    // query term.
    $searchResponse = $youtube->search->listSearch('id,snippet', array(
        'q' => $_GET['q'],
        'maxResults' => $_GET['maxResults'],
        'type' => 'video'
    ));

    function sortResponse($date1, $date2) {
        $date1 = new DateTime($date1['snippet']['publishedAt']);
        $date2 = new DateTime($date2['snippet']['publishedAt']);
        //"<" - sort DESC
        //">" - sort ASC
        return $date1 < $date2;
    }

    $items = $searchResponse['items'];
    usort($items, "sortResponse");

    //now your sorted items are in $items...
}

您可以将所有响应项目合并到一个数组中,并使用自定义排序函数按publishAt的顺序进行排序。

API文档:
https://developers.google.com/youtube/v3/docs/search/list?hl=de#parameters

相关问题:
当我在YouTube API中使用order-date时,总共有结果,但找不到项目


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接