Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/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
Php 如何在YouTube上上传大文件_Php_Upload_Google Api_Youtube Api_Google Api Php Client - Fatal编程技术网

Php 如何在YouTube上上传大文件

Php 如何在YouTube上上传大文件,php,upload,google-api,youtube-api,google-api-php-client,Php,Upload,Google Api,Youtube Api,Google Api Php Client,我尝试过两种方法在YouTube上上传大文件,但都不管用,每种方法都有自己的问题,我的目标是找到上传大文件的正确答案 第一种方法: <?php require_once 'google-api-php-client/src/Google_Client.php'; require_once 'google-api-php-client/src/contrib/Google_YouTubeService.php'; require_once 'google-api-php-client/src

我尝试过两种方法在YouTube上上传大文件,但都不管用,每种方法都有自己的问题,我的目标是找到上传大文件的正确答案

第一种方法:

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_YouTubeService.php';
require_once 'google-api-php-client/src/service/Google_MediaFileUpload.php';
session_start();

$client = new Google_Client();
$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('My Client ID');
$client->setClientSecret('My secret code');
$client->setRedirectUri('http://localhost:8888/mymediaapp2/uploadvideo.php');
$client->setDeveloperKey('My developer key');
$client->setScopes("https://www.googleapis.com/auth/youtube.upload");
$youTubeService = new Google_YoutubeService($client);

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
echo "here";
$json = $client->getAccessToken();
$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);
$myvar = "";
foreach ($jsonIterator as $key => $val) {
   if($key == "access_token")
    echo "val is" .$val;
}

    $service_url = 'https://www.googleapis.com/upload/youtube/v3/videos?
    uploadType=resumable&part=snippet,status';
    $c = curl_init($service_url);
    $curl_post_data = array(
        "Authorization" => $val,
        "Content-Length" => '255',
        "Content-Type" => 'application/json; charset=UTF-8',
        "X-Upload-Content-Length" => '30000',
        "X-Upload-Content-Type" => 'video/mov'
    );

    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_POSTFIELDS, $curl_post_data);
    $curl_resp = curl_exec($c);

    curl_close($c);
    echo $curl_resp;
}else {
  $authUrl = $client->createAuthUrl();
  print "<a href='$authUrl'>upload</a>";
}



?>
{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login 
Required", "locationType": "header", "location": "Authorization" } ], "code": 401, 
"message": 
"Login Required" } } 
它将大文件分成不同的部分,而不是单独发送并立即上载,而是单独上载。最后,我在Youtube上列出了一长串文件中不可加载的不同部分,而不是一个文件

方法1的代码

if ($client->getAccessToken()) {
  $videoPath = "path/to/foo.mp4";
  $snippet = new Google_VideoSnippet();
  $snippet->setTitle("Test title2");
  $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);

  $chunkSizeBytes = 1 * 1024 * 1024;
  $media = new Google_MediaFileUpload('video/mp4', null, true, $chunkSizeBytes);
  $media->setFileSize(filesize($videoPath));

  $result = $youtube->videos->insert("status,snippet", $video,
      array('mediaUpload' => $media));

  $status = false;
  $handle = fopen($videoPath, "rb");
  while (!$status && !feof($handle)) {
    $chunk = fread($handle, $chunkSizeBytes);
    $uploadStatus = $media->nextChunk($result, $chunk);
  }

  fclose($handle);
}
我发现它的答案代码类似于google_mediaFileupload,但我不知道如何使用它

第二种方法

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_YouTubeService.php';
require_once 'google-api-php-client/src/service/Google_MediaFileUpload.php';
session_start();

$client = new Google_Client();
$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('My Client ID');
$client->setClientSecret('My secret code');
$client->setRedirectUri('http://localhost:8888/mymediaapp2/uploadvideo.php');
$client->setDeveloperKey('My developer key');
$client->setScopes("https://www.googleapis.com/auth/youtube.upload");
$youTubeService = new Google_YoutubeService($client);

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
echo "here";
$json = $client->getAccessToken();
$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);
$myvar = "";
foreach ($jsonIterator as $key => $val) {
   if($key == "access_token")
    echo "val is" .$val;
}

    $service_url = 'https://www.googleapis.com/upload/youtube/v3/videos?
    uploadType=resumable&part=snippet,status';
    $c = curl_init($service_url);
    $curl_post_data = array(
        "Authorization" => $val,
        "Content-Length" => '255',
        "Content-Type" => 'application/json; charset=UTF-8',
        "X-Upload-Content-Length" => '30000',
        "X-Upload-Content-Type" => 'video/mov'
    );

    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_POSTFIELDS, $curl_post_data);
    $curl_resp = curl_exec($c);

    curl_close($c);
    echo $curl_resp;
}else {
  $authUrl = $client->createAuthUrl();
  print "<a href='$authUrl'>upload</a>";
}



?>
{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login 
Required", "locationType": "header", "location": "Authorization" } ], "code": 401, 
"message": 
"Login Required" } } 
我也尝试使用可恢复上载来上载它们,但当我使用以下代码时,它会遇到以下错误:

方法2的代码

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_YouTubeService.php';
require_once 'google-api-php-client/src/service/Google_MediaFileUpload.php';
session_start();

$client = new Google_Client();
$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('My Client ID');
$client->setClientSecret('My secret code');
$client->setRedirectUri('http://localhost:8888/mymediaapp2/uploadvideo.php');
$client->setDeveloperKey('My developer key');
$client->setScopes("https://www.googleapis.com/auth/youtube.upload");
$youTubeService = new Google_YoutubeService($client);

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
echo "here";
$json = $client->getAccessToken();
$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);
$myvar = "";
foreach ($jsonIterator as $key => $val) {
   if($key == "access_token")
    echo "val is" .$val;
}

    $service_url = 'https://www.googleapis.com/upload/youtube/v3/videos?
    uploadType=resumable&part=snippet,status';
    $c = curl_init($service_url);
    $curl_post_data = array(
        "Authorization" => $val,
        "Content-Length" => '255',
        "Content-Type" => 'application/json; charset=UTF-8',
        "X-Upload-Content-Length" => '30000',
        "X-Upload-Content-Type" => 'video/mov'
    );

    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_POSTFIELDS, $curl_post_data);
    $curl_resp = curl_exec($c);

    curl_close($c);
    echo $curl_resp;
}else {
  $authUrl = $client->createAuthUrl();
  print "<a href='$authUrl'>upload</a>";
}



?>
{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login 
Required", "locationType": "header", "location": "Authorization" } ], "code": 401, 
"message": 
"Login Required" } } 

请注意,我为解决代码中的一个问题的一个答案提供了50英镑的奖金。尽管问题尚未完全解决。

您应该在中创建一个项目,并从该控制台获取客户id和密码

他有所有的信息

首先,你将从一开始


您还可以了解如何执行此操作。

您忘记设置API密钥,您可以使用可恢复上载。

现在,支持可恢复上载,但大多数大型上载都来自直接上载

应该只是在中设置一个参数的问题

有一个类的用法示例,相关行为:

$media = new Google_MediaFileUpload('video/mp4', null, true, $chunkSizeBytes);
对于身份验证,可以使用中的示例。
尝试创建已安装的应用程序和其他应用程序,在本地测试时使用该客户端id和密码。

我不明白你的意思,你能给我一个示例或示例代码吗?这与代码无关,请阅读本文档并在代码之前应用每个步骤。很好,但不能解决您在开发控制台中创建项目并从API启用数据AAPI v3的问题。然后你使用了客户端id和密码,并在代码中替换了它们,对吗?还有什么部分不起作用?这段视频还可以帮助您将API密钥和客户端id、机密分开。若您使用API密钥,那个么您可以在不使用用户日期的情况下进行调用,并且不需要使用客户机id和密码。当您使用它们时,您可以访问一般可用的数据和用户数据,并且不需要API密钥。API密钥用于简单访问。@IbrahimUlukaya那么您的建议是什么?如果您在开发人员控制台中正确创建了项目,启用了YouTube数据API,使用了最新的客户端库,并复制粘贴了客户端id和密码,应该可以工作。不需要API密钥。API密钥仅用于简单访问(您不需要登录的东西,如搜索公共视频)。我测试了示例1,没有任何问题。谢谢你的回答,你提供的示例正是我正在使用的示例,这段代码的问题在于,它没有将它分别发布的块放在一起。所以最后我会在我的youtube账户上找到一长串文件的不同部分。然而,由于它们都是原始文件的一部分,所以它们都不起作用。如果将该参数设置为$media=new Google_MediaFileUpload('video/mp4',null,true,$chunkSizeBytes);它将作为一个上传比较我的代码与你的和构造函数我找不到我的代码有任何不同。您指的是什么区别?还有,为什么您建议我使用已安装的应用程序进行测试?如果您在本地进行测试,您将收到回令牌,而不是Web服务器