17 – YouTube Data API – Channels – update function

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 this call requires user authentication , you can only make updates to channels that you own.

The Request URL is

PUT https://www.googleapis.com/youtube/v3/channels

 

Parameters:

  • key (string) required. Your API key
  • access_token (string) required. This is the user Access token.
  • part (string) required. One of the following :
    • “brandingSettings”
    • “invideoPromotion”
    • “Localizations”
  • 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.

 

Example Requests:

Update brandingSettings attributes.

https://www.googleapis.com/youtube/v3/channels?key=xxxx      &access_token=xx&part=brandingSettings

 

Response

On successful execution a json encoded Channel Resource is returned.

Sample code to change the description a channel.

<?php
    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
    session_start();
    set_time_limit(60 * 3);
    
    $g_youtubeDataAPIKey = "**";
    $channelId = "***";
    

    $_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"];

   	 $channel = new Channel();
   	 $channel->description = "This is test channel description";

   	 $brandingSettings = new BrandingSettings();
   	 $brandingSettings->channel = $channel;

   	 $channelResource = new ChannelResource();
   	 $channelResource->brandingSettings = $brandingSettings;
   	 $channelResource->id = $channelId;

   	 
   	 $data = json_encode($channelResource);
   	 
   	 
   	 // make api request
   	 $url = "https://www.googleapis.com/youtube/v3/channels?key=" . $g_youtubeDataAPIKey .
   			   "&access_token=" . $accessToken. "&part=brandingSettings";
   	 
   	 
   	 $curl = curl_init();
   	 curl_setopt_array($curl, array(
   				 CURLOPT_HTTPHEADER=>array('Content-Type: application/json'),
   				 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=>"PUT",
   				 CURLOPT_POSTFIELDS=>$data

   				 ));
   	 $resp = curl_exec($curl);

   	 curl_close($curl);
    
   	 var_dump($resp);
   	 

   	 
/////////////////

class ChannelResource {
    public $id;
    public $brandingSettings;
}

class BrandingSettings {
    public $channel;
}

class Channel {
    public $description;

}
    
?>

 

The output:

string(3552) “{ “kind”: “youtube#channel”, “etag”: “\”m2yskBQFythfE4irbTIeOgYYfBU/n8-f3X6BAJhpD42iZKWo8_MOUdY\””, “id”: “UCnXmfpAZ1rLsg0Goh0bBHUA”, “brandingSettings”: { “channel”: { “title”: “Amit Sengupta”, “description”: “This is test channel description”, “defaultTab”: “Featured”, “profileColor”: “#000000”, “defaultLanguage”: “en” }, “image”: { “bannerImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w1060-fcrop64=1,00005a57ffffa5a8-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerMobileImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w640-fcrop64=1,32b75a57cd48a5a8-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerTabletLowImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w1138-fcrop64=1,00005a57ffffa5a8-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerTabletImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w1707-fcrop64=1,00005a57ffffa5a8-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerTabletHdImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w2276-fcrop64=1,00005a57ffffa5a8-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerTabletExtraHdImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w2560-fcrop64=1,00005a57ffffa5a8-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerMobileLowImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w320-fcrop64=1,32b75a57cd48a5a8-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerMobileMediumHdImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w960-fcrop64=1,32b75a57cd48a5a8-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerMobileHdImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w1280-fcrop64=1,32b75a57cd48a5a8-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerMobileExtraHdImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w1440-fcrop64=1,32b75a57cd48a5a8-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerTvImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w2120-fcrop64=1,00000000ffffffff-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerTvLowImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w854-fcrop64=1,00000000ffffffff-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerTvMediumImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w1280-fcrop64=1,00000000ffffffff-nd-c0xffffffff-rj-k-no/channels4_banner”, “bannerTvHighImageUrl”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w1920-fcrop64=1,00000000ffffffff-nd-c0xffffffff-rj-k-no/channels4_banner” }, “hints”: [ { “property”: “channel.featured_tab.template.string”, “value”: “Everything” }, { “property”: “channel.banner.mobile.medium.image.url”, “value”: “https://yt3.ggpht.com/-2yueuJ2GfNc/WSfJ67_xrYI/AAAAAAAAAqg/MIVb0xS9fUQA2Cn-ReWLpapK47WOVaPCQCL8B/w640-fcrop64=1,32b75a57cd48a5a8-nd-c0xffffffff-rj-k-no/channels4_banner” } ] } }

We set up the class structure for  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.

So eg. if you want to change invideoPromotion.position.type then the class structure would be

class ChannelResource {
     public $id;
    public $invideoPromotion;
}

class InvideoPromotion {
     public $position;
}
class Position {
     public $type;
}

The following things are required in the cURL request:

  • Set CURLOPT_HTTPHEADER to ‘Content-Type: application/json’) since  we are passing a Channel resource in the body
  • Set CURLOPT_CUSTOMREQUEST to “PUT” since this is not a POST
  • Set CURLOPT_POSTFIELDS to the json encoded object

If the call is successful a Channel Resource object is returned.

Be the first to comment

Leave a Reply

Your email address will not be published.


*