I have finalised the initial API Post request and it is working. It is not the leanest piece of code, but it is getting the job done:
<?php
return function($kirby, $site, $pages, $page) {
$alert = null;
if($kirby->request()->is('POST') && get('submit')) {
// check the honeypot
if(empty(get('website')) === false) {
go($page->url());
exit;
}
$data = [
'fname' => get('fname'),
'lname' => get('lname'),
'email' => get('email'),
'phone' => get('phone'),
'text' => get('text'),
'checkbox' => get('checkbox'),
'project' => get('project'),
'input_channel_id' => 8,
'source_id' => 2,
'interest_type_id' => 4
];
$rules = [
'fname' => ['required', 'minLength' => 3],
'lname' => ['required', 'minLength' => 3],
'email' => ['required', 'email'],
'phone' => ['required'],
'checkbox' => ['required']
];
$messages = [
'fname' => 'Este campo es requerido.',
'lname' => 'Este campo es requerido.',
'email' => 'Introduzca una dirección de correo electrónico válida.',
'phone' => 'Introduzca un número de telefono válido.',
'checkbox' => 'Este campo es requerido.'
];
// some of the data is invalid
if($invalid = invalid($data, $rules, $messages)) {
$alert = $invalid;
// the data is fine, let's send the email
} else {
try {
$kirby->email([
'template' => 'email',
'from' => 'no-reply@domain.com',
'replyTo' => $data['email'],
'to' => 'test@email.com',
'subject' => 'Mensaje recibido del formulario del sitio web',
'data' => [
'fname' => esc($data['fname']),
'lname' => esc($data['lname']),
'email' => esc($data['email']),
'phone' => esc($data['phone']),
'project' => esc($data['project']),
'text' => esc($data['text'])
]
]);
} catch (Exception $error) {
if(option('debug')):
$alert['error'] = 'No se pudo enviar el formulario: <strong>' . $error->getMessage() . '</strong>';
else:
$alert['error'] = 'No se pudo enviar el formulario. Inténtalo de nuevo.';
endif;
}
// no exception occured, let's send a success message
if (empty($alert) === true) {
$success = 'Gracias por su mensaje, nos pondremos en contacto con usted lo antes posible.';
$data = [];
$object= array(
'fname' => get('fname'),
'lname' => get('lname'),
'email' => get('email'),
'phone' => get('phone'),
'project_id' => get('project'),
'input_channel_id' => 8,
'source_id' => 2,
'interest_type_id' => 4,
'observation' => get('text')
);
$object = (object) array_filter((array) $object);
$result = json_encode($object);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.eterniasoft.com/v3/clients',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $result,
CURLOPT_HTTPHEADER => array(
'Authorization: XXXXXXXX',
'Content-Type: application/json'
),
));
// $response = curl_exec($curl);
curl_exec($curl);
curl_close($curl);
// echo $response;
}
}
}
return [
'alert' => $alert,
'data' => $data ?? false,
'success' => $success ?? false
];
};
I am beginning to start thinking about how to read the received 200 response to check whether ‘phone’ is different, and if so submit a PUT request with the new ‘phone’ data.
Would something like this below be the right direction?
if (!empty($response->content)) {
$newData = json_decode($response->content, true);
} ?>
<?php if($newData['phone'] == $data['phone']): ?>
// PUT REQUEST HERE WITH NEW PHONE NUMBER
<?php endif ?>