通过API导出大型Google文件

时间:2017-06-22 01:40:00

标签: php google-api google-drive-api google-api-php-client

我想知道如何通过API执行大型Google幻灯片导出。我已按照说明here进行操作,并且可以正常使用小文件。如果我尝试使用大文件,它就不起作用了。它没有返回错误或任何东西。我使用的逻辑如下:

<?php session_start();
ini_set('max_execution_time', 300); //300 seconds = 5 minutes    

//INCLUDE PHP CLIENT LIBRARY
require_once 'vendor/autoload.php';

// Create client object
$client = new Google_Client(); 
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/main.php');
$client->setAuthConfig("client_secret.json");
$client->addScope(array('https://www.googleapis.com/auth/drive'));
$client->setAccessType('offline');

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {  

  $client->setAccessToken($_SESSION['access_token']);
  $service = new Google_Service_Drive($client);
  $pptFile = fopen("testPresentation.pptx", "w+");
  $fileId = '1fx2uqe96NgsOabAv8S_mItlEbJ_NJSORqPitH55TMc';

  try {

    $file = $service->files->export($fileId,"application/vnd.openxmlformats-officedocument.presentationml.presentation", array("alt" => "media"));
    $fileContent = $file->getBody()->getContents();    

    fwrite($outHandle, $fileContent);

  } catch(Exception $e) {

    print $e->getMessage();

  }

  //Close output file handle.
  fclose($outHandle);
  echo "Done.\n"; 

} else {

  if (! isset($_GET['code'])) {

  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));

  } else {

  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/main.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));

  }

}

就像我说的,它适用于小文件,但不适用于大文件。我注意到的一件事是,当我在小文件上执行var_dump($file);时,&#39; Content-Length&#39; 标头的大小为 &#34; 30083&#34;

如果我在大文件上执行相同操作,则&#39; Content-Length&#39; 标头的大小为&#34; 0&#34; 。我假设导出大文件必须做不同的事情。我甚至遇到了这个https://github.com/google/google-api-php-client/issues/1124并尝试了那里的片段,但没有成功。

0 个答案:

没有答案