<?php
$id = filter_input(INPUT_POST, 'id' );
$first_name = filter_input(INPUT_POST, 'first_name');
$last_name = filter_input(INPUT_POST, 'last_name');
$middle_name = filter_input(INPUT_POST, 'middle_name');
$email = filter_input(INPUT_POST, 'email' );
$phone = filter_input(INPUT_POST, 'phone' );
$applicant = array(
'r_id'=> (int)$id,
'first_name' => $first_name,
'last_name' => $last_name,
'middle_name' => $middle_name,
'email' => $email,
'phone' => $phone,
'source_type' => 'applied',
);
$fields = array('applicant' => $applicant);
// URL to upload to
$url = "https://site.io/api/v2/orders";
$token = "f";
// curl
$curl = curl_init();
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$post_data = build_data_files($boundary, $fields);
// print_r($post_data);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_data,
CURLINFO_HEADER_OUT => true,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $token",
"Content-Type: multipart/form-data; charset=utf-8; boundary=" . $delimiter,
"Content-Length: " . strlen($post_data)
),
));
$response = curl_exec($curl);
$info = curl_getinfo($curl);
echo "code: ${info['http_code']}";
print_r($info['request_header']);
var_dump($response);
$err = curl_error($curl);
echo "error";
var_dump($err);
curl_close($curl);
function build_data_files($boundary, $fields){
$data = '';
$eol = "\r\n";
$delimiter = '-------------' . $boundary;
foreach ($fields['applicant'] as $name => $content) {
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="applicant[' . $name . "]\"".$eol.$eol
. $content . $eol;
}
// Добавление файла
$content = file_get_contents($_FILES["cv"]["tmp_name"]);
echo $content;
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="applicant[cv]"; filename="' . $_FILES["cv"]["name"] . '"' . $eol
. 'Content-Type: '. $_FILES["cv"]["type"] .$eol
. 'Content-Transfer-Encoding: base64'.$eol
;
$data .= $eol;
$data .= $content . $eol;
// Добавление файла
$data .= "--" . $delimiter . "--".$eol;
return $data;
}