This is article 27 of the YouTube API With PHP series.
The update function modifies a Comment. This requires user authentication as you can only update comments that you have inserted. You cannot update other people’s comments.
The Request URL is
PUT 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 Requests
Update an existing 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 update a Comment.
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
set_time_limit(60 * 3);
session_start();
$g_youtubeDataAPIKey = "**";
$commentId = "z12jy3qp3q21e3se322ttjmaqlimxxohg04.1496032470067447";
$_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 via the API and has been updated.";
$comment = new Comment();
$comment->snippet = $snippet;
$comment->id = $commentId;
$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=>"PUT",
CURLOPT_POSTFIELDS=>$data
));
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
//////////////////////////////////
class Comment {
public $snippet;
public $id;
}
class Snippet {
public $textOriginal;
}
?>
Here is the output:
string(852) “{ “kind”: “youtube#comment”, “etag”: “\”m2yskBQFythfE4irbTIeOgYYfBU/661I-lvcQipMcghf5r4kg2aNd00\””, “id”: “z12jy3qp3q21e3se322ttjmaqlimxxohg04.1496032470067447”, “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”: “This is posted via the API and has been updated.”, “textOriginal”: “This is posted via the API and has been updated.”, “parentId”: “z12jy3qp3q21e3se322ttjmaqlimxxohg04”, “canRate”: true, “viewerRating”: “none”, “likeCount”: 0, “publishedAt”: “2017-05-29T04:34:30.000Z”, “updatedAt”: “2017-05-29T06:36:57.000Z” } } “
The following things are required in the cURL parameters:
- Set CURLOPT_CUSTOMREQUEST to PUT as this is not a POST.
Leave a Reply