【问题标题】:Youtube data api v3 not working as expectedYoutube 数据 api v3 未按预期工作
【发布时间】:2016-07-06 20:24:29
【问题描述】:

我正在使用 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 关键字。
  • q 的确切值?你能展示整个代码(移除 API 密钥)吗?
  • "hardwell%20singles" 是准确值。

标签: php youtube-data-api


【解决方案1】:

您仅获得 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

相关问题:
When I use order-date in youtube api then there are total results but items are not found

【讨论】:

    猜你喜欢
    • 2016-03-16
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2023-03-21
    • 2017-10-24
    相关资源
    最近更新 更多