Guzzle ~6.0 multipart and form_params Guzzle ~6.0 multipart and form_params curl curl

Guzzle ~6.0 multipart and form_params


I ran into the same problem. You need to add your form_params to the multipart array. Where 'name' is the form element name and 'contents' is the value. The example code you supplied would become:

$response = $client->post('http://example.com/api', [    'multipart' => [        [            'name'     => 'image',            'contents' => fopen('/path/to/image', 'r')        ],        [            'name'     => 'name',            'contents' => 'Example name'        ]    ]]);


I got there too, but unfortunately it does not work if you have multidimensional params array. The only way i got it to work is if you send the form_paramaters as query parameters in the array:

$response = $client->post('http://example.com/api', [    'query' => [        'name' => 'Example name',    ],    'multipart' => [        [            'name'     => 'image',            'contents' => fopen('/path/to/image', 'r')        ]    ]]);


I googled similar problem and write here advice:

Do not set header "Content-Type" manually for multipart request.