PHP - add new contact

Highlighted
New Member

PHP - add new contact

Hi All


I'm trying to create a new contact - using a sample provided on the site (http://sourceforge.net/projects/ctctsampleforms/)


Here is my code:


<?php


include_once('cc_class.php');

$ccContactOBJ = new CC_Contact();

$ccListOBJ = new CC_List();


if (isset($_POST))

{

 $postFields = array();

 $postFields = $_POST;

 $postFields = array(6);


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


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

  echo $ccContactOBJ->lastError;

 } else {

  echo "Added";

 }

}

else

{

?>


<form action="" method="post">

<input type="text" name="email_address">

<input type="submit" name="submit" value="submit">

</form>

<?php } ?>


When submit the form it comes back with message "An Error Occurred" - no more info about nature of the error.

I checked contact XML that gets sent to CC server - it seems to be ok and formed correctly. Login info is entered correctly as well - API key, u/p.



Any ideas what might be wrong? Any help will be much appreciated.

Vlad.

4 REPLIES 4
Highlighted
CTCT Employee

 I believe the reason you are

 I believe the reason you are seeing 'An Error Occurred’ is because you likely have $curl_debug set to false in cc_class.php line 29. If you set this to true it should return some more information about why this is failing to post.




I only needed to make one modification for this to create a new contact in my account, which was to change:

$postFields = array(6);




To:

$postFields = array('https://api.constantcontact.com/ws/customers/{username}/lists/6');




As the ‘lists’ needs to be an array of full list ID’s as opposed to just the actual id numbers. I believe the code below will work for you.


I hope this helps, please let me know if you have any other questions regarding this. Thanks.

 


<?php

include_once('cc_class.php');

$ccContactOBJ = new CC_Contact();

$ccListOBJ = new CC_List();

if (isset($_POST))

{

 $postFields = array();

 $postFields = $_POST;

 $postFields = array('https://api.constantcontact.com/ws/customers/{username}/lists/6');

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

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

  echo $ccContactOBJ->lastError;

 } else {

  echo "Added";

 }

}

else

{

?>

<form action="" method="post">

<input type="text" name="email_address">

<input type="submit" name="submit" value="submit">

</form>

<?php } ?>

 

 

David J

Highlighted
Frequent Visitor

Re:  I believe the reason you are

I'm extremely confused here. I've been trying to work though this for a while, and I can't figure it out. Firstly, why would you write these statements?

 

$postFields = array();

 $postFields = $_POST;

 $postFields = array('https://api.constantcontact.com/ws/customers/{username}/lists/6');

 

They appear to simply overwrite each other. When I print_r the  $postFields I only get the last value.

Highlighted
CTCT Employee

Re:  I believe the reason you are

Hey Julia,


I will look into this for you as I believe the idea was to have them append merge but that doesn't seem to be the case programmatically.  I understand that syntax flaw but which area are you having trouble with?

Ryan Davis
Quality Engineer
Highlighted
New Member

that worked. Thanks a lot for

that worked. Thanks a lot for your help.


 

Developer Portal

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

Visit Page

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