Youtube V3 API - 根据关键字获取随机视频 [英] Youtube V3 API - Fetch a random video based on keyword

查看:41
本文介绍了Youtube V3 API - 根据关键字获取随机视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码导航到不同的 Youtube 数据页面.

I am using the below code to navigate to different pages of Youtube data.

我根据 $randomNumber(1 到 20)一次又一次地调用该服务.但我不认为这是更好的方法.

I call the service again and again based on $randomNumber ( 1 to 20). But I don't think this is the better way.

 $youtube = new Google_Service_YouTube($client);

    $searchResponse = $youtube->search->listSearch('id,snippet', array(
        'type' => 'video',
        'q' => $searchTerm,
        'maxResults' => $videoCount
    ));

    $nextPage = $searchResponse["nextPageToken"];

    for($i=1;$i< $randomNumber ;$i++){

        $newSearchResponse = $youtube->search->listSearch('id,snippet', array(
            'q' => $searchTerm,
            'maxResults' => $videoCount,
            'type' => "video",
            'pageToken' => $nextPage
        ));

        $nextPage = $newSearchResponse["nextPageToken"];

    }

   return $newSearchResponse;

请告诉我如何根据搜索文本获取随机视频.

Please let me know how to get a random video based on the search text.

推荐答案

很遗憾,没有官方方法可以使用 YouTube 数据 API 获取随机视频.
看看这个问题:如何使用 YouTube API 获取随机的 YouTube 视频?

Unfortunately there's no official way to get random videos with YouTube Data API.
Take a look at this question: How do I get a random YouTube video with the YouTube API?

我有类似的需求,我已经以与您类似的方式解决了它":我向 API 发出了一些 search->list 请求,使用 maxResults 设置为 50,(同时跟踪视频 ID),然后从结果集中随机选择一个视频.

I had a similar need, and i've "solved it" in a similar way to you: i made a few search->list requests to the API, with maxResults set to 50, (while keeping track of the video IDs) and then picked a random video from the resulting set.

这根本不是随机的,但如果没有太复杂的解决方案,我看不出有任何其他方法可以解决这个问题.

This is not random at all, but I don't see any other way around the problem without having far too complex solutions.

这篇关于Youtube V3 API - 根据关键字获取随机视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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