使用Guzzle从PHP发送多部分表单

问题描述 投票:0回答:1

我正在尝试在我的API中为上传文件创建一个多部分表单。根据guzzle的文件:

\\$data = picture of form
\\$uri = Endpoint in my API
$this->client->post($uri, [
     'multipart' => [
        [
          'name' => 'picture',
          'contents' => file_get_contents($data),
        ]
      ],
      'headers' => $headers
]);

当我使用Insomnia或Postman时:enter image description here

我不明白为什么它不在Guzzle工作。

php api guzzle
1个回答
0
投票
$file = new File($data->getPathname());
        $tmp_picture_path = Storage::putFile("temp_dir", $file);
        $tmp_picture = Storage::readStream($tmp_picture_path);
        $response = $this->client->post(
            $uri,
            [
                'multipart' => [
                    [
                        'name' => 'picture',
                        'contents' => $tmp_picture,
                        'filename' => "avatar." . $data->getClientOriginalExtension()
                    ]
                ],
                'headers' => $headers
            ]
        );
        Storage::delete($tmp_picture_path);

只需保存本地文件并读取流正确发送它。

© www.soinside.com 2019 - 2024. All rights reserved.