Guzzle 6: upload files with array data

laravel guzzle post form data file
guzzle upload file
guzzle headers
guzzle authorization header
guzzle basic auth
guzzle tutorial
guzzle large files
requestoptions guzzle

I have to send array data with files. Using just data work properly:

$client->post('http://xxx/', [
    'form_params' => [
        [
            'data' => ['id' => 1234, 'name' => 'nombre'],
            'valid' => true
        ]
    ]
]);

But since I can't use "form_params" with "multipart", how can send files with arrays and boolean data?

I've tried:

$client->post('http://xxx/', [
    'multipart' => [
        [
            'name'     => 'myfile',
            'contents' => fopen('my_file.txt', 'r'),
        ],
        [
            'name'     => 'data',
            'contents' => ['id' => 1234, 'name' => 'nombre'],
        ]
        [
            'name'     => 'valid',
            'contents' => true,
        ]
    ],
]);

but I get a error since "contents" doesn't accept boolean or array values.

I need some help.

Thanks

Update: I couldn't solve the problem and finally I had to user a not very nice solution, including as query string the form filed parameter and using just Multipart. Something like:

$client->post('http://xxx?id=1234&name=nombre', [
'multipart' => [
    [
        'name'     => 'myfile',
        'contents' => fopen('my_file.txt', 'r'),
    ],
],

]);

php - Guzzle 6: upload files with array data, I have to send array data with files. Using just data work properly: $client->post(' http://xxx/', [ 'form_params' => [ [ 'data' => ['id' => 1234, 'name' => 'nombre'],� Sending form files¶ You can send files along with a form (multipart/form-data POST requests), using the multipart request option. multipart accepts an array of associative arrays, where each associative array contains the following keys: name: (required, string) key mapping to the form field name.

There is a option to send upload files and array data together via Guzzle but in changed form:

$client->post('http://xxx/', [
    'multipart' => [
        [
            'name'     => 'myfile',
            'contents' => fopen('my_file.txt', 'r'),
        ],
        [
            'name'     => 'data[0][id]',
            'contents' => 1234,
        ],
        [
            'name'     => 'data[0][name]',
            'contents' => 'nombre',
        ],
        [
            'name'     => 'data[1][id]',
            'contents' => 2222,
        ],
        [
            'name'     => 'data[1][name]',
            'contents' => 'nombre2',
        ],
        [
            'name'     => 'valid',
            'contents' => true,
        ]
    ],
]);

On the other side you can fetch data with $_POST['data'] and then do the foreach:

foreach ($_POST['data'] as $data) {
    echo "Id: ".$data['id'];
    echo "Name: ".$data['name'];
}

Quickstart — Guzzle Documentation, The client constructor accepts an associative array of options: An easy way to upload JSON data and set the appropriate header is using the json request� In my case, I should accept json data from user in API request, along with local path to a file. There is no way to upload file, no form, only raw data. Not sure how it is going to work. May be I will ask user to convert image to base64 and then pass data to me. Not sure how to get user's local file contents through my API.

You can have a recursive method that gets your array of data and prepares the multipart array:

private function prepareMultipartParams($params, $baseKey = null): array
{
    $result = [RequestOptions::MULTIPART => []];
    foreach ($params as $key => $value) {
        $key = $baseKey == null ? $key : "{$baseKey}[{$key}]";
        if (is_array($value))
            foreach ($this->prepareMultipartParams($value, $key) as $array)
                $result[RequestOptions::MULTIPART] = array_merge($result[RequestOptions::MULTIPART], $array);
        else
            $result[RequestOptions::MULTIPART][] = [
                'name' => $key,
                'contents' => $value instanceof UploadedFile ? fopen($value->path(), 'r+') : $value
            ];
    }

    return $result;
}

How to upload file with Guzzle client? � Issue #198 � FriendsOfPHP , I'm trying to use the Guzzle client to send a form with file. There is no way to upload file, no form, only raw data. Not sure how it is going to� HTTP request messages¶. Request objects are all about building an HTTP message. Each part of an HTTP request message can be set individually using methods on the request object or set in bulk using the setUrl() method.

Send files and multidimensional array with Guzzle 6 � Issue #1679 , hello, I have this query in a project with Guzzle 5.3 $request = $this->guzzleHttp- >createRequest('POST', 'my_host', []); $postBody� Request Options¶. You can customize requests created and transferred by a client using request options.Request options control various aspects of a request including, headers, query string parameters, timeout settings, the body of a request, and much more.

File upload using guzzle client, filepath], $options); i want to upload file using guzzle a i just need data of uploaded file data like size, tmp_name inside $_FILES array. Guzzle, PHP HTTP client. Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc

Upload file using Guzzle 6 to API endpoint, I am able to upload a file to an API endpoint using PostmanI am trying to translate I am having is setting the multipart and headers array with the correct data. Reading Time: < 1 minute Guzzle is a PHP HTTP Client for sending HTTP requests to APIs.But sending multiple files with a single request sometimes bring on a lot of complexity and people get confused in sending arrays of data within an array for a multipart form, quite confusing.

Comments
  • I am fighting this problem myself. I got array data covered by setting [headers] => [ [Content-Type] => application/x-www-form-urlencoded ]. But I cannot get a file to pass with this data. See this issue about array's as values of contents github.com/guzzle/guzzle/issues/1177
  • Thanks so much @Jabari, your first option is not possible since I need send an array with "data" key, second one I already tried and 3rd one doesn't work. The only possibility is sending "form_param" option... Is there any way to send files inside "form_params" option? Thanks again
  • @Averias I'll do some testing later today, I'm interested in getting this working as well.