2013-01-09 249 views
20

我想上傳使用PHP的Youtube視頻。我使用Youtube API v3,並且正在使用Google API PHP Client庫的最新已檢出源代碼。
我使用
https://code.google.com/p/google-api-php-client/給出的示例代碼進行驗證。身份驗證過得很好,但是當我嘗試上傳視頻時,我收到了Google_ServiceException,錯誤代碼爲500,消息爲空。視頻上傳至YouTube使用YouTube API V3和PHP

我看了一下前面提到的問題: Upload video to youtube using php client library v3但接受的答案沒有描述如何指定要上傳的文件數據。
我發現了另一個類似的問題Uploading file with Youtube API v3 and PHP,凡在評論中提到,的categoryId是強制性的,因此我試圖在段設置的categoryId但仍給出了同樣的異常。

我還提到了文檔站點上的Python代碼(https://developers.google.com/youtube/v3/docs/videos/insert),但我在客戶端庫中找不到函數next_chunk。但我試圖把一個循環(在代碼片段中提到)重試獲得錯誤代碼500,但在所有10次迭代中,我得到相同的錯誤。

以下是代碼片段我想:

$youTubeService = new Google_YoutubeService($client); 
if ($client->getAccessToken()) { 
    print "Successfully authenticated"; 
    $snippet = new Google_VideoSnippet(); 
    $snippet->setTitle = "My Demo title"; 
    $snippet->setDescription = "My Demo descrition"; 
    $snippet->setTags = array("tag1","tag2"); 
    $snippet->setCategoryId(23); // this was added later after refering to another question on stackoverflow 

    $status = new Google_VideoStatus(); 
    $status->privacyStatus = "private"; 

    $video = new Google_Video(); 
    $video->setSnippet($snippet); 
    $video->setStatus($status); 

    $data = file_get_contents("video.mp4"); // This file is present in the same directory as the code 
    $mediaUpload = new Google_MediaFileUpload("video/mp4",$data); 
    $error = true; 
    $i = 0; 

    // I added this loop because on the sample python code on the documentation page 
    // mentions we should retry if we get error codes 500,502,503,504 
    $retryErrorCodes = array(500, 502, 503, 504); 
    while($i < 10 && $error) { 
     try{ 
      $ret = $youTubeService->videos->insert("status,snippet", 
                $video, 
                array("data" => $data)); 

      // tried the following as well, but even this returns error code 500, 
      // $ret = $youTubeService->videos->insert("status,snippet", 
      //          $video, 
      //          array("mediaUpload" => $mediaUpload); 
      $error = false; 
     } catch(Google_ServiceException $e) { 
      print "Caught Google service Exception ".$e->getCode() 
        . " message is ".$e->getMessage(); 
      if(!in_array($e->getCode(), $retryErrorCodes)){ 
       break; 
      } 
      $i++; 
     } 
    } 
    print "Return value is ".print_r($ret,true); 

    // We're not done yet. Remember to update the cached access token. 
    // Remember to replace $_SESSION with a real database or memcached. 
    $_SESSION['token'] = $client->getAccessToken(); 
} else { 
    $authUrl = $client->createAuthUrl(); 
    print "<a href='$authUrl'>Connect Me!</a>"; 
} 

難道我做錯了什麼嗎?

+0

您可以發佈堆棧跟蹤嗎?我懷疑是這個問題:http://code.google.com/p/gdata-issues/issues/detail?id=3961 –

+0

我也看到了這個問題。如果設置爲true,那麼出現錯誤代碼爲400的異常「未捕獲的異常'Google_Exception',並顯示消息'無法啓動可恢復的上載」,否則它總是給出錯誤代碼500,消息爲null。 – jayendrap

回答

8

我能得到上傳使用下面的代碼工作:

if($client->getAccessToken()) { 
    $snippet = new Google_VideoSnippet(); 
    $snippet->setTitle("Test title"); 
    $snippet->setDescription("Test descrition"); 
    $snippet->setTags(array("tag1","tag2")); 
    $snippet->setCategoryId("22"); 

    $status = new Google_VideoStatus(); 
    $status->privacyStatus = "private"; 

    $video = new Google_Video(); 
    $video->setSnippet($snippet); 
    $video->setStatus($status); 

    $error = true; 
    $i = 0; 

    try { 
     $obj = $youTubeService->videos->insert("status,snippet", $video, 
             array("data"=>file_get_contents("video.mp4"), 
             "mimeType" => "video/mp4")); 
    } catch(Google_ServiceException $e) { 
     print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage(). " <br>"; 
     print "Stack trace is ".$e->getTraceAsString(); 
    } 
} 
+0

我使用你的代碼,但它不上傳視頻,我應該添加任何代碼?我添加了echo「obj:」。$ obj;但它沒有顯示任何東西。 – 2013-06-09 03:30:55

+1

是否有任何異常被拋出? – jayendrap

+1

感謝您的回覆,我可以解決這個問題,它的工作原理,但我不能上傳大文件,我試圖使用可恢復的上傳,但不起作用,你有什麼想法嗎? – 2013-06-10 10:22:00

3

我意識到這是舊的,但這裏的答案關閉文檔:

// REPLACE this value with the path to the file you are uploading. 
    $videoPath = "/path/to/file.mp4"; 

    $snippet = new Google_Service_YouTube_VideoSnippet(); 
    $snippet->setTitle("Test title"); 
    $snippet->setDescription("Test description"); 
    $snippet->setTags(array("tag1", "tag2")); 

    // Numeric video category. See 
    // https://developers.google.com/youtube/v3/docs/videoCategories/list 
    $snippet->setCategoryId("22"); 

    // 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); 
+0

這就是這個確切的示例https://github.com/youtube /api-samples/blob/master/php/resumable_upload.php萬一有人發現這個答案,並且對此很好奇。 – Link14

2

我也知道這是老,我克隆了最新版的PHP客戶端的從GitHub我跑的麻煩與Google_Service_YouTube_Videos_Resource::insert() - 方法。

我想傳遞一個陣列"data" => file_get_contents($pathToVideo)"mimeType" => "video/mp4"設置爲用於insert() -method一個參數,但我仍然保持在返回得到(400)錯誤請求

調試,並通過谷歌的代碼讀取我在\Google\Service\Resource.php發現有一個檢查(線179-180)對一個數組鍵"uploadType"將啓動Google_Http_MediaFielUpload對象。

$part = 'status,snippet'; 
$optParams = array(
    "data" => file_get_contents($filename), 
    "uploadType" => "media", // This was needed in my case 
    "mimeType" => "video/mp4", 
); 
$response = $youtube->videos->insert($part, $video, $optParams); 

如果我沒有記錯,與PHP-API是沒有必要的uploadType爭論的0.6版本。這可能僅適用於任何一天的回答顯示的直接上傳的風格,而不是可恢復上傳。

相關問題