Latest

Thursday, July 13, 2017

Add a conversion filter to guzzle response

Asked by: apokryfos


There's a remote service I was using which returns a base64 encoded PDF. I was using this as follows:

$fp = fopen($url, "r");
$fn = md5($url).".pdf";
stream_filter_append($fp, 'convert.base64-decode');
\Storage::drive("public")->put($fn,$fp);
return response()->json(["url"=>\Storage::drive("public")->url($fn)]);

This was used to download and write the PDF file on the public disk so I could give a link to it to users without having to load the entire file in memory. Problem here being that the files are large (50mb PDF so larger in base64).

This was working fine until the remote server decided to add an internal redirect which fopen can't handle. Rather than writing my own solution to handle the internal redirect I decided to transition my code to use Guzzle.

This is what I tried:

$client = new Client();
$res = $client->request("GET", $url);
$fp = $res->getBody();
$fn = md5($url).".pdf";
stream_filter_append($fp, 'convert.base64-decode'); //Works without this
\Storage::drive("public")->put($fn,$fp);
return response()->json(["url"=>\Storage::drive("public")->url($fn)]);

The problem is stream_filter_append, code works fine without it. The error I get is

stream_filter_append() expects parameter 1 to be resource, object given

Is there any way to append a stream filter on the body of a guzzle response?



Source

No comments:

Post a Comment

Adbox