|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This sample creates and manages comments by: |
| 5 | + * |
| 6 | + * 1. Getting the top-level comments for a video via "commentThreads.list" method. |
| 7 | + * 2. Replying to a comment thread via "comments.insert" method. |
| 8 | + * 3. Getting comment replies via "comments.list" method. |
| 9 | + * 4. Updating an existing comment via "comments.update" method. |
| 10 | + * 5. Sets moderation status of an existing comment via "comments.setModerationStatus" method. |
| 11 | + * 6. Marking a comment as spam via "comments.markAsSpam" method. |
| 12 | + * 7. Deleting an existing comment via "comments.delete" method. |
| 13 | + * |
| 14 | + * @author Ibrahim Ulukaya |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +// Call set_include_path() as needed to point to your client library. |
| 19 | +require_once 'Google/Client.php'; |
| 20 | +require_once 'Google/Service/YouTube.php'; |
| 21 | +session_start(); |
| 22 | + |
| 23 | + |
| 24 | +/* |
| 25 | + * You can acquire an OAuth 2.0 client ID and client secret from the |
| 26 | + * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}> |
| 27 | + * For more information about using OAuth 2.0 to access Google APIs, please see: |
| 28 | + * <https://developers.google.com/youtube/v3/guides/authentication> |
| 29 | + * Please ensure that you have enabled the YouTube Data API for your project. |
| 30 | + */ |
| 31 | +$OAUTH2_CLIENT_ID = 'REPLACE_ME'; |
| 32 | +$OAUTH2_CLIENT_SECRET = 'REPLACE_ME'; |
| 33 | + |
| 34 | +/* You can replace $VIDEO_ID with one of your videos' id, and text with the |
| 35 | + * comment you want to be added. |
| 36 | + */ |
| 37 | +$VIDEO_ID = 'REPLACE_ME'; |
| 38 | +$TEXT = 'REPLACE_ME'; |
| 39 | + |
| 40 | +$client = new Google_Client(); |
| 41 | +$client->setClientId($OAUTH2_CLIENT_ID); |
| 42 | +$client->setClientSecret($OAUTH2_CLIENT_SECRET); |
| 43 | + |
| 44 | +/* |
| 45 | + * This OAuth 2.0 access scope allows for full read/write access to the |
| 46 | + * authenticated user's account and requires requests to use an SSL connection. |
| 47 | + */ |
| 48 | +$client->setScopes('https://www.googleapis.com/auth/youtube.force-ssl'); |
| 49 | +$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], |
| 50 | + FILTER_SANITIZE_URL); |
| 51 | +$client->setRedirectUri($redirect); |
| 52 | + |
| 53 | +// Define an object that will be used to make all API requests. |
| 54 | +$youtube = new Google_Service_YouTube($client); |
| 55 | + |
| 56 | +if (isset($_GET['code'])) { |
| 57 | + if (strval($_SESSION['state']) !== strval($_GET['state'])) { |
| 58 | + die('The session state did not match.'); |
| 59 | + } |
| 60 | + |
| 61 | + $client->authenticate($_GET['code']); |
| 62 | + $_SESSION['token'] = $client->getAccessToken(); |
| 63 | + header('Location: ' . $redirect); |
| 64 | +} |
| 65 | + |
| 66 | +if (isset($_SESSION['token'])) { |
| 67 | + $client->setAccessToken($_SESSION['token']); |
| 68 | +} |
| 69 | + |
| 70 | +// Check to ensure that the access token was successfully acquired. |
| 71 | +if ($client->getAccessToken()) { |
| 72 | + try { |
| 73 | + # All the available methods are used in sequence just for the sake of an example. |
| 74 | + |
| 75 | + // Call the YouTube Data API's commentThreads.list method to retrieve video comment threads. |
| 76 | + $videoCommentThreads = $youtube->commentThreads->listCommentThreads('snippet', array( |
| 77 | + 'videoId' => $VIDEO_ID, |
| 78 | + 'textFormat' => 'plainText', |
| 79 | + )); |
| 80 | + |
| 81 | + $parentId = $videoCommentThreads[0]['id']; |
| 82 | + |
| 83 | + # Create a comment snippet with text. |
| 84 | + $commentSnippet = new Google_Service_YouTube_CommentSnippet(); |
| 85 | + $commentSnippet->setTextOriginal($TEXT); |
| 86 | + $commentSnippet->setParentId($parentId); |
| 87 | + |
| 88 | + # Create a comment with snippet. |
| 89 | + $comment = new Google_Service_YouTube_Comment(); |
| 90 | + $comment->setSnippet($commentSnippet); |
| 91 | + |
| 92 | + # Call the YouTube Data API's comments.insert method to reply to a comment. |
| 93 | + # (If the intention is to create a new top-level comment, commentThreads.insert |
| 94 | + # method should be used instead.) |
| 95 | + $commentInsertResponse = $youtube->comments->insert('snippet', $comment); |
| 96 | + |
| 97 | + |
| 98 | + // Call the YouTube Data API's comments.list method to retrieve existing comment replies. |
| 99 | + $videoComments = $youtube->comments->listComments('snippet', array( |
| 100 | + 'parentId' => $parentId, |
| 101 | + 'textFormat' => 'plainText', |
| 102 | + )); |
| 103 | + |
| 104 | + if (empty($videoComments)) { |
| 105 | + $htmlBody .= "<h3>Can\'t get video comments.</h3>"; |
| 106 | + } else { |
| 107 | + $videoComments[0]['snippet']['textOriginal'] = 'updated'; |
| 108 | + |
| 109 | + // Call the YouTube Data API's comments.update method to update an existing comment. |
| 110 | + $videoCommentUpdateResponse = $youtube->comments->update('snippet', $videoComments[0]); |
| 111 | + |
| 112 | + // Call the YouTube Data API's comments.setModerationStatus method to set moderation |
| 113 | + // status of an existing comment. |
| 114 | + $youtube->comments->setModerationStatus($videoComments[0]['id'], 'published'); |
| 115 | + |
| 116 | + // Call the YouTube Data API's comments.markAsSpam method to mark an existing comment as spam. |
| 117 | + $youtube->comments->markAsSpam($videoComments[0]['id']); |
| 118 | + |
| 119 | + // Call the YouTube Data API's comments.delete method to delete an existing comment. |
| 120 | + $youtube->comments->delete($videoComments[0]['id']); |
| 121 | + } |
| 122 | + |
| 123 | + $htmlBody .= "<h3>Video Comment Replies</h3><ul>"; |
| 124 | + foreach ($videoComments as $comment) { |
| 125 | + $htmlBody .= sprintf('<li>%s: "%s"</li>', $comment['snippet']['authorDisplayName'], |
| 126 | + $comment['snippet']['textOriginal']); |
| 127 | + } |
| 128 | + $htmlBody .= '</ul>'; |
| 129 | + |
| 130 | + $htmlBody .= "<h2>Replied to a comment for</h2><ul>"; |
| 131 | + $htmlBody .= sprintf('<li>%s: "%s"</li>', |
| 132 | + $commentInsertResponse['snippet']['authorDisplayName'], |
| 133 | + $commentInsertResponse['snippet']['textDisplay']); |
| 134 | + $htmlBody .= '</ul>'; |
| 135 | + |
| 136 | + $htmlBody .= "<h2>Updated comment for</h2><ul>"; |
| 137 | + $htmlBody .= sprintf('<li>%s: "%s"</li>', |
| 138 | + $videoCommentUpdateResponse['snippet']['authorDisplayName'], |
| 139 | + $videoCommentUpdateResponse['snippet']['textDisplay']); |
| 140 | + $htmlBody .= '</ul>'; |
| 141 | + |
| 142 | + } catch (Google_Service_Exception $e) { |
| 143 | + $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', |
| 144 | + htmlspecialchars($e->getMessage())); |
| 145 | + } catch (Google_Exception $e) { |
| 146 | + $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', |
| 147 | + htmlspecialchars($e->getMessage())); |
| 148 | + } |
| 149 | + |
| 150 | + $_SESSION['token'] = $client->getAccessToken(); |
| 151 | +} else { |
| 152 | + // If the user hasn't authorized the app, initiate the OAuth flow |
| 153 | + $state = mt_rand(); |
| 154 | + $client->setState($state); |
| 155 | + $_SESSION['state'] = $state; |
| 156 | + |
| 157 | + $authUrl = $client->createAuthUrl(); |
| 158 | + $htmlBody = <<<END |
| 159 | + <h3>Authorization Required</h3> |
| 160 | + <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> |
| 161 | +END; |
| 162 | +} |
| 163 | +?> |
| 164 | + |
| 165 | +<!doctype html> |
| 166 | +<html> |
| 167 | +<head> |
| 168 | +<title>Insert, list, update, moderate, mark and delete comments.</title> |
| 169 | +</head> |
| 170 | +<body> |
| 171 | + <?=$htmlBody?> |
| 172 | +</body> |
| 173 | +</html> |
0 commit comments