Интернет |
Contact form 7. Отправка данных в API (curl)
Здравствуйте. Очень нужна помощь с запросом. Перепробовала множество вариантов, идеи закончились.
Проблема у меня с отправкой вложения в АПИ, первый запрос. Получаю ответ {"status":"ok","objects":[]}. В objects должен быть id.
Мой код
Тестировала в Postman добавление файла. Там следующий код
Основываясь на нем пробовала еще следующий вариант. Но получала такие ошибки:
{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
wp-json/contact-form-7/v1/contact-forms/55/feedback error 500
Проблема у меня с отправкой вложения в АПИ, первый запрос. Получаю ответ {"status":"ok","objects":[]}. В objects должен быть id.
Мой код
Код:
<?php
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_start_function' );
function wpcf7_before_send_mail_start_function($contact_form){
$submission = WPCF7_Submission::get_instance();
// Get the post data and other post meta values
if ( $submission ) {
$posted_data = $submission->get_posted_data();
$upload_file = $submission->uploaded_files();
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$filePath = '{$upload_file["CV"]}';
$cfile = new CURLFile($filePath, filetype($filePath), basename($filePath));
// Assign POST data
$file_data = [
'file' => $cfile
];
$headers = array("Content-Type:multipart/form-data; boundary=" . $delimiter);
$chf = curl_init("https://cleverstaff.net/hr/public/addFile");
curl_setopt($chf, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($chf, CURLOPT_POSTFIELDS, $file_data);
curl_setopt($chf, CURLOPT_HTTPHEADER, $headers);
curl_setopt($chf, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chf, CURLOPT_POST, true);
curl_setopt($chf, CURLOPT_ENCODING, "");
curl_setopt($chf, CURLOPT_SSL_VERIFYPEER, false);
$responsefile = curl_exec($chf);
$responsefile = json_decode($responsefile);
$responsefile = $responsefile->objects[0];
curl_close($chf);
if($responsefile){
$data = json_encode(array(
"name" => "{$posted_data["Name"]}",
"lastName" => "{$posted_data["LastName"]}",
"phone" => "{$posted_data["Telephone"]}",
"message" => "{$posted_data["message"]} Skype: {$posted_data["Skype"]} Beachance: {$posted_data["Beachance"]}",
"vacancyId" => "{$posted_data["post-slug"]}",
"fileId" => "[{\"name\":\"{$posted_data["CV"]}\", \"attId\":\" {$responsefile}\"}]",
"email" => "{$posted_data["Email"]}",
"lang" => "ru"
));
$ch = curl_init("https://cleverstaff.net/hr/public/sendApplication");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
}
return;
}
} ?>
Код:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://cleverstaff.net/hr/public/addFile",
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=\"file\"; filename=\"test.docx\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"postman-token: 5852d237-a41e-6b1b-cff1-44e788d3f650"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
wp-json/contact-form-7/v1/contact-forms/55/feedback error 500
Код:
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_start_function' );
function wpcf7_before_send_mail_start_function($contact_form){
$submission = WPCF7_Submission::get_instance();
// Get the post data and other post meta values
if ( $submission ) {
$posted_data = $submission->get_posted_data();
$upload_file = $submission->uploaded_files();
// files to upload
$filenames = array("{$upload_file["CV"]}");
$files = array();
foreach ($filenames as $f){
$files[$f] = file_get_contents($f);
}
// URL to upload to
$url = "https://cleverstaff.net/hr/public/addFile";
$curl = curl_init();
$url_data = http_build_query($data);
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$post_data = build_data_files($boundary, $files);
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,
CURLOPT_HTTPHEADER => array(
"Content-Type: multipart/form-data; boundary=" . $delimiter,
"Content-Length: " . strlen($post_data)
),
));
$responsefile = curl_exec($curl);
curl_close($curl);
function build_data_files($boundary, $files){
$data = '';
$eol = "\r\n";
$delimiter = '-------------' . $boundary;
foreach ($files as $name => $content) {
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
. 'Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'.$eol
. 'Content-Transfer-Encoding: binary'.$eol
;
$data .= $eol;
$data .= $content . $eol;
}
$data .= "--" . $delimiter . "--".$eol;
return $data;
}
if($responsefile){
$data = json_encode(array(
"name" => "{$posted_data["Name"]}",
"lastName" => "{$posted_data["LastName"]}",
"phone" => "{$posted_data["Telephone"]}",
"message" => "{$posted_data["message"]} Skype: {$posted_data["Skype"]} Beachance: {$posted_data["Beachance"]}",
"vacancyId" => "{$posted_data["post-slug"]}",
"fileId" => "[{\"name\":\"{$posted_data["CV"]}\", \"attId\":\" {$responsefile}\"}]",
"email" => "{$posted_data["Email"]}",
"lang" => "ru"
));
$ch = curl_init("https://cleverstaff.net/hr/public/sendApplication");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
}
return;
}
}