The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

Using Bulk Add Contacts with JSON Example

Highlighted
Established Member

Using Bulk Add Contacts with JSON Example

I have been trying to figure out how to use the bulk activities add contacts api for quite sometime now and just recently figured out how to actually do it correctly. I could find very little help with what I was doing, so I figure it might help someone with a similar problem as mine to have an example of how to use with JSON, PHP, and curl http://developer.constantcontact.com/docs/bulk_activities_api/bulk-activities-add-contacts.html with a JSON POST script.

 

 

$user = '{
   "import_data":[
      {
         "email_addresses":[
            "user31@example.com"
         ]
      },
      {
         "email_addresses":[
            "user32@example.com"
         ]
      }
   ],
   "lists":[
      "9"
   ],
   "column_names":[
      "EMAIL"
   ]
}';
$access_token = 'ACCESS TOKEN';
    $url = 'https://api.constantcontact.com/v2/activities/addcontacts?api_key=API KEY';
    // Start curl.
    $ch = curl_init();
    // Set query data here with the URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_HTTPHEADER,array('Authorization:Bearer ACCESS TOKEN', 'Content-Type:application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, '30');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $user);
    $content = curl_exec($ch);
    print_r($content);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    
    curl_close($ch);

The formatting of the JSON is very particular so I suggest you run it through http://jsonlint.com/ to be sure you are passing in a valid JSON string.

 

Hopefully this example saves someone some time.

4 REPLIES 4
Highlighted
Moderator

Re: Using Bulk Add Contacts with JSON Example

Thanks for the code snippet post, I'm sure someone will be very greatful for this!  Always great to see the community contributing back like this.  Let us know if you run into anything else in the future, we're here to help if you have questions or run into a roadblock.

 

On another note, we have a PHP wrapper update coming soon that will include all the different options for bulk import, include the different file types and JSON.  You can find this wrapper and all updates here: https://github.com/constantcontact/php-sdk

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Developer

Re: Using Bulk Add Contacts with JSON Example

Hi

 

I'm trying to Bulk Add Contacts but the problem i have is that the lists the various contacts are subscribing to varies.

In other words not everyone is being added to the same lists. The sample code seems to imply that all contacts being added in bulk are for the same lists.

 

Is there anyway this can be handled in one Bulk Add or would it need to be split according to the lists they should all be added to? I'm trying to avoid having to create a large number of requests as i don't want to have problems with the 4 requests per second limitation.

 

Thanks in advance

Enrique

Highlighted
Honored Contributor

Re: Using Bulk Add Contacts with JSON Example

Hi Enrique,

 

With Bulk Imports all of the contacts that are included in the import will be assigned to all lists that are included in the import. Since these imports are treated as an add+update operation, they will only add and update information that is present in the import and will not remove/delete any information from the contacts, including list memberships.

 

Given the above, it might be most effective for your situation to create one import per list that needs to have contacts added. For this you would group all of the contacts that will be added to list A, and create an import with those contacts that specified list A. Then you would do the same for List B, List C, and so on. Once these imports have completed, then any contacts who were part of multiple imports will have the desired list memberships. This would limit the number of requests that need to be made (which should make it easy to remain under 4 calls/second) while still providing a relatively direct way to get all of the contacts uploaded.

 

If you have any questions, please let us know!

 

Sincerely,

Elijah G.
API Support Engineer
Highlighted
Developer

Re: Using Bulk Add Contacts with JSON Example

Hi Elijah

 

Thanks for your reply - really appreciate it. Ok, what you suggest is what i feared you would say :-)

 

Ok, i will proceed with this approach and let you know if i run into problems.

 

Thanks

e

Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured