Discussions
Upload documents and attach to applications
Hello,
I have a Wordpress install with an application form, built with Gravity Forms. I'm using Guzzle to send POST requests to the recruiting API.
All form field values coming in nicely and I can see the applications in the Personio Backend. Except for the uploaded files. I just can't get it to go. I can confirm that the files are getting uploaded to my testserver but are not getting processed further ending in empty files array.
I tried with and without SSL verification but no luck. I didn't find anything helpful in the docs or the discussion board. So if anyone has an idea or actually got it to work with Gravity Forms ... it will be greatly appreciated.
Here are my two functions:
require_once ABSPATH . 'vendor/autoload.php';
function upload_files_to_personio_from_url($file_url, $api_key, $company_id, $category) {
if (empty($file_url)) {
error_log('File URL is empty.');
return false;
}
$client = new \GuzzleHttp\Client(['verify' => false]);
$upload_url = 'https://api.personio.de/v1/recruiting/applications/documents';
try {
$response = $client->request('GET', $file_url);
$file_content = $response->getBody()->getContents();
$response = $client->request('POST', $upload_url, [
'multipart' => [
[
'name' => 'file',
'filename' => basename($file_url),
'contents' => fopen('data://text/plain;base64,' . base64_encode($file_content), 'r'),
],
],
'headers' => [
'X-Company-ID' => $company_id,
'Authorization' => 'Bearer ' . $api_key,
],
]);
$data = json_decode($response->getBody(), true);
$uuid = $data['uuid'];
return ['uuid' => $uuid];
} catch (\Exception $e) {
// Handle error
error_log('File upload error: ' . $e->getMessage());
return false;
}
}
add_action('gform_after_submission', 'post_to_personio_api', 10, 2);
function post_to_personio_api($entry, $form) {
global $api_key, $company_id;
$company_id = 'XXXXXX';
$api_key = 'XXXXXXXXXX';
error_log('Form entry data: ' . print_r($entry, true));
$file_url_cv = isset($entry[14]) ? $entry[14] : null;
if (!empty($file_url_cv)) {
$cv_upload_response = upload_files_to_personio_from_url($file_url_cv, $api_key, $company_id, 'cv');
// Check if the file upload was successful
if ($cv_upload_response !== false) {
// Prepare data array for CV file
$cv_uuid = $cv_upload_response['uuid'];
$cv_data = [
'uuid' => $cv_uuid,
'original_filename' => 'cv_document.pdf',
'category' => 'cv',
];
// Add CV data to $data['files'] array
$data['files'][] = $cv_data;
}
} else {
error_log('CV file URL is empty.');
}
// Initialize $data array
$data = [
'job_position_id' => rgar($entry, '4'),
'first_name' => rgar($entry, '1'),
'last_name' => rgar($entry, '3'),
'email' => rgar($entry, '11'),
'files' => [], // Initialize the files array
'attributes' => [
// ... (attributes)
],
];
// Check if file details are present
if (!empty($file_details_cv) && is_array($file_details_cv)) {
// Existing code for uploading files to Personio
$cv_upload_response = upload_files_to_personio($file_details_cv, $api_key, $company_id, 'cv');
// Check if the file upload was successful
if ($cv_upload_response !== false) {
// Prepare data array for CV file
$cv_uuid = $cv_upload_response['uuid'];
$cv_data = [
'uuid' => $cv_uuid,
'original_filename' => 'cv_document.pdf', // Adjust the filename as needed
'category' => 'cv',
];
// Add CV data to $data['files'] array
$data['files'][] = $cv_data;
}
} else {
error_log('CV file details are empty.');
}
if (!empty($file_details_cv) && is_array($file_details_cv)) {
// Existing code for uploading files to Personio
$cv_upload_response = upload_files_to_personio($file_details_cv, $api_key, $company_id, 'cv');
// Check if the file upload was successful
if ($cv_upload_response !== false) {
// Prepare data array for CV file
$cv_uuid = $cv_upload_response['uuid'];
$cv_data = [
'uuid' => $cv_uuid,
'original_filename' => 'cv_document.pdf', // Adjust the filename as needed
'category' => 'cv',
];
// Add CV data to $data['files'] array
$data['files'][] = $cv_data;
}
} else {
error_log('CV file details are empty.');
}
$client = new \GuzzleHttp\Client();
try {
$response = $client->post('https://api.personio.de/v1/recruiting/applications', [
'json' => $data,
'headers' => [
'X-Company-ID' => $company_id,
'accept' => 'application/json',
'authorization' => 'Bearer ' . $api_key,
'content-type' => 'application/json',
],
]);
// Handle API response
$api_response = json_decode($response->getBody(), true);
error_log(print_r($api_response, true));
} catch (\GuzzleHttp\Exception\ClientException $e) {
// Handle Guzzle client exception
error_log($e->getMessage());
error_log($e->getResponse()->getBody()->getContents());
}
}
Here's my error log:
[26-Jan-2024 12:37:58 UTC] Form entry data: Array
(
[id] => 129
[status] => active
[form_id] => 1
[ip] => 127.0.0.1
[source_url] => https://xxxx.xx/personio_stellen/social-media-werkstudent/
[currency] => EUR
[post_id] =>
[date_created] => 2024-01-26 12:37:28
[date_updated] => 2024-01-26 12:37:28
[is_starred] => 0
[is_read] => 0
[payment_status] =>
[payment_date] =>
[payment_amount] =>
[payment_method] =>
[transaction_id] =>
[is_fulfilled] =>
[created_by] => 1
[transaction_type] =>
[19] =>
[1] => John
[3] => Doe
[5] =>
[6] =>
[20] =>
[21] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] => [email protected]
[12] => Bitte auswählen
[13] => Bitte auswählen
[14] => https://xxxx.xx/wp-content/uploads/gravity_forms/personio/uploadfiles/vita_test.pdf
[17] =>
[4] => 1398765
[18.1] =>
[18.2] =>
[18.3] =>
)
[26-Jan-2024 12:37:58 UTC] CV file details are empty.
[26-Jan-2024 12:37:58 UTC] CV file details are empty.
[26-Jan-2024 12:37:59 UTC]
Kind regards,
Tobi