I'm using Laravel and I want to get files from api (with providing token and some headers) then forward it directly to the response without saving the file locally.
Currently I'm doing it like this:
$response = $client->get($requestUrl, [
'query' => [
'hash' => $hash
],
'headers' => [
'_token_' => $oauthConfig['api_token'],
'_token_issuer_' => 1
],
'stream' => true,
]);
$type = $response->getHeader('Content-Type')[0];
$body = $response->getBody();
$response = new StreamedResponse(function () use ($body) {
while (!$body->eof()) {
echo $body->read(1024);
}
});
$response->headers->set('Content-Type', $type);
return $response;
But I'm getting this exception:
LogicException
The content cannot be set on a StreamedResponse instance.
Any help / guidance is greatly appreciated. Thanks.
It seems the right thing to do is
http://docs.guzzlephp.org/en/stable/psr7.html
use GuzzleHttp\Stream\Stream;
$response = $client->get($requestUrl, [
'query' => [
'hash' => $hash
],
'headers' => [
'_token_' => $oauthConfig['api_token'],
'_token_issuer_' => 1
],
'stream' => true,
]);
$result = '';
while(!$response->getBody()->eof()){
$result .= $response->getBody()->read(8192);
}
$filename = 'test.gif';
return response()->streamDownload(function () use ($result) {
echo $result;
}, $filename);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.