我在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
后,记录也相同。我在这里错过了什么吗?
答案 0 :(得分: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...
}
您可以将所有repsonse项合并到一个数组中,并使用自定义排序功能按publishAt
获取订单。
API文档:
https://developers.google.com/youtube/v3/docs/search/list?hl=de#parameters相关问题:
When I use order-date in youtube api then there are total results but items are not found