Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guzzle ~6.0 multipart and form_params

Tags:

php

curl

guzzle

I am trying to upload file and send post parameters at the same time like this:

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

However my form_params fields are ignored and only the multipart fields are present in my post body. Can I send both at all with guzzle 6.0 ?

like image 630
Jordan Dobrev Avatar asked Jun 04 '15 14:06

Jordan Dobrev


1 Answers

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'
        ]
    ]
]);
like image 200
Simon Crowfoot Avatar answered Oct 07 '22 19:10

Simon Crowfoot