Hiroki
Hiroki

Reputation: 4193

Write a file from garbled data of GuzzleHttp, getContents (GuzzleHttp\Psr7\Response)

What I've been trying is to have a file from a data, which is supposed to be readable.

Here is a line which is using Google Drive API.

$result = $this->service->files->export("ID", 'mimeType', array(
        'alt' => 'media'))
          ->getBody()
          ->getContents();

$result gets an object of GuzzleHttp\Psr7\Response. This is originally a readable Google.doc file. However, the $result is garbled, and I confirmed it with var_dump().

Does anyone have an idea of converting (or restoring) GuzzleHttp\Psr7\Response objects into a readable file? Also, I'd appreciate if you'd give an idea as to saving the restored $result as a doc, pdf or any other major format.

FYI: I'm using Laravel 5, which has GuzzleHttp.

Upvotes: 1

Views: 597

Answers (1)

Adriaan van der Bergh
Adriaan van der Bergh

Reputation: 75

You need to replace the getContents with read().

This worked for me:

$file = $service->files->export($request->id, 'application/pdf', array('alt' => 'media' ));
$size = $file->getBody()->getSize();
$content = $file->getBody()->read($size);

Upvotes: 1

Related Questions