This is article 26 of the YouTube API With PHP series.
The insert function creates a reply to an existing comment. To create a top-level comment, use the commentThreads.insert method. You can use this API to post a reply to any comment on any publicly visible Video or Channel. This call requires user authentication as an anonymous user cannot post replies.
The Request URL is
POST https://www.googleapis.com/youtube/v3/comments
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”
Example Request
Insert a reply to a Comment
https://www.googleapis.com/youtube/v3/comments?key=xx&access_token=xx&part=snippet,id
Response
On successful execution a Comment resource is returned in JSON format.
Sample code to insert a reply to a Comment
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
set_time_limit(60 * 3);
session_start();
$g_youtubeDataAPIKey = "***";
$_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/comments?key=" . $g_youtubeDataAPIKey .
"&access_token=" . $accessToken . "&part=snippet,id";
$snippet = new Snippet();
$snippet->textOriginal = "This is posted as a spam comment";
$snippet->parentId = "z12jy3qp3q21e3se322ttjmaqlimxxohg04";
$comment = new Comment();
$comment->snippet = $snippet;
$data = json_encode($comment);
$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 Comment {
public $snippet;
}
class Snippet {
public $parentId;
public $textOriginal;
}
?>
Here is the output:
string(848) “{ “kind”: “youtube#comment”, “etag”: “\”m2yskBQFythfE4irbTIeOgYYfBU/wGMttOviqnWjcXd-4JsFSxqn62Y\””, “id”: “z12jy3qp3q21e3se322ttjmaqlimxxohg04.1496039501396482”, “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” }, “textDisplay”: “Again posting a reply to a comment via the API”, “textOriginal”: “Again posting a reply to a comment via the API”, “parentId”: “z12jy3qp3q21e3se322ttjmaqlimxxohg04”, “canRate”: true, “viewerRating”: “none”, “likeCount”: 0, “publishedAt”: “2017-05-29T06:31:41.000Z”, “updatedAt”: “2017-05-29T06:31:41.000Z” } } “
Leave a Reply