【问题标题】:Send Multipart Form from PHP with Guzzle使用 Guzzle 从 PHP 发送多部分表单
【发布时间】:2019-03-23 00:51:51
【问题描述】:

我正在尝试在我的 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 时:

我不明白为什么它在 Guzzle 中不起作用。

【问题讨论】:

  • 您遇到了什么错误或问题?
  • Guzzle 不发送带现场图片的多部分

标签: php api guzzle


【解决方案1】:
$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);

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

【讨论】:

    猜你喜欢
    • 2016-09-09
    • 2016-10-27
    • 2019-07-11
    • 2017-06-06
    • 2014-06-24
    • 2021-12-12
    • 2015-08-12
    • 1970-01-01
    • 2019-11-16
    相关资源
    最近更新 更多