Snippets
Created by
Eric Stroo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | <?php
/*
To use this code, pass your personalOAuth code and event ID from Eventbrite and your MailChimp API key (password), list id, and the ids of the two interest groups you wish to populate. For help getting any of these ids, refer to the relevant docs.
For large lists, you may need to be put in a time offset into the cURLs to escape rate limitng.
*/
//API Parameters for Eventbrite
$personalOAuth = '';
$eventID ='';
//API Parameters for MailChimp
$password= '';
$list_id = '';
$rsvpGroupID = '';
$noRSVPGroupID = '';
//Make the call to EventBrite, evalue the array and determine if another call is needed
$eventbriteArray = array();
$page = 1;
//Start a loop of executing code to build the array of Eventbrite attendees
do{
//Paramaters for the EventBrite API. $eventbrite is the root, $method is the API method, $event_id is the event id, $submethod is the specfic endpoint, $filter is the url filter, $personal_OAuth is the token.
$eventbriteRoot = 'https://www.eventbriteapi.com/v3/';
$method = 'events';
$subMethod = 'attendees';
$filter = 'status=attending';
$url = $eventbriteRoot.$method.'/'.$eventID.'/'.$subMethod.'/?'.$filter.'&token='.$personalOAuth.'&page='.$page.'&format=json';
//Using the Event Attendees Method, send a cURL to GET a list of the {profile{email}}
//Start a new instance of cURL
$curl = curl_init();
//cURL parameters
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
//Timeout
set_time_limit(300);
//Execute the cURL
$eventbriteResult = curl_exec($curl);
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
//Handle the result
$eventbriteJSON=json_decode($eventbriteResult,true);
//Check on the total number of pages
$totPages = $eventbriteJSON['pagination']['page_count'];
//Build the $eventbriteArray
foreach($eventbriteJSON['attendees'] as $key => $person) {
$eventbriteArrayForMailchimp = array(
'email_address' => strtolower($person['profile']['email']),
'merge_fields' => array(
'FNAME' => $person['profile']['first_name'],
'LNAME' => $person['profile']['last_name']
)
);
array_push($eventbriteArray,$eventbriteArrayForMailchimp);
};
//Increment offset
$page = $page+1;
}
//End the loop after if there are no more objects
while (count($page)>$totPages);
//Close request
curl_close($curl);
//Make the call to MailChimp, evaluate the array and determine if another API call is needed.
$mailchimpArray = array();
$offset = 0;
//Start a loop of executing code to build the array of MailChimp users
do {
//Using the List Members Method, send a cURL to GET a list of the {members{email_address}}
//cURL params
$username= 'astring';
$root_url = 'https://us5.api.mailchimp.com/3.0';
$resource = '/lists/'.$list_id.'/members';
$url = $root_url.$resource.'?count=100&offset='.$offset;
//Open a new instance of cURL
$curl = curl_init();
//cURL options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $username. ":".$password);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: apikey '.$password));
//Timeout
set_time_limit(300);
//Execute the cURL
$result = curl_exec($curl);
//echo $url;
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
//Handle the result
$mailchimpJSON=json_decode($result,true);
//Build the array
foreach ($mailchimpJSON['members'] as $key => $mailchimpPerson) {
$mailchimpArray[$key]['id'] = $mailchimpPerson['id'];
$mailchimpArray[$key]['email_address'] = $mailchimpPerson['email_address'];
};
//Increment offset
$offset = $offset+100;
}
//End the loop after if there are no more objects
while (count($mailchimpJSON['members'])==100);
//Close request
curl_close($curl);
//Find the difference
//Build a list of EB Emails
$eventbriteEmailArray = array();
foreach($eventbriteArray as $eventbriteperson){
array_push($eventbriteEmailArray,$eventbriteperson['email_address']);
};
//Build a list of MC Emails
$mailchimpEmailArray = array();
foreach($mailchimpArray as $mailchimpEmailAddres) {
array_push($mailchimpEmailArray, $mailchimpEmailAddres['email_address']);
}
//Set the MC Interest Group Array for the RSVPs
$rsvpArray = array (
$rsvpGroupID => true,
$noRSVPGroupID => false
);
//Set the MC Interest Group Array for the Non RSVPs
$noRSVPArray = array(
$rsvpGroupID => false,
$noRSVPGroupID => true
);
//Find the Non RSVPs in the MC Email Array:
$noRSVPs = array_diff($mailchimpEmailArray, $eventbriteEmailArray);
//Build the array of non RSVPs and combine it with the MC ID
$noRSVPUpdate = array();
$updatePeopleIndex = 0;
foreach($noRSVPs as $key => $noRSVPPerson) {
$noRSVPUpdate[$updatePeopleIndex]['id'] = $mailchimpArray[$key]['id'];
$noRSVPUpdate[$updatePeopleIndex]['email_address'] = $noRSVPPerson;
$noRSVPUpdate[$updatePeopleIndex]['interests'] = $noRSVPArray;
$updatePeopleIndex++;
}
//RSVP and in MC Array without interests:
$rsvpsLookUp = array_intersect($mailchimpEmailArray,$eventbriteEmailArray);
//Build the RSVP Array, combine it with the MC ID and the EB merge fields
$updatePeople = array();
$updatePeopleIndex = 0;
foreach($rsvpsLookUp as $key => $updatePerson){
$updatePeople[$updatePeopleIndex]['id'] = $mailchimpArray[$key]['id'];
$updatePeople[$updatePeopleIndex]['email_address'] = $updatePerson;
foreach($eventbriteArray as $key=>$eventbritePerson){
if($eventbritePerson['email_address'] == $updatePerson) {
$updatePeople[$updatePeopleIndex]['merge_fields']=$eventbritePerson['merge_fields'];
}
}
$updatePeople[$updatePeopleIndex]['interests'] = $rsvpArray;
$updatePeopleIndex++;
}
//Update the RSVP Group in MC
//Using the List Members Method, send a cURL to PATCH a list of the {members}
foreach($updatePeople as $key=>$rsvpUpdatePerson) {
//cURL params
$id = $rsvpUpdatePerson['id'];
$username= 'astring';
$root_url = 'https://us5.api.mailchimp.com/3.0';
$resource = '/lists/'.$list_id.'/members/';
$url = $root_url.$resource.$id;
$post_fields = json_encode($rsvpUpdatePerson);
//Open a new instance of cURL
$curl = curl_init();
//cURL options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $username. ":".$password);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json-patch+json', 'Authorization: apikey '.$password));
//Timeout
set_time_limit(300);
//Execute the cURL
$result = curl_exec($curl);
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
}
//Close request
curl_close($curl);
//Update the Non-RSVP Group in MC
foreach($noRSVPUpdate as $key=>$NoRSVPUpdatePerson) {
//cURL params
$id = $NoRSVPUpdatePerson['id'];
$username= 'astring';
$root_url = 'https://us5.api.mailchimp.com/3.0';
$resource = '/lists/'.$list_id.'/members/';
$url = $root_url.$resource.$id;
$post_fields = json_encode($NoRSVPUpdatePerson);
//Open a new instance of cURL
$curl = curl_init();
//cURL options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $username. ":".$password);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json-patch+json', 'Authorization: apikey '.$password));
//Timeout
set_time_limit(300);
//Execute the cURL
$result = curl_exec($curl);
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
}
//Close request
curl_close($curl);
?>
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.