Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
发送第一个区块后,Youtube视频上载在php中失败_Php_Video_Youtube_Youtube Api - Fatal编程技术网

发送第一个区块后,Youtube视频上载在php中失败

发送第一个区块后,Youtube视频上载在php中失败,php,video,youtube,youtube-api,Php,Video,Youtube,Youtube Api,我正在使用Youtube API v3.0(php)将视频上传到Youtube。每次我运行脚本都会出错 发生客户端错误:无法启动可恢复上载 我设置了一些检查点,发现分块上传视频有问题 while (!$status && !feof($handle)) { $chunk = fread($handle, $chunkSizeBytes); $status = $media->nextChunk($chunk);

我正在使用Youtube API v3.0(php)将视频上传到Youtube。每次我运行脚本都会出错

发生客户端错误:无法启动可恢复上载

我设置了一些检查点,发现分块上传视频有问题

while (!$status && !feof($handle)) {
            $chunk = fread($handle, $chunkSizeBytes);
            $status = $media->nextChunk($chunk);
        }
我正在使用大小为10.8MB的视频进行上传。因此,上面的循环应该运行11次,但它在第一次时抛出一个错误,因此产生客户机错误消息

代码:


有了这个错误,您还应该能够看到HTTP代码是什么。。。400、401、403等等,你能取回吗?通常,此特定错误消息表示使用了错误的授权方法;您是否检查了oAuth2流以确保其工作正常,并且您正在获取用户的访问令牌?它没有返回任何错误代码,我尝试捕获错误代码,但它返回了0。关于授权方法,同一个系统在localhost中工作,但当我在服务器上尝试它时,会出现错误:无法启动可恢复上载。我确实检查了我是否获得了访问代码/令牌。我通过独立运行脚本解决了这个问题。似乎有另一个进程与上载脚本并行运行。可能是由于安全问题,Youtube Api不允许这样做。现在它对我有好处。
if ($client->getAccessToken()){
    try{
        // REPLACE this value with the path to the file you are uploading.
        $videoPath = 'test.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($title);
        $snippet->setDescription($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 = "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($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);

        $htmlBody .= "<h3>Video Uploaded</h3><ul>";
        $htmlBody .= sprintf('<li>%s (%s)</li>', $status['snippet']['title'], $status['id']);

        $htmlBody .= '</ul>';
    } catch (Google_ServiceException $e) {
        $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',htmlspecialchars($e->getMessage()));
    } catch (Google_Exception $e) {
        $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',htmlspecialchars($e->getMessage()));
    }
}