How to POST multiple files to Laravel API using cURL?
I have a Laravel backend and have a customer trying to use our API. We allow file uploads through the API, but it requires that each file be an array. One element is for the file itself and the second element is for the type ID.
I've seen the way they sent the data, and it looks something like:
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'http://path-to-api',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'files' => [
['fileData' => '@/path/to/file', 'fileType' => 1],
['fileData' => '@/path/to/file', 'fileType' => 2],
]
],
CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);
But to my understanding, cURL doesn't support multi-dimensional arrays or it doesn't work properly.
What is a possible solution that I can either have the customer implement, or what could I do on the Laravel backend so that I can accept the array of files, while still being able to validate the parameters?
I've been testing the API using cURL and also tried for the POST fields:
CURLOPT_POSTFIELDS => [
'files' => [
['fileData' => new \CURLFile('/path/to/file'), 'fileType' => 1],
['fileData' => new \CURLFile('/path/to/file'), 'fileType' => 2],
]
],
Please or to participate in this conversation.