We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

Adding multiple contacts in the contact List

SOLVED
Go to solution
ChrisC248
Regular Participant

Adding multiple contacts in the contact List

I was just following steps given in the link

http://community.constantcontact.com/t5/Documentation/Creating-an-Add-Contacts-Remove-Contacts-Activ...

 

But these are not clear.

I tried to post the string through curl.

First thing I wanna to ask is what is SV_ADD

second, What is the these string meant for:

activityType=SV_ADD
&data=Email+Address%2CFirst+Name%2CLast+Name%0A
wstest3%40example.com%2C+Fred%2C+Test%0A
wstest4%40example.com%2C+Joan%2C+Test%0A
wstest5%40example.com%2C+Ann%2C+Test
&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2Fjoesflowers%2Flists%2F2
&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2Fjoesflowers%2Flists%2F5

 

Where i have to use this as it's document is not clear. If there is any other Documentation please let me know.

M trying to embed through php code.

1 ACCEPTED SOLUTION

Hi,

 

Yes, it would be possible to set this up with a cron script, but we don't have any specific examples of that.  Below is a PHP script that does a bulk contact add using acitivities--you will just need to add your authentication to it and specify the file path for your csv file and the list number you want the contacts added to.

 

<?PHP
set_time_limit(0);
require 'ConstantContact.php';

###Fill out this information!###
$listNumber = 1;
$FILENAME = "./temp/filename.csv";

// Authentication to Constant Contact

//You shouldn't have to edit anything below this line, unless you have a file with greater than 60,000 rows


// Set row value
$row = 1;

// Start the URI of which to pass to bulk activities
$BeforeEmailAddress = "activityType=SV_ADD&data=Email+Address";

// set value of totalContacts
$totalContacts = 0;

// Used for to pass in the column number for large CSV 
// You may want to remove this depending on your integration

$x = $_GET['x'];

if (($handle = fopen("$FILENAME", "r")) !== FALSE) {
	
	// itterate through rows
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    	// how many columns are in a row
        $num = count($data);
        
        // use echo for testing
        //echo "<p> $num fields in line $row: <br /></p>\n";

        $row++;
        // $c = 0 if your not using X;
        // data[0] is your first column, this needs to contain your email address
        for ($c=$x; $c < ($x+1); $c++) {
          echo "<b>$c</b><br>";
                // Checks for value of "x" as this meant that the contact
                // belongs to the current column list
	
        	if ($data[$c] == $x && $totalContacts < 20000) {
        		$emailAddress .= "\n" . $data[0];
        		$totalContacts++;
                // Bulk activities have a limit of 20k contacts you must have more than 20 contacts
                // to submit the contacts via bulk activity

                // if contacts are over 20k then you need to start a new activity
        	}else if($data[$c] == $x && $totalContacts >= 20000 && $totalContacts < 40000){
        		$emailAddress2 .= "\n" . $data[0];
        		$totalContacts++;
        	}else if($data[$c] == $x && $totalContacts >= 40000){
        		$emailAddress3 .= "\n" . $data[0];
        		$totalContacts++;
        	}

        }
        
    }

    $lv = $listNumber; 
    $list = "http://api.constantcontact.com/ws/customers/$username/lists/".$lv;
   
    fclose($handle);
}
// Echo's the string to verify that you are targeting the correct information to the correct list
echo $BeforeEmailAddress.urlencode($emailAddress)."&".urlencode($list);
 
        // Remove below echo statement to make this script active, always test as your contacts may go to the wrong list

	$ConstantContact->bulkAddContacts($BeforeEmailAddress.urlencode($emailAddress)."&lists=".urlencode($list));
	
        // Start second bulk load activity if more than 20k contacts existed.

	if(isset($emailAddress2))
	{
		$ConstantContact->bulkAddContacts($BeforeEmailAddress.urlencode($emailAddress2)."&lists=".urlencode($list));;
	}
	if(isset($emailAddress3))
	{
		$ConstantContact->bulkAddContacts($BeforeEmailAddress.urlencode($emailAddress3)."&lists=".urlencode($list));;
	}

// Tells you how many contacts were uploaded to which list id.
echo "<br>".$totalContacts . " Have been uploaded to " . $list;
?>

 

 

I hope that helps. Have a great day!

 

Best Regards,

Shannon W.

API Support Specialist

View solution in original post

24 REPLIES 24
Shannon_W
Employee

Hi,

 

Sorry you're not finding the documentation to be clear.

 

SV_ADD is the type of account activity you are creating in your account when you do the POST.  (I believe it was meant to stand for Site Visitor Add, as in adding contacts).

 

The string is the encoded data that you are sending to us through the API.  In the example below, you set the data columns to include email address, first name, and last name.  After that, you are putting in as many contact entries as you want, including their email, first and last name.  Lastly, you are selecting which contact lists in your account they are being added to (all contacts will be added to all lists included).

 

I hope that clarifies things!  Thank you for contacting Constant Contact, and have a wonderful day!

 

Best Regards,

Shannon W.

API Support Specialist

ChrisC248
Regular Participant

I don't want to submit form,

 

This is through cron, is it possible to implement through ph cron system.

Can i get any sample code? because there is not given any activity sample code. so can you help me out in this.

Hi,

 

Yes, it would be possible to set this up with a cron script, but we don't have any specific examples of that.  Below is a PHP script that does a bulk contact add using acitivities--you will just need to add your authentication to it and specify the file path for your csv file and the list number you want the contacts added to.

 

<?PHP
set_time_limit(0);
require 'ConstantContact.php';

###Fill out this information!###
$listNumber = 1;
$FILENAME = "./temp/filename.csv";

// Authentication to Constant Contact

//You shouldn't have to edit anything below this line, unless you have a file with greater than 60,000 rows


// Set row value
$row = 1;

// Start the URI of which to pass to bulk activities
$BeforeEmailAddress = "activityType=SV_ADD&data=Email+Address";

// set value of totalContacts
$totalContacts = 0;

// Used for to pass in the column number for large CSV 
// You may want to remove this depending on your integration

$x = $_GET['x'];

if (($handle = fopen("$FILENAME", "r")) !== FALSE) {
	
	// itterate through rows
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    	// how many columns are in a row
        $num = count($data);
        
        // use echo for testing
        //echo "<p> $num fields in line $row: <br /></p>\n";

        $row++;
        // $c = 0 if your not using X;
        // data[0] is your first column, this needs to contain your email address
        for ($c=$x; $c < ($x+1); $c++) {
          echo "<b>$c</b><br>";
                // Checks for value of "x" as this meant that the contact
                // belongs to the current column list
	
        	if ($data[$c] == $x && $totalContacts < 20000) {
        		$emailAddress .= "\n" . $data[0];
        		$totalContacts++;
                // Bulk activities have a limit of 20k contacts you must have more than 20 contacts
                // to submit the contacts via bulk activity

                // if contacts are over 20k then you need to start a new activity
        	}else if($data[$c] == $x && $totalContacts >= 20000 && $totalContacts < 40000){
        		$emailAddress2 .= "\n" . $data[0];
        		$totalContacts++;
        	}else if($data[$c] == $x && $totalContacts >= 40000){
        		$emailAddress3 .= "\n" . $data[0];
        		$totalContacts++;
        	}

        }
        
    }

    $lv = $listNumber; 
    $list = "http://api.constantcontact.com/ws/customers/$username/lists/".$lv;
   
    fclose($handle);
}
// Echo's the string to verify that you are targeting the correct information to the correct list
echo $BeforeEmailAddress.urlencode($emailAddress)."&".urlencode($list);
 
        // Remove below echo statement to make this script active, always test as your contacts may go to the wrong list

	$ConstantContact->bulkAddContacts($BeforeEmailAddress.urlencode($emailAddress)."&lists=".urlencode($list));
	
        // Start second bulk load activity if more than 20k contacts existed.

	if(isset($emailAddress2))
	{
		$ConstantContact->bulkAddContacts($BeforeEmailAddress.urlencode($emailAddress2)."&lists=".urlencode($list));;
	}
	if(isset($emailAddress3))
	{
		$ConstantContact->bulkAddContacts($BeforeEmailAddress.urlencode($emailAddress3)."&lists=".urlencode($list));;
	}

// Tells you how many contacts were uploaded to which list id.
echo "<br>".$totalContacts . " Have been uploaded to " . $list;
?>

 

 

I hope that helps. Have a great day!

 

Best Regards,

Shannon W.

API Support Specialist

View solution in original post

ChrisC248
Regular Participant

Hi,

 

After getting you reply I found that the class which m using is in cc_class.php. And the file which you included in ConstantContact.php. Is m using old class???

In my class I have to pass only username,password,secret key.

 

But with Constantcontact it ask for oauth. it redirects me to constant contact site ask me to login then after redirect me back to previous page. but I don't want to authenticate after getting onto your site. Is it possible.

 

And the class which you had given having large number of functions they will definetly help us out but now issue is with authenticate.

ChrisC248
Regular Participant

Hi,

 

Even after authentication it gives me this error.

 

Fatal error: Call to a member function xpath() on a non-object in E:\xampp\htdocs\constant\dti\campaign\wrapper\Components.php on line 1427

 

I didn't tweaked anything in core files. can you help me out what's this error due to????

Hi,

 

Could you replace the contents of your Components.php file with the code found on this page in our PHP Wrapper library?  I just want to make sure you have the most recent version.

 

Thanks!

Shannon

ChrisC248
Regular Participant

I updated the file but still same error.

at that line i am having same code.

 

public static function findNextLink($item){
$nextLink = $item->xpath("//*[@rel='next']");
return ($nextLink) ? (string) $nextLink[0]->Attributes()->href : false;
}

ChrisC248
Regular Participant

hi shannon,

 

I think upgradation work. but to send code for authentication is not good as I don't want to authanticate with login. So can you tell how can I modify the code of this class CTCTDataStore. according to me with this this class i can get it done right????

Hi,

 

That's good news.  You could technically use basic authentication, but I would recommend going through the OAuth 2 flow since we will be discontinuing support for basic authentication and applications using basic authentication will fail when that happens (in the next year or so).  With OAuth 2, you need to authenticate an account only once, then you get an access token that doesn't expire.  You can then use that access token in the future.

 

Here is an example of how to do Oauth 2 simply.

 

Best Regards,

Shannon W.

API Support Specialist

 

 

 

 

ChrisC248
Regular Participant

Hi shannon,

 

without "basic" it's not working for me.

So it's ok campaigns are showing with OAuth 2.

 

But if i try to add bulk contacts through my code. I followed the steps you mentioned but not able to insert data. It gives following error.

HTTP Status 401 - Invalid signature for signature method HMAC-SHA1

 

when it print $this->CTCTRequest in code it gives blank signature object.  like this

 

CTCTRequest Object
(
[baseUri] => https://api.constantcontact.com
[username] => ***Correct username***
[apiKey] => ***Correct key***
[authType] => oauth
[requestLogin] =>
[consumerSecret] =>
[consumer] => OAuthConsumer Object
(
[key] => ***Correct key***
[secret] => ***Correct secret***
)

[accessToken] =>
[signatureMethod] => OAuthSignatureMethod_HMAC_SHA1 Object
(
)

)

 

Can you help me, where I am missing code.

 

Code which I executed is:

 

the string which is showed in display is:

activityType=SV_ADD&data=Email+Address%0A+shiv.jangid%40directive.com&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2F{username}%2Flists%2F2

 

But it's not working correctly. hope you got my issue???

ChrisC248
Regular Participant

https://api.constantcontact.com/ws/customers/{username}/activities?oauth_consumer_key={key}&oauth_no...

 

this is the url which m getting. some values in the url are altered. but their syntex is same. this is what m getting while execution so where the issue. if this will help you.

And also $method=POST

Hi,

 

It looks like you have an extra "+" before the email address in the string you are sending to us.  Can you change the string to the string below (with your username put in) and see if that works?

 

activityType=SV_ADD&data=Email+Address%0Ashiv.jangid%40directive.com&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2F{username}%2Flists%2F2

 

Let me know if that works.

 

Best Regards,

Shannon W.

API Support Specialist

ChrisC248
Regular Participant

No it's not working.I already checked it.

Hi,

I can successfully POST the following URL to my account, replacing my username and posting to list 1 (since I deleted list 2):

 

activityType=SV_ADD&data=Email+Address%0Ashiv.jangid%40directive.com&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2Fusername%2Flists%2F1

 

I am using the details below for the header information. Content-type: application/x-www-form-urlencoded

 

What information are you trying to pass in using the signatureMethod object?

 

Best Regards,
Shannon W.
API Support Specialist

ChrisC248
Regular Participant

hi,

 

m not aware of signatureMethod object and how to pass this value and in which function we have to pass.

 

error m getting while updating bulk contacts is 

 

HTTP Status 401 - Invalid signature for signature method HMAC-SHA1

 

Why this error is there only needs how we can sort this out.

I am sending you code on webservices@constantcontact.com this email id.

 

 

Hi,

 

I am looking into what may be causing this issue--both your code and our wrapper, and I will respond to you via email when I have more details.  Thanks!

 

Best Regards,

Shannon W.

API Support Specialist

ChrisC248
Regular Participant

hi Shannon, 

 

Are you able to find out any solution for that???

if there any wrapper update or something please let me know. we need it urgently.

because there is no alternate method in these wrapper. other than we have to use cc_class methods

Hi,

 

I apologize that I haven't been able to address the issue more quickly, but I have been extremely busy over the last few workdays.  I will respond via email once I have more information for you--I will do that today if at all possible (it is high on my list of priorities).  If anyone can add anything here in the forums, please do.

 

Best Regards,

Shannon W.

API Support Specialist

ChrisC248
Regular Participant

Hi shannon,

 

Any luck because I am just stuck in the adding contacts in bulk. otherwise as soon as I am learning constancontact. your script are so much powerful and your concern to my problem is appreciative. Please help me out in this.

Hi,

 

Yes, you should have an email that I sent to you yesterday about this.  Let me know if you can't find it; I can try to look it up.

 

 

Best Regards,

Shannon W.

API Support Specialist

Developer Portal

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

Visit Page