duandi8544 2015-01-01 18:12
浏览 57

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

I have this program that retrieves videos from a channel and shuffles them, as you can see, the program gets the channel's name from the variable $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>

If the channel has like 300 videos the page times out, how can I retrieve 50 videos at a time like a batch and then another 50 when the first group is done?

I know I can alter php.ini but I don't want to do that now

  • 写回答

1条回答 默认 最新

  • dougutuo9879 2015-01-05 19:22
    关注

    This is using the deprecated GData API, if you use the new Data API v3, while retrieving channel's uploaded videos, you can set maxresults and get your results with paging, so you can iterate through pages.

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序