{"id":2828,"date":"2017-07-01T04:46:56","date_gmt":"2017-07-01T04:46:56","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2828"},"modified":"2017-07-01T04:46:56","modified_gmt":"2017-07-01T04:46:56","slug":"12-youtube-data-api-captions-update-function","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2017\/07\/01\/12-youtube-data-api-captions-update-function\/","title":{"rendered":"12 &#8211; YouTube Data API &#8211; Captions &#8211; update function"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPhp.js\"><\/script>\n<p style=\"text-align: center;\"><strong>This is article 12 of the YouTube API With PHP series.<\/strong><\/p>\n<p class=\"NormalTextDroid\">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.<\/p>\n<p class=\"NormalTextDroid\">\u00a0The Request URL is<\/p>\n<pre class=\"PHPCode\"><span style=\"color: #808080;\">\u00a0PUT https:\/\/www.googleapis.com\/upload\/youtube\/v3\/captions\r\n\r\n\r\n<\/span><\/pre>\n<p><strong>Parameters<\/strong><\/p>\n<ul>\n<li><strong>key<\/strong> (string) required. Your API key<\/li>\n<li><strong>part<\/strong> (string) required. Comma separated. One of or all of the below:\n<ul>\n<li>\u201cid\u201d &#8211; used if you are updating the Caption which will be visible to everyone.<\/li>\n<li>\u201csnippet\u201d &#8211; used if you are updating the Caption in draft status and will not be publicly visible.<\/li>\n<\/ul>\n<\/li>\n<li><strong>onBehalfOfContentOwner<\/strong> (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.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>The Request Body must contain a JSON encoded Caption resource with the following fields set:<\/p>\n<ul>\n<li>id (string) the id of the existing Caption Track<\/li>\n<li>isDraft (boolean) Set to true if Caption should be in draft mode. Otherwise set to false<\/li>\n<\/ul>\n<p>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.<\/p>\n<p>Here is sample code which updates an existing Caption track:<br \/>\n<pre class=\"brush: php\">&lt;?php\r\n    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);\r\n    set_time_limit(60 * 3);\r\n    session_start();\r\n\r\n    $clientId = &quot;**&quot;;\r\n    $clientSecret = &quot;**&quot;;\r\n    $g_youtubeDataAPIKey = &quot;**&quot;;\r\n\r\n    $videoId =  &quot;tJEVea-1jWo&quot;;\r\n    \r\n    $_SESSION[&quot;code_id&quot;] = $_SERVER[&quot;PHP_SELF&quot;];\r\n    \r\n    if ($_SESSION[&quot;access_token&quot;] == null || $_SESSION[&quot;access_token&quot;] == &quot;&quot;) {\r\n   \t \/\/ check for oauth response\r\n   \t header(&quot;Location: ..\/..\/init-login.php&quot;);\r\n   \t exit;\r\n    }\r\n\r\n\r\n   \t $accessToken = $_SESSION[&quot;access_token&quot;];\r\n   \t \r\n   \t $snippet  = new Snippet();\r\n   \t $snippet-&gt;isDraft = false;\r\n   \t $snippet-&gt;videoId = $videoId;\r\n   \t $snippet-&gt;language = &quot;en&quot;;\r\n   \t $snippet-&gt;name = &quot;English Captions Updated Twice&quot;; \/\/ name should not be the same as last time\r\n   \t $snippet-&gt;isDraft = false;\r\n\r\n   \t \r\n   \t $resource = new CaptionResource();\r\n   \t $resource-&gt;snippet = $snippet;\r\n   \t $resource-&gt;kind = &quot;youtube#caption&quot;;\r\n   \t $resource-&gt;id = &quot;DzeIRcaZhlr_NWO8E7zqFDDMHN6Dvq-y7Y6VeSF84tzjm4N4oexdQgJfIcQ&quot;;\r\n   \t \r\n   \t $data = $resource;\r\n   \t $data = json_encode($data);\r\n\r\n   \t \/\/ load caption file contents\r\n   \t $fh = fopen(&quot;testcaption.en.vtt&quot;, &quot;r&quot;);https:\/\/www.googleapis.com\/youtube\/v3\/captions?\r\n   \t if (!$fh)\r\n   \t\t exit(&quot;Could not open caption file&quot;);\r\n   \t fclose($fh);\r\n   \t $fileData = file_get_contents(&quot;testcaption.en.vtt&quot;);\r\n\r\n   \t $postData = $data;\r\n   \t var_dump($postData);\r\n   \t echo(&quot;&lt;br&gt;&quot;);\r\n\r\n   \t \/\/ make api request\r\n   \t $url = &quot;https:\/\/www.googleapis.com\/upload\/youtube\/v3\/captions?uploadType=resumable&amp;part=snippet,id&amp;sync=true&amp;key=&quot; .\r\n   \t\t\t $g_youtubeDataAPIKey;\r\n   \t $curl = curl_init();\r\n   \t curl_setopt_array($curl, array(\r\n   \t\t\t\t CURLOPT_HTTPHEADER=&gt;array(&#039;Content-Type: application\/json; charset=UTF-8&#039;,\r\n   \t\t\t\t\t  &#039;Authorization: OAuth &#039; . $accessToken,\r\n   \t\t\t\t\t  &#039;Content-Length: &#039; .  strlen($postData),\r\n   \t\t\t\t\t  &#039;X-Upload-Content-Length: &#039; . filesize(&quot;testcaption.en.vtt&quot;),\r\n   \t\t\t\t\t  &#039;x-upload-content-type: application\/octet-stream&#039;\r\n   \t\t\t\t\t\t\t ),\r\n   \t\t\t\t CURLOPT_RETURNTRANSFER =&gt; 1,\r\n   \t\t\t\t CURLOPT_URL =&gt; $url,\r\n   \t\t\t\t CURLOPT_HEADER=&gt;1,\r\n   \t\t\t\t CURLOPT_USERAGENT =&gt; &#039;YouTube API Tester&#039;,\r\n   \t\t\t\t CURLOPT_SSL_VERIFYPEER =&gt; 1,\r\n   \t\t\t\t CURLOPT_SSL_VERIFYHOST=&gt; 0,\r\n   \t\t\t\t CURLOPT_CAINFO =&gt; &quot;..\/..\/cert\/cacert.pem&quot;,\r\n   \t\t\t\t CURLOPT_CAPATH =&gt; &quot;..\/..\/cert\/cacert.pem&quot;,\r\n   \t\t\t\t CURLOPT_FOLLOWLOCATION =&gt; TRUE,\r\n   \t\t\t\t CURLOPT_CUSTOMREQUEST=&gt;&quot;POST&quot;,\r\n   \t\t\t\t CURLOPT_POSTFIELDS=&gt;$postData\r\n   \t\t\t\t ));\r\n   \t $resp = curl_exec($curl);\r\n\r\n   \t curl_close($curl);\r\n   \t echo(&quot;&lt;hr&gt;&quot;);\r\n   \t \r\n   \t \/\/ get response headers to extract Upload id.\r\n   \t $uploadId = null;   \t \r\n   \t $splitResponse = explode(&quot;\\n&quot;,$resp);\r\n   \t $headers = array();\r\n   \t $headers[&#039;status&#039;]=$splitResponse[0];\r\n   \t array_shift($splitResponse);\r\n   \t foreach($splitResponse as $part){\r\n   \t \t$middle=explode(&quot;:&quot;,$part);\r\n   \t \t$headers[trim($middle[0])] = trim($middle[1]);\r\n   \t }\r\n   \t foreach($headers as $key=&gt;$value) {\r\n   \t\techo($key . &quot; =&quot;. $value . &quot;&lt;br&gt;&quot;);\r\n   \t if ($key == &quot;X-GUploader-UploadID&quot;)\r\n   \t\t $uploadId = $value;\r\n   \t }\r\n   \t \r\n   \t if ($uploadId == null) {\r\n   \t\t exit(&quot;Error. Could not obtain Upload id&quot;);\r\n   \t }\r\n   \t echo(&quot;Upload id=&quot; . $uploadId . &quot;&lt;br&gt;&quot;);\r\n\r\n   \t \/\/ prepare curl for actual caption file upload\r\n   \t $url = &quot;https:\/\/www.googleapis.com\/upload\/youtube\/v3\/captions?uploadType=resumable&amp;part=snippet,id&amp;sync=true&amp;key=&quot; . $g_youtubeDataAPIKey . &quot;&amp;upload_id=&quot; . $uploadId;\r\n\r\n   \t $curl = curl_init();\r\n   \t curl_setopt_array($curl, array(\r\n   \t\t\t\t CURLOPT_HTTPHEADER=&gt;array(&#039;Content-Type: application\/octet-stream&#039;,\r\n   \t\t\t\t\t  &#039;Authorization: OAuth &#039; . $accessToken,\r\n   \t\t\t\t\t  &#039;Content-Length: &#039; . filesize(&quot;testcaption.en.vtt&quot;)\r\n   \t\t\t\t\t\t\t ),\r\n   \t\t\t\t CURLOPT_RETURNTRANSFER =&gt; 1,\r\n   \t\t\t\t CURLOPT_URL =&gt; $url,\r\n   \t\t\t\t CURLOPT_USERAGENT =&gt; &#039;YouTube API Tester&#039;,\r\n   \t\t\t\t CURLOPT_SSL_VERIFYPEER =&gt; 1,\r\n   \t\t\t\t CURLOPT_SSL_VERIFYHOST=&gt; 0,\r\n   \t\t\t\t CURLOPT_CAINFO =&gt; &quot;..\/..\/cert\/cacert.pem&quot;,\r\n   \t\t\t\t CURLOPT_CAPATH =&gt; &quot;..\/..\/cert\/cacert.pem&quot;,\r\n   \t\t\t\t CURLOPT_FOLLOWLOCATION =&gt; TRUE,\r\n   \t\t\t\t CURLOPT_CUSTOMREQUEST=&gt;&quot;POST&quot;,\r\n   \t\t\t\t CURLOPT_POSTFIELDS=&gt;$fileData\r\n   \t\t\t\t ));\r\n   \t $resp = curl_exec($curl);\r\n\r\n   \t curl_close($curl);\r\n   \t var_dump($resp);\r\n\r\n\r\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\n\r\n    class CaptionResource {\r\n   \t public $snippet;\r\n   \t public $kind;\r\n   \t public $id;\r\n    }\r\n\r\n    class Snippet {\r\n   \t public $videoId;\r\n   \t public $language;\r\n   \t public $name;\r\n   \t public $isDraft;\r\n\r\n    }\r\n?&gt;\r\n<\/pre><\/p>\n<p class=\"NormalTextDroid\">Here is the output:<\/p>\n<pre><span style=\"color: #999999;\">string(201) \"{\"snippet\":{\"videoId\":\"tJEVea-1jWo\",\"language\":\"en\",\"name\":\"English Captions Updated Twice\",\"isDraft\":false},\"kind\":\"youtube#caption\",\"id\":\"DzeIRcaZhlr_NWO8E7zqFDDMHN6Dvq-y7Y6VeSF84tzjm4N4oexdQgJfIcQ\"}\"<\/span>\r\n<span style=\"color: #999999;\">status =HTTP\/1.1 200 OK<\/span>\r\n<span style=\"color: #999999;\">X-GUploader-UploadID =AEnB2UrYiBGQfO4HfMRR2rRGgxjzWxRUZ_VjF51eSrhTrUf0volTxpOZus1SfaPpmBb45EoekzdN6CP-2BD6kZdkX50GbtryBg<\/span>\r\n<span style=\"color: #999999;\">Location =https<\/span>\r\n<span style=\"color: #999999;\">Vary =X-Origin<\/span>\r\n<span style=\"color: #999999;\">Cache-Control =no-cache, no-store, max-age=0, must-revalidate<\/span>\r\n<span style=\"color: #999999;\">Pragma =no-cache<\/span>\r\n<span style=\"color: #999999;\">Expires =Mon, 01 Jan 1990 00<\/span>\r\n<span style=\"color: #999999;\">Date =Thu, 25 May 2017 07<\/span>\r\n<span style=\"color: #999999;\">Content-Length =0<\/span>\r\n<span style=\"color: #999999;\">Server =UploadServer<\/span>\r\n<span style=\"color: #999999;\">Content-Type =text\/html; charset=UTF-8<\/span>\r\n<span style=\"color: #999999;\">Alt-Svc =quic=\"<\/span>\r\n<span style=\"color: #999999;\">=<\/span>\r\n<span style=\"color: #999999;\">Upload id=AEnB2UrYiBGQfO4HfMRR2rRGgxjzWxRUZ_VjF51eSrhTrUf0volTxpOZus1SfaPpmBb45EoekzdN6CP-2BD6kZdkX50GbtryBg<\/span>\r\n<span style=\"color: #999999;\">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\" } } \"<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p class=\"NormalTextDroid\">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.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>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 <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2017\/07\/01\/12-youtube-data-api-captions-update-function\/\" title=\"12 &#8211; YouTube Data API &#8211; Captions &#8211; update function\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2727,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[318],"tags":[],"class_list":["post-2828","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-youtube-api-with-php"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2828"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2828\/revisions"}],"predecessor-version":[{"id":2830,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2828\/revisions\/2830"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2727"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}