如何使用GuzzleHttpPsr7Response对象下载文件



我正试图通过他们的api从谷歌存储下载文件,当我传递alt=media参数时,我得到如下响应:

object(GuzzleHttpPsr7Response)#72 (6) {
  ["reasonPhrase":"GuzzleHttpPsr7Response":private]=>
  string(2) "OK"
  ["statusCode":"GuzzleHttpPsr7Response":private]=>
  int(200)
  ["headers":"GuzzleHttpPsr7Response":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":"GuzzleHttpPsr7Response":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":"GuzzleHttpPsr7Response":private]=>
  string(3) "1.1"
  ["stream":"GuzzleHttpPsr7Response":private]=>
  object(GuzzleHttpPsr7Stream)#57 (7) {
    ["stream":"GuzzleHttpPsr7Stream":private]=>
    resource(13) of type (stream)
    ["size":"GuzzleHttpPsr7Stream":private]=>
    NULL
    ["seekable":"GuzzleHttpPsr7Stream":private]=>
    bool(true)
    ["readable":"GuzzleHttpPsr7Stream":private]=>
    bool(true)
    ["writable":"GuzzleHttpPsr7Stream":private]=>
    bool(true)
    ["uri":"GuzzleHttpPsr7Stream":private]=>
    string(10) "php://temp"
    ["customMetadata":"GuzzleHttpPsr7Stream":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对象返回的头。

谢谢。

相关内容

  • 没有找到相关文章

最新更新