我正在尝试通过其api从Google存储空间下载文件,当我传递alt = media参数时,得到如下响应:

object(GuzzleHttp\Psr7\Response)#72 (6) {
  ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=>
  string(2) "OK"
  ["statusCode":"GuzzleHttp\Psr7\Response":private]=>
  int(200)
  ["headers":"GuzzleHttp\Psr7\Response":private]=>
  array(16) {
    ["x-guploader-uploadid"]=>
    array(1) {
      [0]=>
      string(98) "AEnB2Uq5d_aKFnzd6d_DeJ_5P8vi9BiXOJpOzixs7mqU5_WK9SF0o9D-8vQiOQWLw"
    }
    ["content-type"]=>
    array(1) {
      [0]=>
      string(24) "application/octet-stream"
    }
    ["content-disposition"]=>
    array(1) {
      [0]=>
      string(10) "attachment"
    }
    ["etag"]=>
    array(1) {
      [0]=>
      string(16) "CODI1+sCEAE="
    }
    ["vary"]=>
    array(2) {
      [0]=>
      string(6) "Origin"
      [1]=>
      string(8) "X-Origin"
    }
    ["x-goog-generation"]=>
    array(1) {
      [0]=>
      string(16) "1458812108000"
    }
    ["x-goog-hash"]=>
    array(1) {
      [0]=>
      string(44) "crc32c=D0rU4U+yXOR8d0GKmxkDw=="
    }
    ["x-goog-metageneration"]=>
    array(1) {
      [0]=>
      string(1) "1"
    }
    ["cache-control"]=>
    array(1) {
      [0]=>
      string(46) "no-cache, no-store, max-age=0, must-revalidate"
    }
    ["pragma"]=>
    array(1) {
      [0]=>
      string(8) "no-cache"
    }
    ["expires"]=>
    array(1) {
      [0]=>
      string(29) "Fri, 01 Jan 1990 00:00:00 GMT"
    }
    ["date"]=>
    array(1) {
      [0]=>
      string(29) "Fri, 25 Mar 2016 09:50:35 GMT"
    }
    ["content-length"]=>
    array(1) {
      [0]=>
      string(2) "52"
    }
    ["server"]=>
    array(1) {
      [0]=>
      string(12) "UploadServer"
    }
    ["alternate-protocol"]=>
    array(1) {
      [0]=>
      string(12) "443:quic,p=1"
    }
    ["alt-svc"]=>
    array(1) {
      [0]=>
      string(49) "quic=":443"; ma=2592000; v="31,30,29,28,27,26,25""
    }
  }
  ["headerLines":"GuzzleHttp\Psr7\Response":private]=>
  array(16) {
    ["X-GUploader-UploadID"]=>
    array(1) {
      [0]=>
      string(98) "AEnB2Uq5d_aKFnzd6d6PxQgxGQwE_8S3CYBrhdsiTcOActigXrv_DeJ_5P8vi9BiXOJpOzixs7mqvQiOQWLw"
    }
    ["Content-Type"]=>
    array(1) {
      [0]=>
      string(24) "application/octet-stream"
    }
    ["Content-Disposition"]=>
    array(1) {
      [0]=>
      string(10) "attachment"
    }
    ["ETag"]=>
    array(1) {
      [0]=>
      string(16) "CODI1+6O2csCEAE="
    }
    ["Vary"]=>
    array(2) {
      [0]=>
      string(6) "Origin"
      [1]=>
      string(8) "X-Origin"
    }
    ["X-Goog-Generation"]=>
    array(1) {
      [0]=>
      string(16) "1458815402108000"
    }
    ["X-Goog-Hash"]=>
    array(1) {
      [0]=>
      string(44) "crc32c=D0r0OQ=yXOR8d0GKmxkDw=="
    }
    ["X-Goog-Metageneration"]=>
    array(1) {
      [0]=>
      string(1) "1"
    }
    ["Cache-Control"]=>
    array(1) {
      [0]=>
      string(46) "no-cache, no-store, max-age=0, must-revalidate"
    }
    ["Pragma"]=>
    array(1) {
      [0]=>
      string(8) "no-cache"
    }
    ["Expires"]=>
    array(1) {
      [0]=>
      string(29) "Fri, 01 Jan 1990 00:00:00 GMT"
    }
    ["Date"]=>
    array(1) {
      [0]=>
      string(29) "Fri, 25 Mar 2016 09:50:35 GMT"
    }
    ["Content-Length"]=>
    array(1) {
      [0]=>
      string(2) "52"
    }
    ["Server"]=>
    array(1) {
      [0]=>
      string(12) "UploadServer"
    }
    ["Alternate-Protocol"]=>
    array(1) {
      [0]=>
      string(12) "443:quic,p=1"
    }
    ["Alt-Svc"]=>
    array(1) {
      [0]=>
      string(49) "quic=":443"; ma=2592000; v="31,30,29,28,27,26,25""
    }
  }
  ["protocol":"GuzzleHttp\Psr7\Response":private]=>
  string(3) "1.1"
  ["stream":"GuzzleHttp\Psr7\Response":private]=>
  object(GuzzleHttp\Psr7\Stream)#57 (7) {
    ["stream":"GuzzleHttp\Psr7\Stream":private]=>
    resource(13) of type (stream)
    ["size":"GuzzleHttp\Psr7\Stream":private]=>
    NULL
    ["seekable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["readable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["writable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["uri":"GuzzleHttp\Psr7\Stream":private]=>
    string(10) "php://temp"
    ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
    array(0) {
    }
  }
}

如何使用它下载文件?

谢谢。

最佳答案

您可以通过以下方式下载文件(pdf,txt,doc):

$content = $service->files->get($file->id, array( 'alt' => 'media' ));
foreach ($content->getHeaders() as $name => $values) {
    header($name . ': ' . implode(', ', $values));
}
header('Content-Disposition: inline; filename="' . $yourfilename . '"');
echo $content->getBody();

使用GuzzleHttp对象的标头返回。

谢谢。

10-06 00:46