首先,我查看了其他所有標題。所有這些都過時了。我的意思是,他們使用舊的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代碼着色。
任何原因,你是這個手動,而不是用做Google PHP客戶端庫? https://github.com/google/google-api-php-client – DaImTo
你能延長你的答案嗎? – user5481342
現在你正在編碼這一切你自己。 Google爲PHP創建了一個庫,它將爲您完成所有這些工作。您正在更難於自己,那麼它需要https://developers.google.com/youtube/v3/code_samples/php#create_and_manage_comments – DaImTo