php - 通过 Guzzle 客户端发送图像
问题描述
如何通过 Guzzle(PHP) 发送带有图像的 POST 请求?
通过 curl,一切都应该是这样的:
$ curl -X POST "https://example.com.ua/api/add" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "files=@image.png;type=image/png" -F "test=test"
我试过了:
$multipart = [
[
'name' => 'test',
'contents' => 'test'
], [
'name' => 'files',
'contents' => fopen(public_path($this->user->imagePath()), 'r'),
'filename' => 'filename' . '.' . File::extension(public_path($this->user->imagePath())),
'headers' => ['Content-Type' => 'image/jpeg']
],
];
$guzzleClient->request(
'POST',
self::BASE_URL . '/add',
array_merge([RequestOptions::MULTIPART => $multipart], ['headers' => [
'Accept' => 'application/json',
'Content-Type' => 'multipart/form-data'
]])
);
一切由
<a href="http://docs.guzzlephp.org/en/stable/request-options.html#multipart">docs</a>
调试后外部 API 告诉我 -field "files" missed Content-Type: image/jpeg
我究竟做错了什么?
如何在 Guzzle 中执行此查询?
解决方案
推荐阅读
- c# - 后续 http 请求后 TempData 仍然可用吗?
- angular - 如何在 Angular 7 中处理嵌套的 HTTP 请求?
- javascript - 在 map 函数中切换条件语句
- java - File.lastmodified() 生成错误的日期和月份
- c# - 读取一个文本文件,直到一行包含一些字符串文件,然后再次继续读取下一行,直到遇到另一个字符串
- powershell - Start-AzureRmDataFactoryV2Trigger 触发器 XXX 具有意外的资源提供程序:、意外的资源类型,或两者兼有
- android - 如何在xml中隐藏另一个组件后面的布局?
- node.js - 将基本身份验证与 loopback.js 和 ldap 一起使用
- r - 折线图 ggplot - 两条线,来自同一站点/秒的数据点
- python - 使用 python-requests 请求时出现 SSL 错误