{"id":2881,"date":"2017-08-20T03:13:35","date_gmt":"2017-08-20T03:13:35","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2881"},"modified":"2017-08-20T03:13:35","modified_gmt":"2017-08-20T03:13:35","slug":"27-youtube-data-api-comments-update-function","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2017\/08\/20\/27-youtube-data-api-comments-update-function\/","title":{"rendered":"27 &#8211; YouTube Data API &#8211; Comments &#8211; update 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 27 of the YouTube API With PHP series.<\/strong><\/p>\n<p>The update function modifies a Comment. This requires user authentication as you can only update comments that you have inserted. You cannot update other people\u2019s comments.<\/p>\n<p>The Request URL is<\/p>\n<pre><span style=\"color: #999999;\">PUT https:\/\/www.googleapis.com\/youtube\/v3\/comments<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Parameters<br \/>\n<\/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 Requests<\/strong><\/p>\n<p>Update an existing 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>&nbsp;<\/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 update 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    $commentId = &quot;z12jy3qp3q21e3se322ttjmaqlimxxohg04.1496032470067447&quot;;\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 via the API and has been updated.&quot;;\r\n   \t \r\n   \t $comment = new Comment();\r\n   \t $comment-&gt;snippet = $snippet;\r\n   \t $comment-&gt;id = $commentId;\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;PUT&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   \t public $id;\r\n    }\r\n\r\n    class Snippet {\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(852) &#8220;{ &#8220;kind&#8221;: &#8220;youtube#comment&#8221;, &#8220;etag&#8221;: &#8220;\\&#8221;m2yskBQFythfE4irbTIeOgYYfBU\/661I-lvcQipMcghf5r4kg2aNd00\\&#8221;&#8221;, &#8220;id&#8221;: &#8220;z12jy3qp3q21e3se322ttjmaqlimxxohg04.1496032470067447&#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;This is posted via the API and has been updated.&#8221;, &#8220;textOriginal&#8221;: &#8220;This is posted via the API and has been updated.&#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-29T04:34:30.000Z&#8221;, &#8220;updatedAt&#8221;: &#8220;2017-05-29T06:36:57.000Z&#8221; } } &#8220;<\/span><\/p>\n<p>The following things are required in the cURL parameters:<\/p>\n<ul>\n<li>Set CURLOPT_CUSTOMREQUEST to PUT as this is not a POST.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This is article 27 of the YouTube API With PHP series. The update function modifies a Comment. This requires user authentication as you can only <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2017\/08\/20\/27-youtube-data-api-comments-update-function\/\" title=\"27 &#8211; YouTube Data API &#8211; Comments &#8211; update 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-2881","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\/2881","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=2881"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2881\/revisions"}],"predecessor-version":[{"id":2883,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2881\/revisions\/2883"}],"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=2881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}