使用 Guzzle 从 PHP 发送多部分表单
Send Multipart Form from PHP with Guzzle
我正在尝试为我的 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 中不起作用。
$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);
只需将文件保存在本地并读取流或正确发送即可。
我正在尝试为我的 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 中不起作用。
$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);
只需将文件保存在本地并读取流或正确发送即可。