Laravel 7 HTTP客户端附加多个文件

用户名

我正在尝试将POST传递到rest API,在那我需要从用户输入表单发送多个文件。我设法使它与单个文件一起使用,但是当有多个文件作为数组发送($ file [])时,我在laravel文档中看不到任何内容可以显示如何完成此操作。

$response = 'API_URL';
        $response = Http::withToken(ENV('API_KEY'))
        ->attach('file', fopen($request->file, 'r'))
        ->post($url, [
           'uuid' => $request->uuid,
        ]);
穆罕默德·戴亚斯·亚斯库

您可以通过以下方式实现:

->attach('file[0]', fopen($request->file[0], 'r'))
->attach('file[1]', fopen($request->file[1], 'r'))

如果您$files是要发送的文件数组,则可以执行以下操作:

$response = Http::withToken(ENV('API_KEY'));
foreach($files as $k => $file)
{
$response = $response->attach('file['.$k.']', $file);
}
$response = $response->post($url, [
           'uuid' => $request->uuid,
        ]);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章