PHP example of adding multiple contacts to one list

Highlighted
Occasional Participant

PHP example of adding multiple contacts to one list

I've got a form for adding users by an administrator that have filled in a paper based form. The online form allows up to 50 contacts to be added in one time, by checking if each name/email field is completed, and if so adding it to the database through a for loop.


I want to now integrate constant contact into this, but it will only add 1 contact at a time, when I need it to add all completed. The basics of my code are:


include_once('cc_class.php');



for ($i=1; $i <=50; $i++) {



$ccContactOBJ = new CC_Contact();



$name = "name" . $i;

$email = "email" . $i;



$name = $_POST;

$email = $_POST;



if ($email != "" && $name != "") {



$query = sprintf("SELECT * FROM users WHERE email = '%s'", mysql_real_escape_string($email));

$result= mysql_query($query);

if (mysql_num_rows($result) == 0) {

$query = sprintf("INSERT INTO users VALUES ('', '%s', '%s', '1', '1')", mysql_real_escape_string($email), mysql_real_escape_string($name));

mysql_query($query);



$name = "name" . $i;

$email = "email" . $i;



$postFields = array();

$postFields = $_POST;

$postFields = $_POST;

$postFields = "";

$postFields = "";

$postFields = "";

$postFields = "";

$postFields = "";

$postFields = "";

$postFields = "";

$postFields = "";

// The Code is looking for a State Code For Example TX instead of Texas

$postFields = "";

$postFields = "";

$postFields = "";

$postFields = "";

$postFields = "";

$postFields = $_POST;

$contactXML = $ccContactOBJ->createContactXML(null,$postFields);



if (!$ccContactOBJ->addSubscriber($contactXML)) {

    $error = true;

} else {

    $error = false;

    $_POST = array();

}



if (isset($error)) {

       

        if ($error === true) {

            $class = "error";

            $message = $ccContactOBJ->lastError;

        } else {

            $class = "success";

            $message = "Contact ".$_POST." Added.";

        }



        echo '<div class="'.$class.'">';

        echo $message;

        echo '</div>';

    }



}


Can anyone help with this please? I'm sure there must be an easy way around it to add multiple.


Thanks, John

1 REPLY 1
Highlighted
Moderator

Hi John,   The easiest way to

Hi John,


 


The easiest way to add multiple contacts, especially batches of ~50 people like you're talking, is to use our Bulk Activities API.  This is not included in our sample code at this time, however it is very easy to implement.  This is a offline batch job task that will set up a bulk import job (up to 20k email addresses at a time) to be added/updated on a first come, first served queue basis.  Often times this is perfect for chunk adding like you're doing.


 


Unlike our REST APIs, there is no XML required for this API.  It is a URL Encoded Form submission and very easy to script out for batch adds like yours.  For more details, you can see the documentation here: http://developer.constantcontact.com/doc/activities


 


Outside of using the Bulk Activity, you would have to do ~50 single contact add/updates in a loop as there is no way using the REST API to bulk add.  The REST API is intended to be used for small volumes, generally under 15-20 at a time.  

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

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

Constant Contact Would Like To Hear From You!

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured