Skip to content

Instantly share code, notes, and snippets.

@pavax
Last active July 5, 2023 21:03
Show Gist options
  • Save pavax/c692a788f38beee4ead6da6f597bbb65 to your computer and use it in GitHub Desktop.
Save pavax/c692a788f38beee4ead6da6f597bbb65 to your computer and use it in GitHub Desktop.
<?php
/**
* HOW-TO INSTALL
* - Download and edit the DEFINE-ME variables in this file
* - Upload the file to your server (for example into your WordPress installation directory)
* - Go to the Amelia Settings Page and click on Integrations.
* - Go to the "Webhooks" Tab
*
* URL: Provide your URL that matches to where you uploaded this php file on your server.
* E.g. https://my-domain.com/amelia_2_mailchimp_hook.php
*
* Type: "Event"
* I did not test it with another Type than Event
*
* Action: "Booking completed"
**/
// DEFINE-ME: YOUR MAILCHIMP API KEY
define('API_KEY', 'XXXXXXXXXXX');
// DEFINE-ME: MAILCHIMP SERVER (FOR EXAMPLE: https://us17.api.mailchimp.com)
define('SERVER', 'https://us17.api.mailchimp.com');
// DEFINE-ME: MAILCHIMP LIST ID
define('LIST_ID', '12345abcde');
// DEFINE-ME: CUSTOMER default Language
define('LANGUAGE', 'de');
$input = json_decode(file_get_contents('php://input'));
$firstName = $input->bookings->{'0'}->customer->firstName;
$lastName = $input->bookings->{'0'}->customer->lastName;
$email = strtolower($input->bookings->{'0'}->customer->email);
$phone = $input->bookings->{'0'}->customer->phone;
// EXAMPLE ON HOW TO READ A CUSTOM FIELD WHERE THE USER CAN SELECT WHETER TO SUBSCRIBE OR NOT
//$newsletter = strtolower($input->bookings->{'0'}->customFields->{'7'}->value);
//if (strpos($newsletter, "yes") === false) {
// echo 'Noop';
// return;
//}
$postData = array(
"email_address" => $email,
"email_type" => "html",
"status_if_new"=>"subscribed",
"tags"=> array('amelia_booking'),
"language" => LANGUAGE,
"merge_fields" => array(
"FNAME" => "$firstName",
"LNAME" => "$lastName"
)
);
$ch = curl_init(SERVER.'/3.0/lists/'.LIST_ID.'/members'.'/'.md5($email));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer '.API_KEY
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($postData));
$response = curl_exec($ch);
curl_close($ch);
echo 'Done';
//var_dump($response);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment