首页 > 解决方案 > PHP Laravel 5 使用 Guzzle 上传文件

问题描述

我正在尝试使用 Laravel 5 和 Guzzle 上传文件。我正在尝试作为表单数据发送,但我不断收到 400 错误请求。我也尝试使用 cURL 并得到了同样的错误。

这是我试图与 Guzzle 一起使用的内容。当我在 Postman 应用程序中测试它并将其作为表单数据发送时,该 API 有效。

$image = $request->image->storeAs('images', $request->image->getClientOriginalName());
$path = base_path().'/storage/app/';
$client = new Client(); //GuzzleHttp\Client
$response = $client->post($url, [
    'multipart' => [
        [
            'name' => 'input_profile_image',
            'contents' => fopen($path.'/'.$image, 'r'),
            'headers' => ['Content-Type' => 'multipart/form-data']
        ],
        [
            'name' => 'input_email',
            'contents' => $email,
            'headers' => ['Content-Type' => 'multipart/form-data']
        ],
        [
            'name' => 'input_pass',
            'contents' => $password,
            'headers' => ['Content-Type' => 'multipart/form-data']
        ],
        [
            'name' => 'api_request',
            'contents' => 'true',
            'headers' => ['Content-Type' => 'multipart/form-data']
        ]
    ]
]);

我错过了什么?

标签: phplaravelcurllaravel-5guzzle

解决方案


推荐阅读