Using YouTube API to get all comments from a video with the JSON feed

后端 未结 2 620
予麋鹿
予麋鹿 2021-01-06 10:02

I\'m using the YouTube API to get comments for a video with a parameterized query like the following:

http://gdata.youtube.com/feeds/api/videos/theVideoID/comm

相关标签:
2条回答
  • 2021-01-06 10:46

    Use the 'orderby' parameter of the api and set it to 'published' to retrieve almost all the comments.

    https://gdata.youtube.com/feeds/api/videos/<videoID>/comments?max-results=50&alt=json&orderby=published
    

    You can still use the start-index parameter to loop through the comments but it is not a good idea.

    From the documentation: API responses use tags to identify pagination links for the previous and/or next page of entries in a feed. To avoid pagination problems, we recommend that you use these links to enable users to link to different pages of API results.

    If a feed contains a previous page of results, the API response will contain a tag with a rel attribute value of previous. If a feed contains a next page of results, the API response will contain a tag with a rel attribute value of next.

    https://developers.google.com/youtube/2.0/reference#Paging_through_Results

    This way you won't get any nested feeds. To get the next set of results simply use the link given on the previous page of results! Hope this helps. It worked for me!

    0 讨论(0)
  • 2021-01-06 10:47

    I just came across this question and I notice that its been quite some time when this was asked. But since nobody answered it yet, I think I should do that.

    What you should ideally do is, use Youtube's PHP API (using Zend_GData) and use the following code in PHP:

    <?php
    
        require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
    Zend_Loader::loadClass('Zend_Gdata_YouTube');
    
    $yt = new Zend_Gdata_YouTube();
    $yt->setMajorProtocolVersion(2);
    $video = parse_url("http://www.youtube.com/watch?v=K-ob8sr9ZX0");
    parse_str(urldecode($video['query']), $query);
    $videoId = $query['v'];
    
    $commentFeed = $yt->retrieveAllEntriesForFeed($yt->getVideoCommentFeed($videoId));
    
    foreach ($commentFeed as $commentEntry) {
        echo "Full text: " . $commentEntry->content->text . "<br />";
    }
    

    The key element here is the retrieveAllEntriesForFeed() method.

    Instead of echo-ing all the comments, you can construct a JSON and send it back to the waiting Javascript.

    It does not use the max-results or start-index, but does the job well without them.

    0 讨论(0)
提交回复
热议问题