I’ll like to upload videos to youtube, I have correctly the token, but never uploads it , gives no error… it stops execution at line $status = $media->nextChunk($chunk);
Here is my code:
if ($client->getAccessToken()) { try { $videoData['title'] = "test"; $videoData['description'] = "descripcio"; $videoData['tags'] = "tag1, tgag2, tag3"; $videoData['privacy'] = "https://XXXXXX.com/privacy.html"; $videoPath = "/Compilations/u4vmacx0-6685l2bm5aan67nknwbp-wm.mp4"; // 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($videoData['title']); $snippet->setDescription($videoData['description']); $snippet->setTags(explode(",", $videoData['tags'])); // 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 = $videoData['privacy']; // 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 = 50 * 1024 * 1024; // era 1 // 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)) { echo "YES 1"; $chunk = fread($handle, $chunkSizeBytes); echo "YES 2"; $status = $media->nextChunk($chunk); echo "NOT REACHED"; // NEVER REACHES THAT LINE } fclose($handle); // If you want to make other calls after the file upload, set setDefer back to false $client->setDefer(false); if(!empty($status['id'])){ // Uploaded youtube video info $video_title = $status['snippet']['title']; $video_desc = $status['snippet']['description']; $video_tags = implode(",",$status['snippet']['tags']); $youtube_video_id = $status['id']; $status = 'success'; $statusMsg = 'Video has been uploaded to YouTube successfully!'; echo $status. " ". $statusMsg; } } catch (Google_Service_Exception $e) { $statusMsg = 'A service error occurred: <code>'.$e->getMessage().'</code>'; } catch (Google_Exception $e) { $statusMsg = 'An client error occurred: <code>'.$e->getMessage().'</code>'; $statusMsg .= '<br/>Please reset session <a href="logout.php">Logout</a>'; } }
when executing the code, it outputs “YES 1YES 2” , never reaches echo “NOT REACHED”; line…. why? Can I add an exception or something to tell me the error? The video file exists and the program finds it, as the file size is correct.
Thanks
Advertisement
Answer
Very hard to spot the mistake with no logs or exceptions, but how about $videoData['privacy'] = "https://XXXXXX.com/privacy.html"
?
Privacy status can be PUBLIC | PRIVATE | UNLISTED
, and on ‘normal’ accounts it can be only PUBLIC
. Maybe that could be mistake?