|
| 1 | +<?php |
| 2 | + |
| 3 | +// Call set_include_path() as needed to point to your client library. |
| 4 | +require_once 'Google/Client.php'; |
| 5 | +require_once 'Google/Service/YouTube.php'; |
| 6 | +session_start(); |
| 7 | + |
| 8 | +/* |
| 9 | + * You can acquire an OAuth 2.0 client ID and client secret from the |
| 10 | + * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}> |
| 11 | + * For more information about using OAuth 2.0 to access Google APIs, please see: |
| 12 | + * <https://developers.google.com/youtube/v3/guides/authentication> |
| 13 | + * Please ensure that you have enabled the YouTube Data API for your project. |
| 14 | + */ |
| 15 | +$OAUTH2_CLIENT_ID = 'REPLACE_ME'; |
| 16 | +$OAUTH2_CLIENT_SECRET = 'REPLACE_ME'; |
| 17 | + |
| 18 | +$client = new Google_Client(); |
| 19 | +$client->setClientId($OAUTH2_CLIENT_ID); |
| 20 | +$client->setClientSecret($OAUTH2_CLIENT_SECRET); |
| 21 | +$client->setScopes('https://www.googleapis.com/auth/youtube'); |
| 22 | +$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], |
| 23 | + FILTER_SANITIZE_URL); |
| 24 | +$client->setRedirectUri($redirect); |
| 25 | + |
| 26 | +// Define an object that will be used to make all API requests. |
| 27 | +$youtube = new Google_Service_YouTube($client); |
| 28 | + |
| 29 | +if (isset($_GET['code'])) { |
| 30 | + if (strval($_SESSION['state']) !== strval($_GET['state'])) { |
| 31 | + die('The session state did not match.'); |
| 32 | + } |
| 33 | + |
| 34 | + $client->authenticate($_GET['code']); |
| 35 | + $_SESSION['token'] = $client->getAccessToken(); |
| 36 | + header('Location: ' . $redirect); |
| 37 | +} |
| 38 | + |
| 39 | +if (isset($_SESSION['token'])) { |
| 40 | + $client->setAccessToken($_SESSION['token']); |
| 41 | +} |
| 42 | + |
| 43 | +// Check to ensure that the access token was successfully acquired. |
| 44 | +if ($client->getAccessToken()) { |
| 45 | + try{ |
| 46 | + // REPLACE this value with the path to the file you are uploading. |
| 47 | + $videoPath = "/path/to/file.mp4"; |
| 48 | + |
| 49 | + // Create a snippet with title, description, tags and category ID |
| 50 | + // Create an asset resource and set its snippet metadata and type. |
| 51 | + // This example sets the video's title, description, keyword tags, and |
| 52 | + // video category. |
| 53 | + $snippet = new Google_Service_YouTube_VideoSnippet(); |
| 54 | + $snippet->setTitle("Test title"); |
| 55 | + $snippet->setDescription("Test description"); |
| 56 | + $snippet->setTags(array("tag1", "tag2")); |
| 57 | + |
| 58 | + // Numeric video category. See |
| 59 | + // https://developers.google.com/youtube/v3/docs/videoCategories/list |
| 60 | + $snippet->setCategoryId("22"); |
| 61 | + |
| 62 | + // Set the video's status to "public". Valid statuses are "public", |
| 63 | + // "private" and "unlisted". |
| 64 | + $status = new Google_Service_YouTube_VideoStatus(); |
| 65 | + $status->privacyStatus = "public"; |
| 66 | + |
| 67 | + // Associate the snippet and status objects with a new video resource. |
| 68 | + $video = new Google_Service_YouTube_Video(); |
| 69 | + $video->setSnippet($snippet); |
| 70 | + $video->setStatus($status); |
| 71 | + |
| 72 | + // Specify the size of each chunk of data, in bytes. Set a higher value for |
| 73 | + // reliable connection as fewer chunks lead to faster uploads. Set a lower |
| 74 | + // value for better recovery on less reliable connections. |
| 75 | + $chunkSizeBytes = 1 * 1024 * 1024; |
| 76 | + |
| 77 | + // Setting the defer flag to true tells the client to return a request which can be called |
| 78 | + // with ->execute(); instead of making the API call immediately. |
| 79 | + $client->setDefer(true); |
| 80 | + |
| 81 | + // Create a request for the API's videos.insert method to create and upload the video. |
| 82 | + $insertRequest = $youtube->videos->insert("status,snippet", $video); |
| 83 | + |
| 84 | + // Create a MediaFileUpload object for resumable uploads. |
| 85 | + $media = new Google_Http_MediaFileUpload( |
| 86 | + $client, |
| 87 | + $insertRequest, |
| 88 | + 'video/*', |
| 89 | + null, |
| 90 | + true, |
| 91 | + $chunkSizeBytes |
| 92 | + ); |
| 93 | + $media->setFileSize(filesize($videoPath)); |
| 94 | + |
| 95 | + |
| 96 | + // Read the media file and upload it chunk by chunk. |
| 97 | + $status = false; |
| 98 | + $handle = fopen($videoPath, "rb"); |
| 99 | + while (!$status && !feof($handle)) { |
| 100 | + $chunk = fread($handle, $chunkSizeBytes); |
| 101 | + $status = $media->nextChunk($chunk); |
| 102 | + } |
| 103 | + |
| 104 | + fclose($handle); |
| 105 | + |
| 106 | + // If you want to make other calls after the file upload, set setDefer back to false |
| 107 | + $client->setDefer(false); |
| 108 | + |
| 109 | + |
| 110 | + $htmlBody .= "<h3>Video Uploaded</h3><ul>"; |
| 111 | + $htmlBody .= sprintf('<li>%s (%s)</li>', |
| 112 | + $status['snippet']['title'], |
| 113 | + $status['id']); |
| 114 | + |
| 115 | + $htmlBody .= '</ul>'; |
| 116 | + |
| 117 | + } catch (Google_ServiceException $e) { |
| 118 | + $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', |
| 119 | + htmlspecialchars($e->getMessage())); |
| 120 | + } catch (Google_Exception $e) { |
| 121 | + $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', |
| 122 | + htmlspecialchars($e->getMessage())); |
| 123 | + } |
| 124 | + |
| 125 | + $_SESSION['token'] = $client->getAccessToken(); |
| 126 | +} else { |
| 127 | + // If the user hasn't authorized the app, initiate the OAuth flow |
| 128 | + $state = mt_rand(); |
| 129 | + $client->setState($state); |
| 130 | + $_SESSION['state'] = $state; |
| 131 | + |
| 132 | + $authUrl = $client->createAuthUrl(); |
| 133 | + $htmlBody = <<<END |
| 134 | + <h3>Authorization Required</h3> |
| 135 | + <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> |
| 136 | +END; |
| 137 | +} |
| 138 | +?> |
| 139 | + |
| 140 | +<!doctype html> |
| 141 | +<html> |
| 142 | +<head> |
| 143 | +<title>Video Uploaded</title> |
| 144 | +</head> |
| 145 | +<body> |
| 146 | + <?=$htmlBody?> |
| 147 | +</body> |
| 148 | +</html> |
0 commit comments