20 – YouTube Data API – Channel Sections – update function

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

The update function updates an existing ChannelSection. This call requires user-authentication so only ChannelSections belonging to the current user ,can be updated.

The Request URL is

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

 

Parameters:

  • key (string) required. Your API key
  • access_token (string) required. This is the user Access token.
  • part (string) required. One or all of the following , comma separated:
    • “contentDetails”
    • “id”
    • “localizations”
    • “snippet”
    • “targeting”
  • 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

Add a ChannelSection to the current user’s Channel

https://www.googleapis.com/youtube/v3/channelSections?key=xx&access_token=xx&part=id,contentDetails,localizations,snippet,targeting

 

Response

On successful execution, a json encoded ChannelSection resource is returned.

Sample code to update a ChannelSection of the current user’s Channel:

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


    $_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/channelSections?key=" . $g_youtubeDataAPIKey .
   			  "&access_token=" . $accessToken . "&part=id,contentDetails,localizations,snippet,targeting";

   	 $playlists = array("PLLAs2gIR3bXPIL1pIO5r0kv2kxaGBoS5C");
   	 
   	 $contentDetails = new ContentDetails();
   	 $contentDetails->playlists = $playlists;
   	 
   	 $snippet = new Snippet();
   	 $snippet->type = "recentPosts";
   	 $snippet->style = "horizontalRow";
   	 $snippet->title = "Test Channel Section Updated";

   	 $channelSection = new ChannelSection();
   	 $channelSection->snippet = $snippet;
   	 $channelSection->contentDetails = $contentDetails;
   	 $channelSection->id = "UCnXmfpAZ1rLsg0Goh0bBHUA.jNQXAC9IVRw";

   	 $data = json_encode($channelSection);

   	 
   	 $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 ChannelSection {
   	 public $snippet;
   	 public $contentDetails;
   	 public $id;
    }

    class Snippet {
   	 public $type;
   	 public $style;
   	 public $title;
    }
    
    class ContentDetails {
   	 public $playlists;
    }
?>

Here is the output:

string(287) “{ “kind”: “youtube#channelSection”, “etag”: “\”m2yskBQFythfE4irbTIeOgYYfBU/-crlLBG8Ica3XHfcIjT0QCAQi4M\””, “id”: “UCnXmfpAZ1rLsg0Goh0bBHUA.jNQXAC9IVRw”, “snippet”: { “type”: “recentPosts”, “style”: “horizontalRow”, “channelId”: “UCnXmfpAZ1rLsg0Goh0bBHUA”, “position”: 0 } } “

The following parameters are compulsory:

  • style
  • type
  • Id

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.