如何在不超时的情况下从频道中检索 youtube 视频



>我有一个程序,可以从频道中检索视频并对其进行洗牌,如您所见,该程序从变量中获取频道的名称$playlistId

<?php
        session_start();
        class PlayList {
                public function __construct($id) {
                        $this->id = $id;
                        $this->videos = [];
                        $this->currentVideo = 0;
                        $this->addVideos('https://gdata.youtube.com/feeds/api/users/' . $this->id . '/uploads?v=2&alt=json');
                        shuffle($this->videos);
                }
                public function addVideos($url) {
                        $cont = json_decode(file_get_contents($url));
                        foreach ($cont->feed->entry as $entry) {
                                $video = [];
                                $video['title'] = $entry->title->{'$t'};
                                $video['desc'] = $entry->{'media$group'}->{'media$description'}->{'$t'};
                                $video['id'] = $entry->{'media$group'}->{'yt$videoid'}->{'$t'};
                                $this->videos[] = $video;
                        }
                        foreach ($cont->feed->link as $link) {
                                if ($link->rel === 'next') {
                                        $this->addVideos($link->href);
                                        break;
                                }
                        }
                }
                public function getId() {
                        return $this->id;
                }
                public function nextVideo() {
                        if ($this->currentVideo < count($this->videos) - 1) {
                                $this->currentVideo += 1;
                        } else {
                                $this->currentVideo = 0;
                        }
                }
                public function getVideoTitle() {
                        return $this->videos[$this->currentVideo]['title'];
                }
                public function getVideoDesc() {
                        return $this->videos[$this->currentVideo]['desc'];
                }
                public function getVideoSrc() {
                        return "http://www.youtube.com/embed/" . $this->videos[$this->currentVideo]['id'];
                }
                public function printList() {
                        foreach ($this->videos as $video) {
                                echo $video['title'] . '<br>';
                        }
                }
        }
?>
<?php
        $playlistId = "NAKCollection";
        if (!isset($_SESSION['playlist']) || $_SESSION['playlist']->getId() != $playlistId) {
                $_SESSION['playlist'] = new PlayList($playlistId);
        } else {
                $_SESSION['playlist']->nextVideo();
        }
?>
<html>
    <head>
        <title>Video Shuffle</title>
    </head>
    <body>
    <div style="float: right;">
        <?php
            $_SESSION['playlist']->printList();
        ?>
    </div>
    <div>
        <?php echo  $_SESSION['playlist']->getVideoTitle(); ?>
    </div>
    <div>
        <iframe type="text/html" src="<?php echo $_SESSION['playlist']->getVideoSrc(); ?>" allowfullscreen></iframe>
    </div>
    </body>
</html>

如果频道有 300 个视频,页面超时,我如何像批处理一样一次检索 50 个视频,然后在第一组完成后再检索 50 个视频?

我知道我可以改变 php.ini但现在我不想这样做

这是使用已弃用的 GData API,如果使用新的数据 API v3,在检索频道上传的视频时,可以设置 maxresults 并通过分页获取结果,以便您可以循环访问页面。

相关内容

  • 没有找到相关文章

最新更新