nouveau-theatre-de-besancon/site/config/routes/brevo-create-contact.php
2024-09-04 18:30:33 +02:00

45 lines
1.7 KiB
PHP

<?php
require_once(__DIR__ . '/../../../vendor/autoload.php');
return [
'pattern' => '/brevo-create-contact.json',
'method' => 'POST',
'action' => function () {
$jsonRequest = file_get_contents("php://input");
$request = json_decode($jsonRequest, true);
$email = $request['email'];
$apiKey = site()->brevoApiKey()->value();
// Configure API key authorization: api-key
$config = Brevo\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', $apiKey);
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = Brevo\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('api-key', 'Bearer');
// Configure API key authorization: partner-key
$config = Brevo\Client\Configuration::getDefaultConfiguration()->setApiKey('partner-key', $apiKey);
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = Brevo\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('partner-key', 'Bearer');
$apiInstance = new Brevo\Client\Api\ContactsApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
$config
);
$createContact = new \Brevo\Client\Model\CreateContact([
'email' => $email,
'updateEnabled' => true,
]);
try {
$result = $apiInstance->createContact($createContact);
return json_encode($email . ' correctement ajouté à Brevo.');
} catch (Exception $e) {
return json_encode('Exception when calling ContactsApi->createContact: ' . $e->getMessage());
}
}
];