This is article 23 of the YouTube API With PHP series.
The insert function creates a new top-level comment. To add a reply to an existing comment, use the comments.insert method instead. You can use this API call to insert comments for any Video or Channel.
The Request URL is
POST https://www.googleapis.com/youtube/v3/commentThreads
Parameters
- key (string) required. Your API key
- access_token (string) required in certain cases. This is the user Access token.
- part (string). Required. One or all of the following in comma separated fashion:
- “id”
- “snippet”
- “replies”
Example Requests
Insert a CommentThread in a Channel
https://www.googleapis.com/youtube/v3/commentThreads?key=xx&access_token=” . $accessToken . “&part=snippet
Response
On successful execution a CommentThread resource is returned in JSON format.
Sample code to insert a top level Comment Thread for a Video
<?php error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED); set_time_limit(60 * 3); session_start(); $g_youtubeDataAPIKey = "**"; $channelId = "UCnXmfpAZ1rLsg0Goh0bBHUA"; $videoId = "WNnNJSXHe6s"; $_SESSION["code_id"] = $_SERVER["PHP_SELF"]; if ($_SESSION["access_token"] == null || $_SESSION["access_token"] == "") { // check for oauth response header("Location: ../../init-login.php"); exit; } $accessToken = $_SESSION["access_token"]; // make api request $url = "https://www.googleapis.com/youtube/v3/commentThreads?key=" . $g_youtubeDataAPIKey . "&access_token=" . $accessToken . "&part=snippet"; $topLevelCommentSnippet = new TopLevelCommentSnippet(); $topLevelCommentSnippet->textOriginal = "This is a comment posted via the API"; $topLevelComment = new TopLevelComment(); $topLevelComment->snippet = $topLevelCommentSnippet; $snippet = new Snippet(); $snippet->videoId = $videoId; $snippet->topLevelComment = $topLevelComment; $snippet->isPublic = true; $commentThread = new CommentThread(); $commentThread->snippet = $snippet; $data = json_encode($commentThread); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_HTTPHEADER=>array('Content-Type: application/json'), CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $url, CURLOPT_USERAGENT => 'YouTube API Tester', CURLOPT_SSL_VERIFYPEER => 1, CURLOPT_SSL_VERIFYHOST=> 0, CURLOPT_CAINFO => "../../cert/cacert.pem", CURLOPT_CAPATH => "../../cert/cacert.pem", CURLOPT_FOLLOWLOCATION => TRUE, CURLOPT_CUSTOMREQUEST=>"POST", CURLOPT_POSTFIELDS=>$data )); $resp = curl_exec($curl); curl_close($curl); var_dump($resp); ////////////////////////////////// class CommentThread { public $snippet; } class Snippet { public $topLevelComment; public $isPublic; public $videoId; } class TopLevelComment { public $textOriginal; } class TopLevelCommentSnippet { public $textOriginal; } ?>
Here is the output:
string(1204) “{ “kind”: “youtube#commentThread”, “etag”: “\”m2yskBQFythfE4irbTIeOgYYfBU/kSR19kRoKQoN0H3RB3fdn1fPZGo\””, “id”: “z123uppgbl2bu3m5304cfb5ycuu5ufrqlpk0k”, “snippet”: { “channelId”: “UCnXmfpAZ1rLsg0Goh0bBHUA”, “videoId”: “WNnNJSXHe6s”, “topLevelComment”: { “kind”: “youtube#comment”, “etag”: “\”m2yskBQFythfE4irbTIeOgYYfBU/LXq8sJPgsfpmZxpyuBRww4qiy8Y\””, “id”: “z123uppgbl2bu3m5304cfb5ycuu5ufrqlpk0k”, “snippet”: { “authorDisplayName”: “Amit Sengupta”, “authorProfileImageUrl”: “https://yt3.ggpht.com/-HGpjiYmK2OY/AAAAAAAAAAI/AAAAAAAAAAA/1nRoeezR4HM/s28-c-k-no-mo-rj-c0xffffff/photo.jpg”, “authorChannelUrl”: “http://www.youtube.com/channel/UCnXmfpAZ1rLsg0Goh0bBHUA”, “authorChannelId”: { “value”: “UCnXmfpAZ1rLsg0Goh0bBHUA” }, “channelId”: “UCnXmfpAZ1rLsg0Goh0bBHUA”, “videoId”: “WNnNJSXHe6s”, “textDisplay”: “This is a comment posted via the API”, “textOriginal”: “This is a comment posted via the API”, “canRate”: true, “viewerRating”: “none”, “likeCount”: 0, “publishedAt”: “2017-05-28T08:21:27.000Z”, “updatedAt”: “2017-05-28T08:21:27.000Z” } }, “canReply”: true, “totalReplyCount”: 0, “isPublic”: true } } “
Here is how it looks on Youtube:
We set up the CommentThread structure using classes. Depending on whether we are inserting a CommentThread for a channel or a video, we either use videoId or channelId in the Snippet structure.
You have to be careful about the two snippet structures – one is within the top level CommentThread class. The other is within the TopLevelComment class. The TopLevelComment class represents a Comment resource , which is similar to a CommentThread resource.
Leave a Reply