Snippets

Burness Eventbrite to MailChimp RSVP Sync

Created by Eric Stroo
<?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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.