{"id":2877,"date":"2017-08-16T03:48:11","date_gmt":"2017-08-16T03:48:11","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2877"},"modified":"2017-08-16T03:48:11","modified_gmt":"2017-08-16T03:48:11","slug":"26-youtube-data-api-comments-insert-function","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2017\/08\/16\/26-youtube-data-api-comments-insert-function\/","title":{"rendered":"26 &#8211; YouTube Data API &#8211; Comments &#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 26 of the YouTube API With PHP series.<\/strong><\/p>\n<p>The insert function creates a reply to an existing comment. To create a top-level comment, use the commentThreads.insert method. You can use this API to post a reply to any comment on any publicly visible Video or Channel. This call requires user authentication as an anonymous user cannot post replies.<\/p>\n<p>The Request URL is<\/p>\n<pre><span style=\"color: #999999;\">POST https:\/\/www.googleapis.com\/youtube\/v3\/comments<\/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<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Example Request<\/strong><\/p>\n<p>Insert a reply to a Comment<\/p>\n<p><span style=\"color: #999999;\">https:\/\/www.googleapis.com\/youtube\/v3\/comments?key=xx&amp;access_token=xx&amp;part=snippet,id<\/span><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>Response<\/strong><\/p>\n<p>On successful execution a Comment resource is returned in JSON format.<\/p>\n<p>Sample code to insert a reply to a Comment<\/p>\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\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\/comments?key=&quot; . $g_youtubeDataAPIKey .\r\n   \t\t\t  &quot;&amp;access_token=&quot; . $accessToken . &quot;&amp;part=snippet,id&quot;;\r\n\r\n   \t \r\n\r\n   \t $snippet = new Snippet();\r\n   \t $snippet-&gt;textOriginal = &quot;This is posted as a spam comment&quot;;\r\n   \t $snippet-&gt;parentId = &quot;z12jy3qp3q21e3se322ttjmaqlimxxohg04&quot;;\r\n   \t \r\n   \t $comment = new Comment();\r\n   \t $comment-&gt;snippet = $snippet;\r\n   \t \r\n   \t $data = json_encode($comment);\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 Comment {\r\n   \t public $snippet;\r\n\r\n    }\r\n\r\n    class Snippet {\r\n   \t public $parentId;\r\n   \t public $textOriginal;\r\n    }\r\n\r\n?&gt;\r\n<\/pre><br \/>\nHere is the output:<\/p>\n<p><span style=\"color: #999999;\">string(848) &#8220;{ &#8220;kind&#8221;: &#8220;youtube#comment&#8221;, &#8220;etag&#8221;: &#8220;\\&#8221;m2yskBQFythfE4irbTIeOgYYfBU\/wGMttOviqnWjcXd-4JsFSxqn62Y\\&#8221;&#8221;, &#8220;id&#8221;: &#8220;z12jy3qp3q21e3se322ttjmaqlimxxohg04.1496039501396482&#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;textDisplay&#8221;: &#8220;Again posting a reply to a comment via the API&#8221;, &#8220;textOriginal&#8221;: &#8220;Again posting a reply to a comment via the API&#8221;, &#8220;parentId&#8221;: &#8220;z12jy3qp3q21e3se322ttjmaqlimxxohg04&#8221;, &#8220;canRate&#8221;: true, &#8220;viewerRating&#8221;: &#8220;none&#8221;, &#8220;likeCount&#8221;: 0, &#8220;publishedAt&#8221;: &#8220;2017-05-29T06:31:41.000Z&#8221;, &#8220;updatedAt&#8221;: &#8220;2017-05-29T06:31:41.000Z&#8221; } } &#8220;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This is article 26 of the YouTube API With PHP series. The insert function creates a reply to an existing comment. To create a top-level <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2017\/08\/16\/26-youtube-data-api-comments-insert-function\/\" title=\"26 &#8211; YouTube Data API &#8211; Comments &#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-2877","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\/2877","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=2877"}],"version-history":[{"count":1,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2877\/revisions"}],"predecessor-version":[{"id":2879,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2877\/revisions\/2879"}],"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=2877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}