This is article 12 of the YouTube API With PHP series.
The update API call is used to update an existing Caption Track. The same restrictions of file size and mime-types apply as in the Insert call.
The Request URL is
PUT https://www.googleapis.com/upload/youtube/v3/captions
Parameters
- key (string) required. Your API key
- part (string) required. Comma separated. One of or all of the below:
- “id” – used if you are updating the Caption which will be visible to everyone.
- “snippet” – used if you are updating the Caption in draft status and will not be publicly visible.
- 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.
The Request Body must contain a JSON encoded Caption resource with the following fields set:
- id (string) the id of the existing Caption Track
- isDraft (boolean) Set to true if Caption should be in draft mode. Otherwise set to false
Most of the rules which apply to the insert call , are applicable here as well. The main difference is that you have to specify the id of the Caption resource that you are updating. If the id is wrong or missing the call will fail. You can get a list of all the Caption track ids of a video by executing the list call.
Here is sample code which updates an existing Caption track:
<?php error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED); set_time_limit(60 * 3); session_start(); $clientId = "**"; $clientSecret = "**"; $g_youtubeDataAPIKey = "**"; $videoId = "tJEVea-1jWo"; $_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"]; $snippet = new Snippet(); $snippet->isDraft = false; $snippet->videoId = $videoId; $snippet->language = "en"; $snippet->name = "English Captions Updated Twice"; // name should not be the same as last time $snippet->isDraft = false; $resource = new CaptionResource(); $resource->snippet = $snippet; $resource->kind = "youtube#caption"; $resource->id = "DzeIRcaZhlr_NWO8E7zqFDDMHN6Dvq-y7Y6VeSF84tzjm4N4oexdQgJfIcQ"; $data = $resource; $data = json_encode($data); // load caption file contents $fh = fopen("testcaption.en.vtt", "r");https://www.googleapis.com/youtube/v3/captions? if (!$fh) exit("Could not open caption file"); fclose($fh); $fileData = file_get_contents("testcaption.en.vtt"); $postData = $data; var_dump($postData); echo("<br>"); // make api request $url = "https://www.googleapis.com/upload/youtube/v3/captions?uploadType=resumable&part=snippet,id&sync=true&key=" . $g_youtubeDataAPIKey; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_HTTPHEADER=>array('Content-Type: application/json; charset=UTF-8', 'Authorization: OAuth ' . $accessToken, 'Content-Length: ' . strlen($postData), 'X-Upload-Content-Length: ' . filesize("testcaption.en.vtt"), 'x-upload-content-type: application/octet-stream' ), CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $url, CURLOPT_HEADER=>1, 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=>"POST", CURLOPT_POSTFIELDS=>$postData )); $resp = curl_exec($curl); curl_close($curl); echo("<hr>"); // get response headers to extract Upload id. $uploadId = null; $splitResponse = explode("\n",$resp); $headers = array(); $headers['status']=$splitResponse[0]; array_shift($splitResponse); foreach($splitResponse as $part){ $middle=explode(":",$part); $headers[trim($middle[0])] = trim($middle[1]); } foreach($headers as $key=>$value) { echo($key . " =". $value . "<br>"); if ($key == "X-GUploader-UploadID") $uploadId = $value; } if ($uploadId == null) { exit("Error. Could not obtain Upload id"); } echo("Upload id=" . $uploadId . "<br>"); // prepare curl for actual caption file upload $url = "https://www.googleapis.com/upload/youtube/v3/captions?uploadType=resumable&part=snippet,id&sync=true&key=" . $g_youtubeDataAPIKey . "&upload_id=" . $uploadId; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_HTTPHEADER=>array('Content-Type: application/octet-stream', 'Authorization: OAuth ' . $accessToken, 'Content-Length: ' . filesize("testcaption.en.vtt") ), 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=>"POST", CURLOPT_POSTFIELDS=>$fileData )); $resp = curl_exec($curl); curl_close($curl); var_dump($resp); //////////////////// class CaptionResource { public $snippet; public $kind; public $id; } class Snippet { public $videoId; public $language; public $name; public $isDraft; } ?>
Here is the output:
string(201) "{"snippet":{"videoId":"tJEVea-1jWo","language":"en","name":"English Captions Updated Twice","isDraft":false},"kind":"youtube#caption","id":"DzeIRcaZhlr_NWO8E7zqFDDMHN6Dvq-y7Y6VeSF84tzjm4N4oexdQgJfIcQ"}" status =HTTP/1.1 200 OK X-GUploader-UploadID =AEnB2UrYiBGQfO4HfMRR2rRGgxjzWxRUZ_VjF51eSrhTrUf0volTxpOZus1SfaPpmBb45EoekzdN6CP-2BD6kZdkX50GbtryBg Location =https Vary =X-Origin Cache-Control =no-cache, no-store, max-age=0, must-revalidate Pragma =no-cache Expires =Mon, 01 Jan 1990 00 Date =Thu, 25 May 2017 07 Content-Length =0 Server =UploadServer Content-Type =text/html; charset=UTF-8 Alt-Svc =quic=" = Upload id=AEnB2UrYiBGQfO4HfMRR2rRGgxjzWxRUZ_VjF51eSrhTrUf0volTxpOZus1SfaPpmBb45EoekzdN6CP-2BD6kZdkX50GbtryBg string(531) "{ "kind": "youtube#caption", "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/l0U8P2R6_Tg5rjSN2K0XPeHuqlc\"", "id": "axgUe6Et_P4isLwoI955G63ZxOiToJxi1IhwL_W9S9w61KPnmOvuOiUevcv8buAmn8R8Y0fmRm8=", "snippet": { "videoId": "tJEVea-1jWo", "lastUpdated": "2017-05-25T07:33:15.895Z", "trackKind": "standard", "language": "en", "name": "English Captions Updated Twice", "audioTrackType": "unknown", "isCC": false, "isLarge": false, "isEasyReader": false, "isDraft": false, "isAutoSynced": true, "status": "syncing" } } "
Again , as in the insert call, if you specify the name attribute in the snippet, it should not be the same as what is already there.
Leave a Reply