2013-01-09 19 views
20

का उपयोग कर यूट्यूब पर वीडियो अपलोड करें मैं PHP का उपयोग कर यूट्यूब में एक वीडियो अपलोड करने की कोशिश कर रहा हूं। मैं यूट्यूब एपीआई v3 का उपयोग कर रहा हूं और मैं Google एपीआई PHP क्लाइंट लाइब्रेरी के नवीनतम चेक आउट स्रोत कोड का उपयोग कर रहा हूं।
मैं प्रमाणीकरण करने के लिए
https://code.google.com/p/google-api-php-client/ पर दिए गए नमूना कोड का उपयोग कर रहा हूं। प्रमाणीकरण ठीक से चला जाता है लेकिन जब मैं एक वीडियो अपलोड करने का प्रयास करता हूं तो मुझे Google_ServiceException त्रुटि कोड 500 और शून्य के रूप में संदेश मिलता है।यूट्यूब एपीआई वी 3 और PHP

मैंने पहले निम्नलिखित प्रश्नों पर एक नज़र डाली थी: Upload video to youtube using php client library v3 लेकिन स्वीकार्य उत्तर फ़ाइल डेटा को अपलोड करने के तरीके को निर्दिष्ट करने का वर्णन नहीं करता है।
मुझे एक और समान प्रश्न Uploading file with Youtube API v3 and PHP मिला, जहां टिप्पणी में यह उल्लेख किया गया है कि श्रेणी आईडी अनिवार्य है, इसलिए मैंने श्रेणी आईडी को स्निपेट में सेट करने का प्रयास किया लेकिन फिर भी यह एक ही अपवाद देता है।

मैंने प्रलेखन साइट (https://developers.google.com/youtube/v3/docs/videos/insert) पर पायथन कोड को भी संदर्भित किया, लेकिन मुझे क्लाइंट लाइब्रेरी में अगला_चंक फ़ंक्शन नहीं मिला। लेकिन मैंने त्रुटि कोड 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

मैंने उस मुद्दे को भी देखा था।जब सत्य के रूप में पुन: प्रारंभ करने योग्य सेट किया जाता है, तो मुझे त्रुटि अपवाद के साथ "अपवादित अपवाद 'Google_Exception' संदेश के साथ 'पुनः प्रारंभ करने योग्य अपलोड शुरू करने में विफल" त्रुटि मिलती है, अन्यथा यह हमेशा त्रुटि कोड के साथ त्रुटि कोड 500 देता है। – 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

मैं आपके कोड का उपयोग कर रहा हूं लेकिन यह वीडियो अपलोड नहीं करता है, क्या मुझे कोड में कुछ भी जोड़ना चाहिए? मैंने गूंज जोड़ा है "obj:"। $ Obj; लेकिन यह कुछ भी नहीं दिखाता है। –

+1

क्या कोई अपवाद है जो फेंक दिया गया है? – jayendrap

+1

आपकी प्रतिक्रिया के लिए धन्यवाद, मैं इस मुद्दे को हल कर सकता हूं, यह काम करता है लेकिन मैं बड़ी फाइलें अपलोड नहीं कर सकता, मैं फिर से शुरू करने योग्य अपलोड का उपयोग करने की कोशिश कर रहा हूं लेकिन क्या आपको कोई विचार नहीं है? –

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-क्लाइंट का नवीनतम संस्करण क्लोन किया था, मैं Google_Service_YouTube_Videos_Resource::insert() -method के साथ परेशानी में भाग गया।

मैं "data" => file_get_contents($pathToVideo) और "mimeType" => "video/mp4"insert() -method के लिए एक तर्क के रूप में सेट के साथ एक सरणी से होकर गुजरेगा, लेकिन मैं अभी भी (400) बदले में BadRequest हो रही है।

डिबगिंग और गूगल के कोड के माध्यम से पढ़ने मैं \Google\Service\Resource.php में पाया एक चेक एक सरणी कुंजी "uploadType" कि Google_Http_MediaFielUpload वस्तु आरंभ होगा के खिलाफ (लाइनों 179-180 पर) नहीं था।

$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 के संस्करण 0.6 के साथ अपलोडटाइप तर्क की आवश्यकता नहीं थी। यह केवल प्रत्यक्ष अपलोड शैली के लिए लागू हो सकता है और पुन: प्रयोज्य किसी भी दिन के उत्तर में दिखाया गया अपलोड नहीं हो सकता है।