{"id":2853,"date":"2017-07-30T04:43:04","date_gmt":"2017-07-30T04:43:04","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2853"},"modified":"2017-07-30T04:43:04","modified_gmt":"2017-07-30T04:43:04","slug":"19-youtube-data-api-channel-sections-insert-function","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2017\/07\/30\/19-youtube-data-api-channel-sections-insert-function\/","title":{"rendered":"19 &#8211; YouTube Data API &#8211; Channel Sections &#8211; insert 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 19 of the YouTube API With PHP series.<\/strong><\/p>\n<p>The insert function adds a channel section to the authenticated user&#8217;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\u2019s Channel.<\/p>\n<p>The Request URL is<\/p>\n<pre><span style=\"color: #999999;\">POST https:\/\/www.googleapis.com\/youtube\/v3\/channelSections<\/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 or all of the following , comma separated:\n<ul>\n<li>\u201ccontentDetails\u201d<\/li>\n<li>\u201cid\u201d<\/li>\n<li>\u201clocalizations\u201d<\/li>\n<li>\u201csnippet\u201d<\/li>\n<li>\u201ctargeting\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<li><strong>onBehalfOfContentOwnerChannel<\/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>Add a ChannelSection to the current user\u2019s Channel.<\/p>\n<p><span style=\"color: #999999;\">https:\/\/www.googleapis.com\/youtube\/v3\/channelSections?key=xx&amp;access_token=xx&amp;part=id,contentDetails,localizations,snippet,targeting<\/span><\/p>\n<p><strong>Response<\/strong><\/p>\n<p>On successful execution, a json encoded ChannelSection resource is returned.<\/p>\n<p>Sample code to add a ChannelSection to current user\u2019s Channel:<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    $g_youtubeDataAPIKey = &quot;**&quot;;\r\n    $channelId = &quot;UCo6DJdltbIub80bLiyJRv3w&quot;;\r\n    \r\n\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   \t \r\n   \t \r\n   \t $accessToken = $_SESSION[&quot;access_token&quot;];\r\n\r\n   \t \/\/ make api request\r\n   \t $url = &quot;https:\/\/www.googleapis.com\/youtube\/v3\/channelSections?key=&quot; . $g_youtubeDataAPIKey .\r\n   \t\t\t  &quot;&amp;access_token=&quot; . $accessToken . &quot;&amp;part=id,contentDetails,localizations,snippet,targeting&quot;;\r\n\r\n   \t $playlists = array(&quot;PLLAs2gIR3bXPIL1pIO5r0kv2kxaGBoS5C&quot;);\r\n   \t \r\n   \t $contentDetails = new ContentDetails();\r\n   \t $contentDetails-&gt;playlists = $playlists;\r\n   \t \r\n   \t $snippet = new Snippet();\r\n   \t $snippet-&gt;type = &quot;singlePlaylist&quot;;\r\n   \t $snippet-&gt;style = &quot;horizontalRow&quot;;\r\n   \t $snippet-&gt;title = &quot;Classical Piano Section&quot;;\r\n\r\n   \t $channelSection = new ChannelSection();\r\n   \t $channelSection-&gt;snippet = $snippet;\r\n   \t $channelSection-&gt;contentDetails = $contentDetails;\r\n\r\n   \t $data = json_encode($channelSection);\r\n\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;POST&quot;,\r\n   \t\t\t\t CURLOPT_POSTFIELDS=&gt;$data\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\r\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\n    class ChannelSection {\r\n   \t public $snippet;\r\n   \t public $contentDetails;\r\n    }\r\n\r\n    class Snippet {\r\n   \t public $type;\r\n   \t public $style;\r\n   \t public $title;\r\n    }\r\n    \r\n    class ContentDetails {\r\n   \t public $playlists;\r\n    }\r\n?&gt;\r\n<\/pre><\/p>\n<p>Here is the output:<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #999999;\">string(376) &#8220;{ &#8220;kind&#8221;: &#8220;youtube#channelSection&#8221;, &#8220;etag&#8221;: &#8220;\\&#8221;m2yskBQFythfE4irbTIeOgYYfBU\/i8KM08OlK5Fxat1b3YqSM9BLeWM\\&#8221;&#8221;, &#8220;id&#8221;: &#8220;UCnXmfpAZ1rLsg0Goh0bBHUA.Zx4DA4xg9IM&#8221;, &#8220;snippet&#8221;: { &#8220;type&#8221;: &#8220;singlePlaylist&#8221;, &#8220;style&#8221;: &#8220;horizontalRow&#8221;, &#8220;channelId&#8221;: &#8220;UCnXmfpAZ1rLsg0Goh0bBHUA&#8221;, &#8220;position&#8221;: 4 }, &#8220;contentDetails&#8221;: { &#8220;playlists&#8221;: [ &#8220;PLLAs2gIR3bXPIL1pIO5r0kv2kxaGBoS5C&#8221; ] } } &#8220;<\/span><\/p>\n<p>The two compulsory parameters which must be passed are :<\/p>\n<ul>\n<li>type<\/li>\n<li>style<\/li>\n<\/ul>\n<p>The other parameters are optional and can be set as required.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This is article 19 of the YouTube API With PHP series. The insert function adds a channel section to the authenticated user&#8217;s channel. A channel <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2017\/07\/30\/19-youtube-data-api-channel-sections-insert-function\/\" title=\"19 &#8211; YouTube Data API &#8211; Channel Sections &#8211; insert 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-2853","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\/2853","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=2853"}],"version-history":[{"count":3,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2853\/revisions"}],"predecessor-version":[{"id":2856,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2853\/revisions\/2856"}],"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=2853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}