通过Guzzle Client发送图像

时间:2018-12-20 16:35:36

标签: php guzzle

如何通过Guzzle(PHP)发送带有图像的POST请求?

通过卷曲,一切都应该像这样:

$ 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中执行此查询?

0 个答案:

没有答案