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;
     }
 }
    $client->authenticate($_GET['code']);
    $_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';
        die('The session state did not match.');
    }
    $client->authenticate($_GET['code']);
    $_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.
        # 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);