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



首先,我看了所有其他标题。他们所有人都过时了。我的意思是,他们使用旧的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代码着色。

您正在使用参数&nextPagetoken=调用commentThreads

正确使用的参数是&pageToken=

这是递归的裸骨功能,可以返回视频中的所有注释

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;

    }

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

如果您必须收集视频的所有评论,并随机将其中一个用于彩票等。请使用这些网站 ->
https://www.randomcommentpicker.com
http://commentpicker.com/youtube.html

相关内容

  • 没有找到相关文章

最新更新