我需要您的帮助,找到一种使用Laravel在前端和第三方软件组件之间充当“代理”的方法。
它是我公司网络中的第三方软件,它按需生成文件。例如,如果我遇到http://mycompany-third-party.com/12/csv,它将从他们的数据库中读取数据并将其传送以作为响应下载(在这种情况下为csv文件)。
但是我不能允许用户直接与第三方端点联系,所以我想使用laravel(guzzle)将文件询问给第三方,然后直接重定向到特定的客户端而不将其存储在本地磁盘。
我如何使用laravel / guzzle或其他库来做类似的事情?
这是我的最佳猜测:
$response = Http::withOptions(['stream' => true])->withHeaders([
'X-Metabase-Session' => $this->token,
])->post('http://mycompany-third-party.com/12/csv');
$headers = $response->headers();
return response($response->getBody())->withHeaders($headers);
大家好,我学习了很多,已经找到了解决方案。为此,我们需要使用Streamed Downloads。
这是我的答案:
$response = Http::withOptions(['stream' => true])->withHeaders([
'X-Metabase-Session' => $this->token,
])->post(your-3th-url);
return response()->streamDownload(function () use ($response) {
echo $response->getBody()->getContents();
}, 'fileName.extType');
见。