/** * @param $videoId * @param $commentText * @param null $channelId * * @return bool */ public function comment($videoId, $commentText, $channelId = null) { try { /* * $commentText = 'This is great'; * $channelId = "UC8p8tbo9-FNLIrvmGnPzVUQ"; * $videoId = "sKFCCRVO-fc"; */ $commentSnippet = new \Google_Service_YouTube_CommentSnippet(); $commentSnippet->setTextOriginal($commentText); $topLevelComment = new \Google_Service_YouTube_Comment(); $topLevelComment->setSnippet($commentSnippet); $commentThreadSnippet = new \Google_Service_YouTube_CommentThreadSnippet(); $commentThreadSnippet->setTopLevelComment($topLevelComment); $commentThreadSnippet->setVideoId($videoId); // $commentThreadSnippet->setChannelId($CHANNEL_ID); $commentThread = new \Google_Service_YouTube_CommentThread(); $commentThread->setSnippet($commentThreadSnippet); $videoCommentInsertResponse = $this->youtube->commentThreads->insert('snippet', $commentThread); if ($videoCommentInsertResponse) { return true; } return false; } catch (\Google_Service_Exception $e) { return false; } catch (\Google_Exception $e) { return false; } }
$_SESSION['token'] = $client->getAccessToken(); header('Location: ' . $redirect); } if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); } // Check to ensure that the access token was successfully acquired. if ($client->getAccessToken()) { try { # All the available methods are used in sequence just for the sake of an example. // Call the YouTube Data API's commentThreads.list method to retrieve video comment threads. $videoCommentThreads = $youtube->commentThreads->listCommentThreads('snippet', array('videoId' => $VIDEO_ID, 'textFormat' => 'plainText')); $parentId = $videoCommentThreads[0]['id']; # Create a comment snippet with text. $commentSnippet = new Google_Service_YouTube_CommentSnippet(); $commentSnippet->setTextOriginal($TEXT); $commentSnippet->setParentId($parentId); # Create a comment with snippet. $comment = new Google_Service_YouTube_Comment(); $comment->setSnippet($commentSnippet); # Call the YouTube Data API's comments.insert method to reply to a comment. # (If the intention is to create a new top-level comment, commentThreads.insert # method should be used instead.) $commentInsertResponse = $youtube->comments->insert('snippet', $comment); // Call the YouTube Data API's comments.list method to retrieve existing comment replies. $videoComments = $youtube->comments->listComments('snippet', array('parentId' => $parentId, 'textFormat' => 'plainText')); if (empty($videoComments)) { $htmlBody .= "<h3>Can\\'t get video comments.</h3>"; } else { $videoComments[0]['snippet']['textOriginal'] = 'updated'; // Call the YouTube Data API's comments.update method to update an existing comment.