Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mvnobrega's avatar

How to insert featured comments with Youtube API

I've searched and posted this question on other forums, but I didn't get an answer. Hope someone here has worked with the youtube API and can help me...

I'm inserting videos on youtube using API in PHP version. But I'm not able to include a highlight comment in the videos. In the youtube documentation the example is in json , and as I'm doing it in php it just doesn't work.

See part of my code is like this:

// Create a snippet with title, description, tags and category ID
    // Create an asset resource and set its snippet metadata and type.
    // This example sets the video's title, description, keyword tags, and
    // video category.
    $snippet = new Google_Service_YouTube_VideoSnippet();

    $snippet->setTitle("Cats are cute #".$filme['Data ID']);


    $snippet->setDescription($filme['titulo']);

    $snippet->setTags('cats','cutecats','cute');

    

    // Numeric video category. See
    // https://developers.google.com/youtube/v3/docs/videoCategories/list
    $snippet->setCategoryId("19");

    // Set the video's status to "public". Valid statuses are "public",
    // "private" and "unlisted".
    $status = new Google_Service_YouTube_VideoStatus();
    $status->privacyStatus = "public";

    // Associate the snippet and status objects with a new video resource.
    $video = new Google_Service_YouTube_Video();
    $video->setSnippet($snippet);
    $video->setStatus($status);

And the youtube suggestion to include comments using the API is this:

{
  "snippet": {
   "channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
   "topLevelComment": {
    "snippet": {
      "textOriginal": "This video is awesome!"
    }
   },
   "videoId": "MILSirUni5E"
  }
 }

I tried like this and it didn't work: $snippet->textOriginal('My comment');

How can I submit a featured comment using youtube API in php?

0 likes
5 replies
mvnobrega's avatar

@Tray2 Thanks for the suggestion, but it didn't help me much. My current code merges vignettes and creates thumbnail before uploading to youtube. If I change to another format I would have to redo everything and I don't have time for that right now.

mvnobrega's avatar

I found the solution and will leave it here for anyone who needs it. Just insert the following snippet after completing the video upload. Then just retrieve the id of the video sent and send the comment:

$videoId = $status['id'];



    $commentSnippet = new Google_Service_YouTube_CommentSnippet();
    $commentSnippet->setTextOriginal('Your comment here');
    $topLevelComment = new Google_Service_YouTube_Comment();
    $topLevelComment->setSnippet($commentSnippet);
    $commentThreadSnippet = new Google_Service_YouTube_CommentThreadSnippet();
    $commentThreadSnippet->setTopLevelComment($topLevelComment);
    $commentThread = new Google_Service_YouTube_CommentThread();
    $commentThread->setSnippet($commentThreadSnippet);
    $commentThreadSnippet->setVideoId($videoId);
    $videoCommentInsertResponse = $youtube->commentThreads->insert('snippet', $commentThread);

Please or to participate in this conversation.