This is article 24 of the YouTube API With PHP series.
The update function modifies the top-level comment in a comment thread. This call requires user authentication so you can only update CommentThreads of your own videos.
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
Update 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 update a CommentThread:
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
set_time_limit(60 * 3);
session_start();
$g_youtubeDataAPIKey = "***";
$videoId = "WNnNJSXHe6s";
$commentId = "z12aentgsyvmybkty22ttjmaqlimxxohg04";
$_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 and is now updated";
$topLevelComment = new TopLevelComment();
$topLevelComment->snippet = $topLevelCommentSnippet;
$topLevelComment->id = $commentId;
$snippet = new Snippet();
$snippet->topLevelComment = $topLevelComment;
$commentThread = new CommentThread();
$commentThread->snippet = $snippet;
$commentThread->id = $commentId;
$data = json_encode($commentThread);
var_dump($data);
$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=>"PUT",
CURLOPT_POSTFIELDS=>$data
));
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
//////////////////////////////////
class CommentThread {
public $snippet;
public $id;
}
class Snippet {
public $topLevelComment;
}
class TopLevelComment {
public $id;
}
class TopLevelCommentSnippet {
public $textOriginal;
}
?>
Here is the output:
string(204) “{“snippet”:{“topLevelComment”:{“id”:”z12aentgsyvmybkty22ttjmaqlimxxohg04″,”snippet”:{“textOriginal”:”This is a comment posted via the API and is now updated”}}},”id”:”z12aentgsyvmybkty22ttjmaqlimxxohg04″}” string(1150) “{ “kind”: “youtube#commentThread”, “etag”: “\”m2yskBQFythfE4irbTIeOgYYfBU/5CaduM8n_neBmD5p_YiucaeCaso\””, “id”: “z12aentgsyvmybkty22ttjmaqlimxxohg04”, “snippet”: { “videoId”: “WNnNJSXHe6s”, “topLevelComment”: { “kind”: “youtube#comment”, “etag”: “\”m2yskBQFythfE4irbTIeOgYYfBU/BqPbKslzhT97hwES5miEGBl6Blg\””, “id”: “z12aentgsyvmybkty22ttjmaqlimxxohg04”, “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” }, “videoId”: “WNnNJSXHe6s”, “textDisplay”: “This is a comment posted via the API and is now updated”, “textOriginal”: “This is a comment posted via the API and is now updated”, “canRate”: true, “viewerRating”: “none”, “likeCount”: 0, “publishedAt”: “2017-05-28T06:04:32.000Z”, “updatedAt”: “2017-05-28T09:19:33.000Z” } }, “canReply”: true, “totalReplyCount”: 0, “isPublic”: true } } “
Here is how the comment looks in YouTube:

Leave a Reply