I am trying to send an uploaded file via cURL with PHP.
<?php
if (!isset($_POST['idcampagnestatus'])) {
header("Location: exampleblablavacatures/?vacature=0&errors=1");
die();
}
$file_path = 'cv/' . time() . '-' . $_FILES['docsbestand']['name'];
file_put_contents($file_path, file_get_contents($_FILES['docsbestand']['tmp_name']));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "exampleapiurl",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"docsbestand\"; filename=\"" . 'exampleblablawp-content/plugins/hoezorgjij-api/' . $file_path . "\"\r\nContent-Type: " . $_FILES['docsbestand']['type'] . "\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
unlink($file_path);
if ($err) {
echo "cURL Error #:" . $err;
} else {
if (json_decode($response)->FOUT) {
header("Location: exampleblablavacatures/?vacature=0&errors=1");
die();
} else {
header("Location: exampleblablavacatures/?vacature=0&errors=0");
die();
}
}
I tried other things, but often I get an empty file. The idea is that I save a file to the webserver and then send it to an API which should process it and put it on their own webserver. More details are sent through the API and these all work perfectly. What am I missing? Thank you in advance.