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

相關問題