1

我很难让它按我想要的方式工作。我希望从播放列表中获取所有视频。目前我可以检索 20 个,但有些播放列表包含 100 多个视频。这是我遇到问题的地方。我在这里使用从另一个用户那里找到的以下代码,因为我已经用尽了我能想到的一切。

这将启动该过程。请注意,我通过特定 URL 调用 XML 提要,因为 Google 开发网站上关于我正在尝试做的事情的信息很少。

public function saveSpecificVideoFeed($id) {
    $url = 'https://gdata.youtube.com/feeds/api/playlists/' . $id . '?v=2';
    $feed = $this->yt->getPlaylistVideoFeed($url);
    $this->saveEntireFeed($feed, 1);
}

这就是我将上述函数传递给的内容:

public function saveEntireFeed($videoFeed, $counter) {
        foreach ($videoFeed as $videoEntry) {
            if (self::saveVideoEntry($videoEntry)) {
                $this->success++;
            } else {
                $this->failed++;
            }
            $counter++;
        }

        // See whether we have another set of results
        try {
            $videoFeed = $videoFeed->getNextFeed();
        } catch (Zend_Gdata_App_Exception $e) {
            echo $e->getMessage() . "<br/>";
            echo "Successfuly Pulled: <b>" . $this->success . "</b> Videos.<br/>";
            echo "Failed to Pull: <b>" . $this->failed . "</b> Videos.<br/>";
            echo "You Tryed to Insert: <b>" . $this->duplicate . "</b> Duplicate Videos.";
            return;
        }

        if ($videoFeed) {
            self::saveEntireFeed($videoFeed, $counter);
        }
    }

以下是我单独保存视频的方式:

private function saveVideoEntry($videoEntry) {
        if (self::videoExists($videoEntry->getVideoId())) {
            // Do nothing if it exists
        } else {

            $videoThumbnails = $videoEntry->getVideoThumbnails();

            $thumbs = null;
            foreach ($videoThumbnails as $videoThumbnail) {
                $thumbs .= $videoThumbnail['url'] . ',';
            }

            $binds = array(
                'title' => $videoEntry->getVideoTitle(),
                'videoId' => $videoEntry->getVideoId(),
                'updated' => $videoEntry->getUpdated(),
                'description' => $videoEntry->getVideoDescription(),
                'category' => $videoEntry->getVideoCategory(),
                'tags' => implode(", ", $videoEntry->getVideoTags()),
                'watchPage' => $videoEntry->getVideoWatchPageUrl(),
                'flashPlayerUrl' => $videoEntry->getFlashPlayerUrl(),
                'duration' => $videoEntry->getVideoDuration(),
                'viewCount' => $videoEntry->getVideoViewCount(),
                'thumbnail' => $thumbs,
            );

            $sql = "INSERT INTO $this->tblName (title, videoId, updated, description, category, tags, watchPage, flashPlayerUrl, duration, viewCount, thumbnail) 
            VALUES (:title, :videoId, :updated, :description, :category, :tags, :watchPage, :flashPlayerUrl, :duration, :viewCount, :thumbnail)";

            $sth = $this->db->prepare($sql);

            foreach ($binds as $key => $value) {
                if ($value == null) {
                    $value = '';
                }
                $sth->bindValue(":{$key}", $value);
            }

            if ($sth->execute()) {
                return true;
            } else {
                print_r($sth->errorInfo());
                return false;
            }
        }
    }

这是我从浏览器输出中得到的,以一种易于阅读的格式让我知道我从拉取中得到了什么:

表已创建并继续提取。未找到下一组结果的链接。成功拉取:20 个视频。拉取失败:0 个视频。您尝试插入:0 个重复视频。

然而,这是一个包含 36 个视频的播放列表,所以我的问题是访问剩余的视频。有没有更简单但没有记录的方法来做到这一点?任何帮助将不胜感激。

我已经尝试在请求 URL 中使用 max-results 和 start-index 元素,并在循环时将它们增加到所需的值,但这对 YouTube API 的 XML 输出没有影响。

任何帮助将不胜感激。

4

1 回答 1

1

所以我决定走一条不同的路线并使用以下代码:

<?php

include('HttpCurl.php');

class YouTube {

    public $url;
    private $content;
    private $videoId = array();
    private $Http,$Doc;

    function __construct() {
        $this->Http = new HttpCurl();
        $this->Doc = new DOMDocument();
    }

    /*
     * Sets url to strip the videos from;
     * Insert the full URL for the videos YouTube playlist like:
     * http://www.youtube.com/watch?v=saVE7pMhaxk&list=EC6F914D0CF944737A
     */

    public function setUrl($url) {
        $this->url = $url;
        if ($this->check($this->url)) {
            $this->getPage();
        }
    }

    private function check($item) {
        if (isset($item)) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * Grab the page that is needed
     */

    private function getPage() {
        $this->content = $this->Http->getContent($this->url);
        if($this->check($this->content)) {
            $this->getPlaylistVideos();
        }
    }

    /*
     * Parse page for desired result in our case this will default to the
     * playlist videos.
     */

    private function getPlaylistVideos() {
        $this->Doc->preserveWhiteSpace = false;
        // Load the url's contents into the DOM (the @ supresses any errors from invalid XML)
        if (@$this->Doc->loadHTML($this->content) == false) {
            die("Failed to load the document you specified.: " . $page);
        }

        $xpath = new DOMXPath($this->Doc);
        if ($hrefs = $xpath->query("//ol[@id='watch7-playlist-tray']//li")) {
            //echo "<br/>Grabbing Videos and playlists!";
            //Loop through each <a> and </a> tag in the dom and add it to the link array
            //$count = count($this->data['link']);
            foreach ($hrefs as $link) {
                $this->videoId[] = $link->getAttribute('data-video-id');
            }
            var_dump($this->videoId);
        }
    }

}

所以不是我想要的,而是返回视频的所有 id,以便我可以解析它们以获取来自 YouTube API 的完整数据。

于 2013-01-09T09:02:26.567 回答