使用YouTube Gdata API检索整个YouTube播放列表(所有视频)

时间:2013-01-09 00:03:36

标签: php youtube-api zend-gdata

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

这开始了这个过程。请注意,我通过特定的URL调用XML提要,因为Googles Dev网站上的信息很少,我正在尝试做什么。

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输出没有影响。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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中的完整数据。