{"id":2865,"date":"2017-08-07T04:31:30","date_gmt":"2017-08-07T04:31:30","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2865"},"modified":"2017-08-07T04:31:30","modified_gmt":"2017-08-07T04:31:30","slug":"23-youtube-data-api-comment-threads-insert-function","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2017\/08\/07\/23-youtube-data-api-comment-threads-insert-function\/","title":{"rendered":"23 &#8211; YouTube Data API &#8211; Comment Threads- 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 23 of the YouTube API With PHP series.<\/strong><\/p>\n<p>The insert function creates a new top-level comment. To add a reply to an existing comment, use the comments.insert method instead. You can use this API call to insert comments for any Video or Channel.<\/p>\n<p>The Request URL is<\/p>\n<pre><span style=\"color: #999999;\">POST https:\/\/www.googleapis.com\/youtube\/v3\/commentThreads\r\n\r\n<\/span><\/pre>\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 in certain cases. This is the user Access token.<\/li>\n<li><strong>part<\/strong> (string). Required. One or all of the following in comma separated fashion:\n<ul>\n<li>\u201cid\u201d<\/li>\n<li>\u201csnippet\u201d<\/li>\n<li>\u201creplies\u201d<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Example Requests<\/strong><\/p>\n<p>Insert a CommentThread in a Channel<\/p>\n<p><span style=\"color: #999999;\">https:\/\/www.googleapis.com\/youtube\/v3\/commentThreads?key=xx&amp;access_token=&#8221; . $accessToken . &#8220;&amp;part=snippet<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Response<\/strong><\/p>\n<p>On successful execution a CommentThread resource is returned in JSON format.<\/p>\n<p>Sample code to insert a top level Comment Thread for a Video<\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #999999;\">\u00a0<\/span><\/pre>\n<p><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;UCnXmfpAZ1rLsg0Goh0bBHUA&quot;;\r\n    $videoId = &quot;WNnNJSXHe6s&quot;;\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\/commentThreads?key=&quot; . $g_youtubeDataAPIKey .\r\n   \t\t\t  &quot;&amp;access_token=&quot; . $accessToken . &quot;&amp;part=snippet&quot;;\r\n\r\n   \t \r\n   \t $topLevelCommentSnippet = new TopLevelCommentSnippet();\r\n   \t $topLevelCommentSnippet-&gt;textOriginal = &quot;This is a comment posted via the API&quot;;\r\n\r\n   \t $topLevelComment = new TopLevelComment();\r\n   \t $topLevelComment-&gt;snippet = $topLevelCommentSnippet;\r\n   \t \r\n   \t $snippet = new Snippet();\r\n   \t $snippet-&gt;videoId = $videoId;\r\n   \t $snippet-&gt;topLevelComment = $topLevelComment;\r\n   \t $snippet-&gt;isPublic = true;\r\n\r\n   \t $commentThread = new CommentThread();\r\n   \t $commentThread-&gt;snippet = $snippet;\r\n   \t \r\n   \t \r\n   \t $data = json_encode($commentThread);\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 CommentThread {\r\n   \t public $snippet;\r\n    }\r\n\r\n    class Snippet {\r\n   \t public $topLevelComment;\r\n   \t public $isPublic;\r\n   \t public $videoId;\r\n    }\r\n\r\n    class TopLevelComment {\r\n   \t public $textOriginal;\r\n    }\r\n\r\n    class TopLevelCommentSnippet {\r\n   \t public $textOriginal;\r\n    }\r\n\r\n?&gt;\r\n<\/pre><\/p>\n<p>Here is the output:<\/p>\n<p><span style=\"color: #999999;\">string(1204) &#8220;{ &#8220;kind&#8221;: &#8220;youtube#commentThread&#8221;, &#8220;etag&#8221;: &#8220;\\&#8221;m2yskBQFythfE4irbTIeOgYYfBU\/kSR19kRoKQoN0H3RB3fdn1fPZGo\\&#8221;&#8221;, &#8220;id&#8221;: &#8220;z123uppgbl2bu3m5304cfb5ycuu5ufrqlpk0k&#8221;, &#8220;snippet&#8221;: { &#8220;channelId&#8221;: &#8220;UCnXmfpAZ1rLsg0Goh0bBHUA&#8221;, &#8220;videoId&#8221;: &#8220;WNnNJSXHe6s&#8221;, &#8220;topLevelComment&#8221;: { &#8220;kind&#8221;: &#8220;youtube#comment&#8221;, &#8220;etag&#8221;: &#8220;\\&#8221;m2yskBQFythfE4irbTIeOgYYfBU\/LXq8sJPgsfpmZxpyuBRww4qiy8Y\\&#8221;&#8221;, &#8220;id&#8221;: &#8220;z123uppgbl2bu3m5304cfb5ycuu5ufrqlpk0k&#8221;, &#8220;snippet&#8221;: { &#8220;authorDisplayName&#8221;: &#8220;Amit Sengupta&#8221;, &#8220;authorProfileImageUrl&#8221;: &#8220;https:\/\/yt3.ggpht.com\/-HGpjiYmK2OY\/AAAAAAAAAAI\/AAAAAAAAAAA\/1nRoeezR4HM\/s28-c-k-no-mo-rj-c0xffffff\/photo.jpg&#8221;, &#8220;authorChannelUrl&#8221;: &#8220;http:\/\/www.youtube.com\/channel\/UCnXmfpAZ1rLsg0Goh0bBHUA&#8221;, &#8220;authorChannelId&#8221;: { &#8220;value&#8221;: &#8220;UCnXmfpAZ1rLsg0Goh0bBHUA&#8221; }, &#8220;channelId&#8221;: &#8220;UCnXmfpAZ1rLsg0Goh0bBHUA&#8221;, &#8220;videoId&#8221;: &#8220;WNnNJSXHe6s&#8221;, &#8220;textDisplay&#8221;: &#8220;This is a comment posted via the API&#8221;, &#8220;textOriginal&#8221;: &#8220;This is a comment posted via the API&#8221;, &#8220;canRate&#8221;: true, &#8220;viewerRating&#8221;: &#8220;none&#8221;, &#8220;likeCount&#8221;: 0, &#8220;publishedAt&#8221;: &#8220;2017-05-28T08:21:27.000Z&#8221;, &#8220;updatedAt&#8221;: &#8220;2017-05-28T08:21:27.000Z&#8221; } }, &#8220;canReply&#8221;: true, &#8220;totalReplyCount&#8221;: 0, &#8220;isPublic&#8221;: true } } &#8220;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Here is how it looks on Youtube:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2866\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2017\/08\/1-620x718.png\" alt=\"1\" width=\"620\" height=\"718\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2017\/08\/1-620x718.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2017\/08\/1-300x347.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2017\/08\/1.png 686w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>We set up the CommentThread structure using classes. Depending on whether we are inserting a CommentThread for a channel or a video, we either use videoId or channelId in the Snippet structure.<\/p>\n<p>You have to be careful about the two snippet structures &#8211; one is within the top level CommentThread class. The other is within the TopLevelComment class. The TopLevelComment class represents a Comment resource , which is similar to a CommentThread resource.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This is article 23 of the YouTube API With PHP series. The insert function creates a new top-level comment. To add a reply to an <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2017\/08\/07\/23-youtube-data-api-comment-threads-insert-function\/\" title=\"23 &#8211; YouTube Data API &#8211; Comment Threads- 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-2865","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\/2865","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=2865"}],"version-history":[{"count":1,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2865\/revisions"}],"predecessor-version":[{"id":2867,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2865\/revisions\/2867"}],"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=2865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}