28 – YouTube Data API – Comments – markAsSpam function

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

markAsSpam function expresses the current user’s opinion that one or more comments should be flagged as spam. This is the API equivalent of marking a comment or a reply as Spam in YouTube. You can use this for any public comment or reply in any publicly visible Channel or Video. This call requires user-authentication as anonymous users are not allowed to mark comments as spam.

Note that this wont automatically mark the comment as spam. It will just send notification for that.

The Request URL is

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

 

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 update.

 

Example Requests

Mark a comment as spam.

https://www.googleapis.com/youtube/v3/comments/markAsSpam?key=xx&access_token=xx&id=xx

Response

On successful execution his 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 mark a comment as spam:

<?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/markAsSpam?key=" . $g_youtubeDataAPIKey .
   			  "&access_token=" . $accessToken . "&id=" . $commentId;

   	 $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 06:48:39 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.


*