{"id":2848,"date":"2017-07-13T03:18:45","date_gmt":"2017-07-13T03:18:45","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2848"},"modified":"2017-07-13T03:18:45","modified_gmt":"2017-07-13T03:18:45","slug":"17-youtube-data-api-channels-update-function","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2017\/07\/13\/17-youtube-data-api-channels-update-function\/","title":{"rendered":"17 &#8211; YouTube Data API &#8211; Channels &#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 17 of the YouTube API With PHP series.<\/strong><\/p>\n<p>The update function allows you to make changes in the Channel resource data. Since this call requires user authentication , you can only make updates to channels that you own.<\/p>\n<p>The Request URL is<\/p>\n<pre><span style=\"color: #999999;\">PUT https:\/\/www.googleapis.com\/youtube\/v3\/channels<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>key<\/strong> (string) required. Your API key<\/li>\n<li><strong>access_token<\/strong> (string) required. This is the user Access token.<\/li>\n<li><strong>part<\/strong> (string) required. One of the following :\n<ul>\n<li>\u201cbrandingSettings\u201d<\/li>\n<li>\u201cinvideoPromotion\u201d<\/li>\n<li>\u201cLocalizations\u201d<\/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><strong>Example Requests:<\/strong><\/p>\n<p>Update brandingSettings attributes.<\/p>\n<p><span style=\"color: #999999;\">https:\/\/www.googleapis.com\/youtube\/v3\/channels?key=xxxx \u00a0\u00a0\u00a0\u00a0 &amp;access_token=xx&amp;part=brandingSettings<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Response<\/strong><\/p>\n<p>On successful execution a json encoded Channel Resource is returned.<\/p>\n<p>Sample code to change the description a channel.<\/p>\n<p><pre class=\"brush: php\">&lt;?php\r\n    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);\r\n    session_start();\r\n    set_time_limit(60 * 3);\r\n    \r\n    $g_youtubeDataAPIKey = &quot;**&quot;;\r\n    $channelId = &quot;***&quot;;\r\n    \r\n\r\n    $_SESSION[&quot;code_id&quot;] = $_SERVER[&quot;PHP_SELF&quot;];\r\n\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   \t $accessToken = $_SESSION[&quot;access_token&quot;];\r\n\r\n   \t $channel = new Channel();\r\n   \t $channel-&gt;description = &quot;This is test channel description&quot;;\r\n\r\n   \t $brandingSettings = new BrandingSettings();\r\n   \t $brandingSettings-&gt;channel = $channel;\r\n\r\n   \t $channelResource = new ChannelResource();\r\n   \t $channelResource-&gt;brandingSettings = $brandingSettings;\r\n   \t $channelResource-&gt;id = $channelId;\r\n\r\n   \t \r\n   \t $data = json_encode($channelResource);\r\n   \t \r\n   \t \r\n   \t \/\/ make api request\r\n   \t $url = &quot;https:\/\/www.googleapis.com\/youtube\/v3\/channels?key=&quot; . $g_youtubeDataAPIKey .\r\n   \t\t\t   &quot;&amp;access_token=&quot; . $accessToken. &quot;&amp;part=brandingSettings&quot;;\r\n   \t \r\n   \t \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&#039;),\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;PUT&quot;,\r\n   \t\t\t\t CURLOPT_POSTFIELDS=&gt;$data\r\n\r\n   \t\t\t\t ));\r\n   \t $resp = curl_exec($curl);\r\n\r\n   \t curl_close($curl);\r\n    \r\n   \t var_dump($resp);\r\n   \t \r\n\r\n   \t \r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\nclass ChannelResource {\r\n    public $id;\r\n    public $brandingSettings;\r\n}\r\n\r\nclass BrandingSettings {\r\n    public $channel;\r\n}\r\n\r\nclass Channel {\r\n    public $description;\r\n\r\n}\r\n    \r\n?&gt;\r\n<\/pre><\/p>\n<p>&nbsp;<\/p>\n<p>The output:<\/p>\n<p><span style=\"color: #999999;\">string(3552) &#8220;{ &#8220;kind&#8221;: &#8220;youtube#channel&#8221;, &#8220;etag&#8221;: &#8220;\\&#8221;m2yskBQFythfE4irbTIeOgYYfBU\/n8-f3X6BAJhpD42iZKWo8_MOUdY\\&#8221;&#8221;, &#8220;id&#8221;: &#8220;UCnXmfpAZ1rLsg0Goh0bBHUA&#8221;, &#8220;brandingSettings&#8221;: { &#8220;channel&#8221;: { &#8220;title&#8221;: &#8220;Amit Sengupta&#8221;, &#8220;description&#8221;: &#8220;This is test channel description&#8221;, &#8220;defaultTab&#8221;: &#8220;Featured&#8221;, &#8220;profileColor&#8221;: &#8220;#000000&#8221;, &#8220;defaultLanguage&#8221;: &#8220;en&#8221; }, &#8220;image&#8221;: { &#8220;bannerImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w1060-fcrop64=1,00005a57ffffa5a8-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerMobileImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w640-fcrop64=1,32b75a57cd48a5a8-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerTabletLowImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w1138-fcrop64=1,00005a57ffffa5a8-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerTabletImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w1707-fcrop64=1,00005a57ffffa5a8-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerTabletHdImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w2276-fcrop64=1,00005a57ffffa5a8-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerTabletExtraHdImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w2560-fcrop64=1,00005a57ffffa5a8-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerMobileLowImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w320-fcrop64=1,32b75a57cd48a5a8-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerMobileMediumHdImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w960-fcrop64=1,32b75a57cd48a5a8-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerMobileHdImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w1280-fcrop64=1,32b75a57cd48a5a8-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerMobileExtraHdImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w1440-fcrop64=1,32b75a57cd48a5a8-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerTvImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w2120-fcrop64=1,00000000ffffffff-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerTvLowImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w854-fcrop64=1,00000000ffffffff-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerTvMediumImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w1280-fcrop64=1,00000000ffffffff-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221;, &#8220;bannerTvHighImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w1920-fcrop64=1,00000000ffffffff-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221; }, &#8220;hints&#8221;: [ { &#8220;property&#8221;: &#8220;channel.featured_tab.template.string&#8221;, &#8220;value&#8221;: &#8220;Everything&#8221; }, { &#8220;property&#8221;: &#8220;channel.banner.mobile.medium.image.url&#8221;, &#8220;value&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-2yueuJ2GfNc\/WSfJ67_xrYI\/AAAAAAAAAqg\/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B\/w640-fcrop64=1,32b75a57cd48a5a8-nd-c0xffffffff-rj-k-no\/channels4_banner&#8221; } ] } }<\/span><\/p>\n<p>We set up the class structure for\u00a0 a Channel Resource object. Since here we are only updating the channel description we have only defined description in the Channel object. You have to setup the required nested classes based on the attributes you are going to update.<\/p>\n<p>So eg. if you want to change invideoPromotion.position.type then the class structure would be<\/p>\n<pre><span style=\"color: #999999;\">class ChannelResource {<\/span>\r\n<span style=\"color: #999999;\">\u00a0\u00a0\u00a0\u00a0 public $id;<\/span>\r\n<span style=\"color: #999999;\">\u00a0\u00a0\u00a0 public $invideoPromotion;<\/span>\r\n<span style=\"color: #999999;\">}<\/span>\r\n\r\n<span style=\"color: #999999;\">class InvideoPromotion {<\/span>\r\n<span style=\"color: #999999;\">\u00a0\u00a0\u00a0\u00a0 public $position;<\/span>\r\n<span style=\"color: #999999;\">}<\/span>\r\n<span style=\"color: #999999;\">class Position {<\/span>\r\n<span style=\"color: #999999;\">\u00a0\u00a0\u00a0\u00a0 public $type;<\/span>\r\n<span style=\"color: #999999;\">}\r\n\r\n<\/span><\/pre>\n<p>The following things are required in the cURL request:<\/p>\n<ul>\n<li>Set CURLOPT_HTTPHEADER to &#8216;Content-Type: application\/json&#8217;) since\u00a0 we are passing a Channel resource in the body<\/li>\n<li>Set CURLOPT_CUSTOMREQUEST to &#8220;PUT&#8221; since this is not a POST<\/li>\n<li>Set CURLOPT_POSTFIELDS to the json encoded object<\/li>\n<\/ul>\n<p>If the call is successful a Channel Resource object is returned.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This is article 17 of the YouTube API With PHP series. The update function allows you to make changes in the Channel resource data. Since <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2017\/07\/13\/17-youtube-data-api-channels-update-function\/\" title=\"17 &#8211; YouTube Data API &#8211; Channels &#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-2848","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\/2848","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=2848"}],"version-history":[{"count":1,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2848\/revisions"}],"predecessor-version":[{"id":2849,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2848\/revisions\/2849"}],"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=2848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}