2016-10-04 103 views

回答

1

是Omprakash,可以

  1. 您需要谷歌API客戶端庫PHP。
  2. 您還需要在 https://console.developers.google.com/上創建項目並獲取憑證(即 客戶端密鑰&客戶端ID)。
  3. 最後,您需要爲特定頻道生成訪問令牌。 請看看這個鏈接 (https://youtube-eng.googleblog.com/2013/06/google-page-identities-and-youtube-api_24.html) 生成訪問令牌。
  4. 一旦您準備好了所有這些東西,您就可以使用現成的 示例代碼在適用於PHP的Google API客戶端庫中上傳 視頻。

注意:這並不是詳細的過程。無法詳細解釋堆棧溢出的所有過程。但是,一旦您接近解決方案,您可以重新發布或發表評論以獲得進一步的幫助。

這是在YouTube上上傳視頻的示例代碼。希望,它會幫助你

/*include google libraries */ 
require_once '../api/src/Google/autoload.php'; 
require_once '../api/src/Google/Client.php'; 
require_once '../api/src/Google/Service/YouTube.php'; 

$application_name = 'Your application/project name created on google developer console'; 
$client_secret = 'Your client secret'; 
$client_id = 'Your client id'; 
$scope = array('https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube', 'https://www.googleapis.com/auth/youtubepartner'); 


    try{ 
     $key = file_get_contents('the_key.txt'); //it stores access token obtained in step 3 
     $videoPath = 'video path on your server goes here'; 
     $videoTitle = 'video title'; 
     $videoDescription = 'video description'; 
     $videoCategory = "22"; //please take a look at youtube video categories for videoCategory.Not so important for our example 
     $videoTags = array('tag1', 'tag2','tag3'); 

     // Client init 
     $client = new Google_Client(); 
     $client->setApplicationName($application_name); 
     $client->setClientId($client_id); 
     $client->setAccessType('offline'); 
     $client->setAccessToken($key); 
     $client->setScopes($scope); 
     $client->setClientSecret($client_secret); 

     if ($client->getAccessToken()) { 
      /** 
      * Check to see if our access token has expired. If so, get a new one and save it to file for future use. 
      */ 
      if($client->isAccessTokenExpired()) { 
       $newToken = json_decode($client->getAccessToken()); 
       $client->refreshToken($newToken->refresh_token); 
       file_put_contents('the_key.txt', $client->getAccessToken()); 
      } 

      $youtube = new Google_Service_YouTube($client); 



      // Create a snipet with title, description, tags and category id 
      $snippet = new Google_Service_YouTube_VideoSnippet(); 
      $snippet->setTitle($videoTitle); 
      $snippet->setDescription($videoDescription); 
      $snippet->setCategoryId($videoCategory); 
      $snippet->setTags($videoTags); 

      // Create a video status with privacy status. Options are "public", "private" and "unlisted". 
      $status = new Google_Service_YouTube_VideoStatus(); 
      $status->setPrivacyStatus('public'); 

      // Create a YouTube video with snippet and status 
      $video = new Google_Service_YouTube_Video(); 
      $video->setSnippet($snippet); 
      $video->setStatus($status); 

      // Size of each chunk of data in bytes. Setting it higher leads faster upload (less chunks, 
      // for reliable connections). Setting it lower leads better recovery (fine-grained chunks) 
      $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); 

      /** 
      * Video has successfully been upload, now lets perform some cleanup functions for this video 
      */ 
      if ($status->status['uploadStatus'] == 'uploaded') { 
       $youtube_id = $status->id; //you got here youtube video id 
      } else { 
       // handle failere here 
      } 

      // If you want to make other calls after the file upload, set setDefer back to false 
      $client->setDefer(true); 

     } else{ 
      // @TODO Log error 
      echo 'Problems creating the client'; 
     } 

    } catch(Google_Service_Exception $e) { 
     echo "\r\n Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage(); 
     echo "\r\n Stack trace is ".$e->getTraceAsString(); 
    } catch (Exception $e) { 
     echo "\r\n Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage(); 
     echo "\r\n Stack trace is ".$e->getTraceAsString(); 
    } 
+0

我已經做了以下步驟爲你所提到的: - 1) 2) 3) 4) 我的視頻成功上傳到YouTube,但我想上傳到特定頻道或特定帳戶。現在用戶上傳視頻到他們的YouTube帳戶。 –

+0

在步驟3中,提及訪問令牌。需要訪問令牌才能在YouTube上上傳視頻。您正在從用戶處收集此訪問令牌。這就是爲什麼視頻會上傳到他們的頻道。相反,您應該創建自己的訪問令牌,如步驟3中提供的鏈接中所述。此訪問令牌在一段時間後過期(即,例如1小時)。您需要定期刷新它。 –

+0

@Omprakash:我在答案中添加了代碼。 –

相關問題