14 – YouTube Data API – Captions – delete function

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

The delete call deletes a specified Caption track.

 The Request URL is

 DELETE https://www.googleapis.com/youtube/v3/captions/

Parameters

  • key (string) required. Your API key
  • id (string) required This is added as a suffix to the GET url. This has to be a valid id for an existing Caption track.
  • onBehalfOfContentOwner (string) optional. This is relevant only for YouTube Channel Partners. For this parameter, the API request URL should have user authentication.We will not be exploring this option.

Response

On a successful call, no content is returned – only HTTP status code 204 is returned.

This call requires user-authentication so you can only delete Caption tracks of your own videos. You need a valid Caption Track id. You can get a list of all the Caption tracks of a video by running the list call.

Here is sample code which deletes a Caption track:

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

    $clientId = "***";
    $clientSecret = "***";
    $g_youtubeDataAPIKey = "***";

    $captionId =  "axgUe6Et_P4isLwoI955G63ZxOiToJxi1IhwL_W9S9w61KPnmOvuOiUevcv8buAmn8R8Y0fmRm8=";
    
    $_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/captions/?id=" . $captionId . "&key=" .
   			 $g_youtubeDataAPIKey . "&access_token=" . $accessToken;
   	 $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=>"DELETE"
   				 ));
   	 $resp = curl_exec($curl);
   	 curl_close($curl);
   	 var_dump($resp);
?>

Here is the output:

string(341) "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: Thu, 25 May 2017 07:50:21 GMT ETag: "m2yskBQFythfE4irbTIeOgYYfBU/7MMpT8ZDo9n_QH3wl-DdyiOMn04" 
Vary: Origin Vary: X-Origin Server: GSE Alt-Svc: quic=":443"; 
ma=2592000; v="37,36,35" "


Since this API calls does an HTTP DELETE command which is different from an HTTP POST, we specify  CURLOPT_CUSTOMREQUEST as “DELETE”. We also set CURLOPT_HEADER to 1 as we need to check for the 204 response code, which signifies successful deletion.

Be the first to comment

Leave a Reply

Your email address will not be published.


*