如何从Youtube视频中获取所有评论

时间:2017-02-22 11:55:10

标签: php json youtube youtube-api youtube-data-api

首先,我看了所有其他标题。所有这些都过时了。我的意思是,他们使用旧的api。

我编写了一个代码,用nextPageToken

列出所有评论
<?php
$url      = "SE0wDh_pILk"; // Youtube video ID
$ytkey    = "IzaSyCaRXmJ9XDC4XucAZCzXx7hisCtYEH0mNs"; //"IzaSyBuu-rnbmPAj1DjR6WmyxGmpmQKz8aTXbw"  Your api key
$nextPage = ""; // Next Page Token for get comments of next Page.
//$i =0; // DO NOT CHANGE


for ($i = 0; $i < 5; $i++) {
    $str = file_get_contents("https://www.googleapis.com/youtube/v3/commentThreads?key=" . "$ytkey" . "&textFormat=plainText&part=snippet&videoId=" . "$url" . "&maxResults=100&nextPagetoken=" . "$nextPage");

    $json = json_decode($str, true); // decode the JSON into an associative array
    //echo '<pre>' . print_r($json, true) . '</pre>'; // Print json data as array structer ..

    echo "$i - " . "Next Page Token : " . $json['nextPageToken']; // Take the next Page Token for get next 100 comment...
    echo "<hr>"; // Divider


    $nextPage = $json['nextPageToken']; // Take token for next query
    // print comments.

    foreach ($json['items'] as $val) { // Loop for list comments...
        $author  = $val['snippet']['topLevelComment']['snippet']['authorDisplayName']; //Get Comment Author Name.
        //$author_url = $val['snippet']['topLevelComment']['snippet']['authorChannelUrl']; //Get Comment Author URL.
        //$author_thumbnail_url = $val['snippet']['topLevelComment']['snippet']['authorProfileImageUrl']; //Get Comment Author Thumbnail URL.
        $comment = $val['snippet']['topLevelComment']['snippet']['textDisplay']; //Get Comment Content.

        echo "<span style='color:red';>" . "$author" . "</span>" . " --> " . "$comment"; // Author and comment
        echo "<hr>"; // Divider
    }

}

echo "Process over. ";
?>

我学习如何解析json以及如何在stackoverflow上显示它们。

现在,使用nextPageTokens没有问题。但我无法得到评论。

当我运行脚本时,它返回不同的nextPageToken但注释相同,它们来自第一页。

我尝试添加足够的评论行。 对不起,我无法为php代码着色。

3 个答案:

答案 0 :(得分:2)

您使用参数commentThreads呼叫&nextPagetoken=

要使用的正确参数是&pageToken=

答案 1 :(得分:0)

这是一个递归的裸露骨头功能,可返回视频中的所有评论

public function getAllComments($videoId,$pageToken=null,$maxResults){
        $url = "https://www.googleapis.com/youtube/v3/commentThreads";

        static $all =[];
        $params =[
            'key' => 'your-key',
            'part' => 'snippet',
            'maxResults' => $maxResults,
            'videoId' => $videoId,
            'pageToken' => $pageToken
        ];

        $call = $url.'?'.http_build_query($params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $call);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        $data = NULL;
        $data = json_decode($output,true);
        $all[] = $data;
        if(isset($data['nextPageToken'])){
            if($data['nextPageToken'] != NULL ){
                $pageToken = $data['nextPageToken'];
                getAllComments($videoId,$pageToken,$maxResults);
            }
        }
        curl_close($ch);
        return $all;


    }

答案 2 :(得分:-1)

最后,我找到了一个可以做我想做的网站。

如果您必须收集视频的所有评论并随机抽取其中一个用于抽奖等,请使用这些网站 - &gt;
https://www.randomcommentpicker.com
http://commentpicker.com/youtube.html