Пример отправки файла CURL PHP
Пример отправки файла полученного методом POST на другой хост через CURL на PHP:
if (isset($_FILES["importXls"]) && $_FILES["importXls"]["tmp_name"] != "") {
$boundary = "------------------------" . md5(mt_rand() . microtime());
$postData = "--{$boundary}\r\n" .
"Content-Disposition: form-data; name=\"file\"; filename=\"" . $_FILES["importXls"]["name"] . "\"\r\n" .
"Content-Type: " . $_FILES["importXls"]["type"] . "\r\n\r\n" .
file_get_contents($_FILES["importXls"]["tmp_name"]) . "\r\n\r\n" .
"--{$boundary}--\r\n";
//print_r($postData);die();
$headers = [
"Content-Type: multipart/form-data; boundary={$boundary}",
"Accept: application/json",
"Authorization: token TOKEN, "")
];
$ch = curl_init();
$options = [
CURLOPT_URL => "https://HOST/api/test",
CURLOPT_HEADER => false,
CURLOPT_POST => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "gzip"
];
curl_setopt_array($ch, $options);
$responce = curl_exec($ch);
if (!curl_errno($ch)) {
$info = curl_getinfo($ch);
if ($info["http_code"] != 200) {
$return = [
"status" => "fail",
"message" => "File uploaded successfully",
"error" => json_decode($responce, true)
];
$errorParce = true;
}
} else {
$return = [
"status" => "fail",
"message" => curl_error($ch)
];
$errorParce = true;
}
curl_close($ch);
$responce = json_decode($responce, true);
}
Результат запроса подразумевается, что будет в формате JSON и будет получен в переменную $responce.