Example #1
1
 /**
  * @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;
     }
 }
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.
        # Insert channel comment by omitting videoId.
        # Create a comment snippet with text.
        $commentSnippet = new Google_Service_YouTube_CommentSnippet();
        $commentSnippet->setTextOriginal($TEXT);
        # Create a top-level comment with snippet.
        $topLevelComment = new Google_Service_YouTube_Comment();
        $topLevelComment->setSnippet($commentSnippet);
        # Create a comment thread snippet with channelId and top-level comment.
        $commentThreadSnippet = new Google_Service_YouTube_CommentThreadSnippet();
        $commentThreadSnippet->setChannelId($CHANNEL_ID);
        $commentThreadSnippet->setTopLevelComment($topLevelComment);
        # Create a comment thread with snippet.
        $commentThread = new Google_Service_YouTube_CommentThread();
        $commentThread->setSnippet($commentThreadSnippet);
        // Call the YouTube Data API's commentThreads.insert method to create a comment.
        $channelCommentInsertResponse = $youtube->commentThreads->insert('snippet', $commentThread);
        # Insert video comment
        $commentThreadSnippet->setVideoId($VIDEO_ID);
        // Call the YouTube Data API's commentThreads.insert method to create a comment.
        $videoCommentInsertResponse = $youtube->commentThreads->insert('snippet', $commentThread);
        // Call the YouTube Data API's commentThreads.list method to retrieve video comment threads.
        $videoComments = $youtube->commentThreads->listCommentThreads('snippet', array('videoId' => $VIDEO_ID, 'textFormat' => 'plainText'));
        if (empty($videoComments)) {
            $htmlBody .= "<h3>Can\\'t get video comments.</h3>";