예제 #1
0
 function google($video_temp = '', $video_name = '', $video_desc = '')
 {
     $OAUTH2_CLIENT_ID = '353001433162-42vrona3fi8msfve7akh857t6fk0di9v.apps.googleusercontent.com';
     $OAUTH2_CLIENT_SECRET = 'cEcHT7CkTK5GYUDmC7dgYa8r';
     $redirect = 'http://localhost/multitvfinal/index.php/video/google';
     $client = new Google_Client();
     $client->setClientId($OAUTH2_CLIENT_ID);
     $client->setClientSecret($OAUTH2_CLIENT_SECRET);
     $client->setScopes('https://www.googleapis.com/auth/youtube');
     $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL);
     $client->setRedirectUri($redirect);
     $youtube = new Google_Service_YouTube($client);
     if (isset($_GET['code'])) {
         if (strval($this->session->userdata('state')) !== strval($_GET['state'])) {
             die('The session state did not match.');
         }
         $client->authenticate($_GET['code']);
         $this->session->set_userdata('token', $client->getAccessToken());
         header('Location: ' . $redirect);
     }
     $session_token = $this->session->userdata('token');
     if ($session_token) {
         $client->setAccessToken($this->session->userdata('token'));
     }
     if ($client->getAccessToken()) {
         if (isset($video_temp) && $video_temp != '') {
             // REPLACE this value with the path to the file you are uploading.
             $videoPath = $video_temp;
             // Create a snippet with title, description, tags and category ID
             // Create an asset resource and set its snippet metadata and type.
             // This example sets the video's title, description, keyword tags, and
             // video category.
             $snippet = new Google_Service_YouTube_VideoSnippet();
             $snippet->setTitle($video_name);
             $snippet->setDescription($video_desc);
             $snippet->setTags(array("globalPunjab", "Video"));
             // Numeric video category. See
             // https://developers.google.com/youtube/v3/docs/videoCategories/list
             $snippet->setCategoryId("22");
             $snippet->setChannelTitle("GlobalPunjab");
             // Set the video's status to "public". Valid statuses are "public",
             // "private" and "unlisted".
             $status = new Google_Service_YouTube_VideoStatus();
             $status->privacyStatus = "public";
             // Associate the snippet and status objects with a new video resource.
             $video = new Google_Service_YouTube_Video();
             $video->setSnippet($snippet);
             $video->setStatus($status);
             // Specify the size of each chunk of data, in bytes. Set a higher value for
             // reliable connection as fewer chunks lead to faster uploads. Set a lower
             // value for better recovery on less reliable connections.
             $chunkSizeBytes = 1 * 1024 * 1024;
             // Setting the defer flag to true tells the client to return a request which can be called
             // with ->execute(); instead of making the API call immediately.
             $client->setDefer(true);
             // Create a request for the API's videos.insert method to create and upload the video.
             $insertRequest = $youtube->videos->insert("status,snippet", $video);
             // Create a MediaFileUpload object for resumable uploads.
             $media = new Google_Http_MediaFileUpload($client, $insertRequest, 'video/*', null, true, $chunkSizeBytes);
             $media->setFileSize(filesize($videoPath));
             // Read the media file and upload it chunk by chunk.
             $status = false;
             $handle = fopen($videoPath, "rb");
             while (!$status && !feof($handle)) {
                 $chunk = fread($handle, $chunkSizeBytes);
                 $status = $media->nextChunk($chunk);
             }
             fclose($handle);
             // If you want to make other calls after the file upload, set setDefer back to false
             $client->setDefer(false);
             $htmlBody = $status['id'];
             //echo "<pre>"; print_r($status);
         }
         $htmlBody = false;
     } else {
         // If the user hasn't authorized the app, initiate the OAuth flow
         $state = mt_rand();
         $client->setState($state);
         $this->session->set_userdata('state', $state);
         //echo $this->session->userdata('state');
         $authUrl = $client->createAuthUrl();
         $htmlBody = $authUrl;
     }
     return $htmlBody;
     //spl_autoload_register('google_api_php_client_autoload'); die;
 }