lundi 19 décembre 2016

Sending multi-dimensional array with files using cURL

Is there a proper way to POST a multi-dimensional array with files using cURL? I am in control of the receiving back-end, which is built using Laravel. But I'm trying find a way to send the data endpoint using cURL, because it's how a client connects to our API and I'm trying to assist them with that.

If I use a Chrome add-on called Postman, I'm able to use the endpoint as intended. But is there a way to make it work using cURL?

I've tried something like:

    $ch = curl_init();

    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'http://ift.tt/2gVqdMJ',
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => $data,
    ]);

    $files = array(
        ['file_data' => new \CURLFile('logo.jpg'), 'file_type' => 1],
        ['file_data' => new \CURLFile('logo2.jpg'), 'file_type' => 2],
    );

    $data = ['files' => $files];

    $response = curl_exec($ch);

    curl_close($ch);

But then I get an Array to string conversion error.

The endpoint expects a parameter called files to be an array, which contains an array of files and their type.

Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire