2017-02-22 8 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

사용이 아닌 수동으로하고있는 어떤 이유 구글 PHP 클라이언트 라이브러리? https://github.com/google/google-api-php-client – DaImTo

+0

답변을 연장 할 수 있습니까? – user5481342

+1

지금 바로이 코드를 모두 코딩하고 있습니다. 이 모든 작업을 Google for PHP에서 작성한 라이브러리가 있습니다. 스스로 힘들어지면 기능이 내 요청을 충족시킬 수없는 https://developers.google.com/youtube/v3/code_samples/php#create_and_manage_comments – DaImTo

답변

2

매개 변수 &nextPagetoken=을 사용하여 commentThreads으로 전화를 겁니다.

올바른 매개 변수는 &pageToken=입니다.

+0

같은 결과 : (토큰은 괜찮지 만 댓글은 동일합니다.) – user5481342

+1

코드에서 비디오 ID SE0wDh_pILk의 경우 totalResults는 요청한 100 개보다 적은 90입니다. 솔직히 이유를 모르겠습니다. 이 경우에는 결과의 페이지가 1 페이지이므로 YouTube는 nextPageToken을 반환합니다. 동영상에 – johnh10

+0

페이지에 "200 개가 넘는 댓글이 있지만, json에는 약 100 개만 표시됩니다. – user5481342