Reply
New Member
vshalamov
Posts: 2
Registered: ‎01-19-2011

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.

CTCT Employee
David_J
Posts: 511
Registered: ‎06-04-2009

 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

New Member
vshalamov
Posts: 2
Registered: ‎01-19-2011

that worked. Thanks a lot for

that worked. Thanks a lot for your help.


 

Frequent Visitor
JuliaZ
Posts: 2
Registered: ‎07-28-2011

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.

Ryan_D
Posts: 453
Topics: 0
Kudos: 20
Solutions: 21
Registered: ‎06-28-2011

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
Support Engineer, Constant Contact