2017-02-22 99 views
0

首先,我查看了其他所有标题。所有这些都过时了。我的意思是,他们使用旧的api。如何从Youtube视频获得所有评论

我写的代码列出所有的意见与他们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,以及如何向他们展示从计算器PHP。

现在,是采取nextPageTokens没有问题。但我无法获得评论。

当我运行该脚本,它返回不同nextPageToken但意见是一样的,他们来自第一页。

我尝试添加足够的注释行。 对不起,我不能给php代码着色。

+0

任何原因,你是这个手动,而不是用做Google PHP客户端库? https://github.com/google/google-api-php-client – DaImTo

+0

你能延长你的答案吗? – user5481342

+1

现在你正在编码这一切你自己。 Google为PHP创建了一个库,它将为您完成所有这些工作。您正在更难于自己,那么它需要https://developers.google.com/youtube/v3/code_samples/php#create_and_manage_comments – DaImTo

回答

2

要调用commentThreads与参数&nextPagetoken=

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

+0

同样的结果:(令牌是好的。但是意见是一样的。 – user5481342

+1

对于你的代码中的视频ID SE0wDh_pILk,在使用totalResults为90,低于所要求的100。老实说,我不知道为什么在这种情况下,YouTube会返回nextPageToken,因为只有1页结果。 – johnh10

+0

视频中,页面显示“有超过200条评论,但在json中仅显示约100条 – user5481342