29 – YouTube Data API – Comments – setModerationStatus function

This is article 29 of the YouTube API With PHP series.

This function sets the moderation status of one or more comments. The API request must be authorized by the owner of the channel or video associated with the comments.

The Request URL is

POST https://www.googleapis.com/youtube/v3/comments/setModerationStatus

 

Parameters

  • key (string) required. Your API key
  • access_token (string) required in certain cases. This is the user Access token.
  • id (string). Required. The id of the comment to be marked as spam.
  • moderationStatus (string) required. Identifies the new moderation status of the specified comments. Acceptable values are:
    • “heldForReview” – Marks a comment as awaiting review by a moderator.
    • “published” – Clears a comment for public display.
    • “rejected” – Rejects a comment as being unfit for display. This action also effectively hides all replies to the rejected comment.
  • banAuthor (boolean) Optional. This lets you indicate that you want to automatically reject any additional comments written by the comment’s author. Set the parameter value to true to ban the author. This parameter is only valid if the moderationStatus parameter is also set to “rejected”.

 

Example Requests

Set moderation status for a comment

https://www.googleapis.com/youtube/v3/comments/setModerationStatus?key=xx&access_token=” . $accessToken . “&id=xx&moderationStatus=rejected

 

Response

On successful execution this method returns an HTTP 204 response code (No Content). If you specify a non-existent comment id then the call will fail.

Sample code to set the moderation status of a comment.

<?php
    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
    set_time_limit(60 * 3);
    session_start();
    
    $g_youtubeDataAPIKey = "**";

    $commentId = "z12jy3qp3q21e3se322ttjmaqlimxxohg04.1496034554121428";
    
    $_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/setModerationStatus?key=" . $g_youtubeDataAPIKey .
   			  "&access_token=" . $accessToken . "&id=" . $commentId . "&moderationStatus=rejected";

   	 
   	 $curl = curl_init();
   	 curl_setopt_array($curl, array(
   				 CURLOPT_RETURNTRANSFER => 1,
   				 CURLOPT_URL => $url,
   				 CURLOPT_USERAGENT => 'YouTube API Tester',
   				 CURLOPT_SSL_VERIFYPEER => 1,
   				 CURLOPT_SSL_VERIFYHOST=> 0,
   				 CURLOPT_HEADER=>1,
   				 CURLOPT_CAINFO => "../../cert/cacert.pem",
   				 CURLOPT_CAPATH => "../../cert/cacert.pem",
   				 CURLOPT_FOLLOWLOCATION => TRUE,
   				 CURLOPT_CUSTOMREQUEST=>"POST",
   				 CURLOPT_POSTFIELDS=>""

   				 ));
   	 $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(344) “HTTP/1.1 204 No Content Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: Mon, 01 Jan 1990 00:00:00 GMT Date: Mon, 29 May 2017 07:20:50 GMT ETag: “m2yskBQFythfE4irbTIeOgYYfBU/7MMpT8ZDo9n_QH3wl-DdyiOMn04″ Vary: Origin Vary: X-Origin Server: GSE Alt-Svc: quic=”:443″; ma=2592000; v=”38,37,36,35″ “

The cURL request should have the following things:

  • Set CURLOPT_HEADER to 1 as the response will contain a header
  • Set CURLOPT_CUSTOMREQUEST to “POST” even though there is no data being sent.
  • Set CURLOPT_POSTFIELDS to an empty string.

Be the first to comment

Leave a Reply

Your email address will not be published.


*