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

后端 未结 2 702
予麋鹿
予麋鹿 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: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:

    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 . "
    "; }

    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.

提交回复
热议问题