如何使用PHP恢复可恢复上传到YouTube

时间:2015-10-22 06:52:11

标签: php algorithm upload youtube-api

我正在使用可恢复上传方法将大型文件从我的服务器传输到YouTube(请参阅下面的代码)。当我在PHP过程中遇到错误时,如何保存/恢复上传?更准确地说,上传存储的实际状态在哪里以及上传中断后放置文件指针的位置在哪里?

    /*
        Uploads one file to youtube chunk by chunk
    */
    function uploadFile($dbfile) {
        $client = $this->client;
        $youtube = new Google_Service_YouTube($client);
        $htmlBody = "";
        try {

            // 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("SO Example");

            // 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 = "private";

            // 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($dbfile->localfile));

            // Read the media file and upload it chunk by chunk.
            $status = false;
            $handle = fopen($dbfile->localfile, "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);

            $log = array("success" => true, "snippet_id" => $status["id"]);
        } catch (Google_ServiceException $e) {
            $log = array("success" => false, "errormsg" => $e->getMessage());
        } catch (Google_Exception $e) {
            $log = array("success" => false, "errormsg" => $e->getMessage());
        }
        return $log;
    }

0 个答案:

没有答案