如何在没有超时的情况下从频道中检索YouTube视频?

时间:2015-01-01 18:12:05

标签: php youtube youtube-api

我有这个程序可以从频道中检索视频并对其进行随机播放,正如您所看到的,该程序从变量$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,但我现在不想这样做

1 个答案:

答案 0 :(得分:0)

这是使用已弃用的GData API,如果您使用新的Data API v3,而retrieving channel's uploaded videos,您可以设置maxresults并通过分页获取结果,这样您就可以遍历页面