19 – YouTube Data API – Channel Sections – insert function

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

The insert function adds a channel section to the authenticated user’s channel. A channel can create a maximum of 10 shelves without setting targeting data and can create a maximum of 100 shelves with targeting data. This call requires user-authentication so ChannelSections can only be added to the current user’s Channel.

The Request URL is

POST 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.
  • onBehalfOfContentOwnerChannel (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 add a ChannelSection to 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 = "singlePlaylist";
   	 $snippet->style = "horizontalRow";
   	 $snippet->title = "Classical Piano Section";

   	 $channelSection = new ChannelSection();
   	 $channelSection->snippet = $snippet;
   	 $channelSection->contentDetails = $contentDetails;

   	 $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=>"POST",
   				 CURLOPT_POSTFIELDS=>$data
   				 ));
   	 $resp = curl_exec($curl);

   	 curl_close($curl);

   	 var_dump($resp);
   	 


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

    class ChannelSection {
   	 public $snippet;
   	 public $contentDetails;
    }

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

Here is the output:

 

string(376) “{ “kind”: “youtube#channelSection”, “etag”: “\”m2yskBQFythfE4irbTIeOgYYfBU/i8KM08OlK5Fxat1b3YqSM9BLeWM\””, “id”: “UCnXmfpAZ1rLsg0Goh0bBHUA.Zx4DA4xg9IM”, “snippet”: { “type”: “singlePlaylist”, “style”: “horizontalRow”, “channelId”: “UCnXmfpAZ1rLsg0Goh0bBHUA”, “position”: 4 }, “contentDetails”: { “playlists”: [ “PLLAs2gIR3bXPIL1pIO5r0kv2kxaGBoS5C” ] } } “

The two compulsory parameters which must be passed are :

  • type
  • style

The other parameters are optional and can be set as required.

Be the first to comment

Leave a Reply

Your email address will not be published.


*