cancel
Showing results for 
Search instead for 
Did you mean: 

two issues with php v2 api contact form

Occasional Contributor

two issues with php v2 api contact form

I am getting an error message which I don't understand:

 

Array
(
    [0] => Array
        (
            [error_key] => http.status.conflict
            [error_message] => There was a conflict between the supplied data and the existing resource.
        )

)

my form is pretty simple - just want to capture email address, subscirbe to a single list and  the second issue I have is I want to add the data for customfield1

 

here is my form:

 

<!DOCTYPE HTML>
<html>
<head>
    <title>Constant Contact API v2 Add/Update Contact Example</title>
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
    <link href="css/styles.css" rel="stylesheet">
</head> 

<?php
require_once 'src/Ctct/autoload.php';

use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\Contacts\EmailAddress;
use Ctct\Exceptions\CtctException;

define("APIKEY", "--REDACTED--");
define("ACCESS_TOKEN", "--REDACTED--");

$cc = new ConstantContact(APIKEY);

// attempt to fetch lists in the account, catching any exceptions and printing the errors to screen
try{
    $lists = $cc->getLists(ACCESS_TOKEN);
} catch (CtctException $ex) {
    foreach ($ex->getErrors() as $error) {
        print_r($error);
    }     
}

// check if the form was submitted
if (isset($_POST['email']) && strlen($_POST['email']) > 1) {
    $action = "Getting Contact By Email Address";
    try {
        // check to see if a contact with the email addess already exists in the account
        $response = $cc->getContactByEmail(ACCESS_TOKEN, $_POST['email']);

        // create a new contact if one does not exist
        if (empty($response->results)) {
            $action = "Creating Contact";

            $contact = new Contact();
            $contact->addEmail($_POST['email']);
            $contact->addList($_POST['list']);
            $contact->first_name = $_POST['first_name'];
            $contact->last_name = $_POST['last_name'];
            $returnContact = $cc->addContact(ACCESS_TOKEN, $contact); 

        // update the existing contact if address already existed
        } else {            
            $action = "Updating Contact";

            $contact = $response->results[0];
            $contact->addList($_POST['list']);
            $contact->first_name = $_POST['first_name'];
            $contact->last_name = $_POST['last_name'];
            $returnContact = $cc->updateContact(ACCESS_TOKEN, $contact);  
        }
        
    // catch any exceptions thrown during the process and print the errors to screen
    } catch (CtctException $ex) {
        echo '<span class="label label-important">Error '.$action.'</span>';
        echo '<div class="container alert-error"><pre class="failure-pre">';
        print_r($ex->getErrors()); 
        echo '</pre></div>';
        die();
    }
} 
?>

<body>
<div class="well">
    <h3>Add or Update a Contact</h3>


    <form class="form-horizontal" name="submitContact" id="submitContact" method="POST" action="dp.cc.subscribe.php">
        <div class="control-group">
            <label class="control-label" for="email">Email</label>


		   <div class="controls">
                <input type="email" id="email" name="email" placeholder="Email Address">
            </div>
        </div>
        
        <div class="control-group">
            <label class="control-label" for="list">List</label>


          <div class="controls">
                <select name="list">
	                <option value="1">delanceyplace.com</option>
                    <?php
                 //   foreach ($lists as $list) {
                 //       echo '<option value="' . $list->id . '">' . $list->name . '</option>';
                 //   }
                    ?>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">
                <div class="controls">
                    <input type="submit" value="Submit" class="btn btn-primary"/>
                </div>
            </label>    
        </div>
    </form>
</div>


<!-- Success Message -->
<?php if (isset($returnContact)) {
    echo '<div class="container alert-success"><pre class="success-pre">';
    print_r($returnContact);
    echo '</pre></div>';
} ?>


</body>
</html>

 

 

 

THanks for your help!

 

Craig

 

 

 

 

 

5 REPLIES 5
Honored Contributor

Re: two issues with php v2 api contact form

Hi Craig,

 

I have been looking into this, and based on the error message I think it is most likely that there is an issue with some of the data being generated being invalid for a specific contact that is being added through this form.

 

Is it possible for you to email webservices@constantcontact.com with some details about what contact is having problems and what information you are inputting on your form to re-create the issue? It would also be very helpful if you can provide the URL for the form as well.

 

Best Regards,

Elijah G.
API Support Engineer
Occasional Participant

Re: two issues with php v2 api contact form

I have the same problem, is it fixed now?

Are we able to add or update contact with addList being able to create a list?


@CraigH20464 wrote:

I am getting an error message which I don't understand:

 

Array
(
    [0] => Array
        (
            [error_key] => http.status.conflict
            [error_message] => There was a conflict between the supplied data and the existing resource.
        )

)

my form is pretty simple - just want to capture email address, subscirbe to a single list and  the second issue I have is I want to add the data for customfield1

 

here is my form:

 

<!DOCTYPE HTML>
<html>
<head>
    <title>Constant Contact API v2 Add/Update Contact Example</title>
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
    <link href="css/styles.css" rel="stylesheet">
</head> 

<?php
require_once 'src/Ctct/autoload.php';

use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\Contacts\EmailAddress;
use Ctct\Exceptions\CtctException;

define("APIKEY", "--REDACTED--");
define("ACCESS_TOKEN", "--REDACTED--");

$cc = new ConstantContact(APIKEY);

// attempt to fetch lists in the account, catching any exceptions and printing the errors to screen
try{
    $lists = $cc->getLists(ACCESS_TOKEN);
} catch (CtctException $ex) {
    foreach ($ex->getErrors() as $error) {
        print_r($error);
    }     
}

// check if the form was submitted
if (isset($_POST['email']) && strlen($_POST['email']) > 1) {
    $action = "Getting Contact By Email Address";
    try {
        // check to see if a contact with the email addess already exists in the account
        $response = $cc->getContactByEmail(ACCESS_TOKEN, $_POST['email']);

        // create a new contact if one does not exist
        if (empty($response->results)) {
            $action = "Creating Contact";

            $contact = new Contact();
            $contact->addEmail($_POST['email']);
            $contact->addList($_POST['list']);
            $contact->first_name = $_POST['first_name'];
            $contact->last_name = $_POST['last_name'];
            $returnContact = $cc->addContact(ACCESS_TOKEN, $contact); 

        // update the existing contact if address already existed
        } else {            
            $action = "Updating Contact";

            $contact = $response->results[0];
            $contact->addList($_POST['list']);
            $contact->first_name = $_POST['first_name'];
            $contact->last_name = $_POST['last_name'];
            $returnContact = $cc->updateContact(ACCESS_TOKEN, $contact);  
        }
        
    // catch any exceptions thrown during the process and print the errors to screen
    } catch (CtctException $ex) {
        echo '<span class="label label-important">Error '.$action.'</span>';
        echo '<div class="container alert-error"><pre class="failure-pre">';
        print_r($ex->getErrors()); 
        echo '</pre></div>';
        die();
    }
} 
?>

<body>
<div class="well">
    <h3>Add or Update a Contact</h3>


    <form class="form-horizontal" name="submitContact" id="submitContact" method="POST" action="dp.cc.subscribe.php">
        <div class="control-group">
            <label class="control-label" for="email">Email</label>


		   <div class="controls">
                <input type="email" id="email" name="email" placeholder="Email Address">
            </div>
        </div>
        
        <div class="control-group">
            <label class="control-label" for="list">List</label>


          <div class="controls">
                <select name="list">
	                <option value="1">delanceyplace.com</option>
                    <?php
                 //   foreach ($lists as $list) {
                 //       echo '<option value="' . $list->id . '">' . $list->name . '</option>';
                 //   }
                    ?>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">
                <div class="controls">
                    <input type="submit" value="Submit" class="btn btn-primary"/>
                </div>
            </label>    
        </div>
    </form>
</div>


<!-- Success Message -->
<?php if (isset($returnContact)) {
    echo '<div class="container alert-success"><pre class="success-pre">';
    print_r($returnContact);
    echo '</pre></div>';
} ?>


</body>
</html>

 

 

 

THanks for your help!

 

Craig

 

 

 

 

 


 

CTCT Employee

Re: two issues with php v2 api contact form

Hi Craig,

 

I put example code in my response to your other forum post on how to create a new list and add a contact to it. The code you referenced here will only add an existing list to the contact (whether they're a new or updated contact).

 

We're going to need to see the specific email address getting the "There was a conflict between the supplied data and the existing resource" message, along with your username to help on that issue. Email us at webservices@constantcontact.com with your username and the email address so I can troubleshoot. 

 

Adding custom fields to a contact should look like this:

 

$contact->custom_fields = array( 
array('label' => 'CustomField1', 'name' => 'CustomField1', 'value' => 'value 1'), 
array('label' => 'CustomField2', 'name' => 'CustomField2', 'value' => 'value 2') 
); 

 

 

Best Regards,

Shannon Wallace

Partner API Support Engineer

Occasional Participant

Re: two issues with php v2 api contact form

public function AddOrUpdateAContact($list_name , $objects ){
echo "<PRE>"; print_r("creating a new list by name ". $list_name . " objects->list has " .$objects->list ); echo "</PRE>";
$lists = $this->ConstantContact->getLists();
// Lists are returned in multidimentional arrays 0 being the list, and 1 being the next50
foreach ($lists as $list) {
echo "<PRE>"; echo  $list->id . '">' . $list->name ; echo "</PRE>";
}
//Make a new list
$listObj = new ContactList();
$listObj->name = $list_name;
$newList = $this->ConstantContact->addList($listObj);
 
 
$lists =  $this->ConstantContact->getLists();
 
foreach ($lists as $list) {
echo "<PRE>"; echo  $list->id . '">' . $list->name ; echo "</PRE>";
}
// Get potential contact lists
$lists =  $this->ConstantContact->getLists();
// Lists are returned in multidimentional arrays 0 being the list, and 1 being the next50
 
//Insert searching logic to get the newly created list
 
// Email address here is used for testing purposes
$emailAddress = "testemail11192012@test.com";
 
// Search for our new Email address
$search = $this->ConstantContact->searchContactsByEmail($objects->email);
 
// If the search didn't return a contact object
if($search == false)
{
// Create a new Contact Object to store data into
$contactObj = new Contact();
// Adding multiple lists to this new Contact Object
$contactObj->lists = array($lists['lists'][0]->id, $lists['lists'][1]->id);
// Set the email address
$contactObj->emailAddress = $objects->email;
// Create the Contact and DONE
$Contact = $this->ConstantContact->addContact($contactObj);
 
echo "<PRE>"; echo ("Contact added.") ; echo "</PRE>";
 
 
} // Otherwise we update our existing contact
else
{
// Gather data from our previous search and store it into a data type
$contactObj = $this->ConstantContact->getContactDetails($search[0]);
 
// We need to get the old list and add a new list to it as
// this request requires a PUT and will remove the lists
// as they are stored in an array
array_push($contactObj->lists, $lists['lists'][1]->id );
 
// Update the contact and DONE
$UpdateContact = $this->ConstantContact->updateContact($contactObj);
echo "<PRE>"; echo ("Contact updated."); echo "</PRE>";
 
 
}
}

 

 

throws me an error with 

 

Error Creating Contact

Array

(

    [0] => Array

        (

            [error_key] => json.field.lists.value.invalid

            [error_message] => The contact list General Interest does not exist.

        )

 

)

 

Honored Contributor

Re: two issues with php v2 api contact form

Hello,

 

In this case, it looks like your lists are not getting assigned properly, which is leading to the trouble that you're having. This can be resolved by changing how you are assigning the lists to a Contact object.

 

In your code right now you have the following:

$contactObj->lists = array($lists['lists'][0]->id, $lists['lists'][1]->id);

Using the SDK, you should use the following code to add these two lists to the contact:

$contactObj->addList($lists['lists'][0]->id);
$contactObj->addList($lists['lists'][1]->id);

This addList method of the Contact class accepts one of two things are arguments:

  1. An instance of the ContactList class
  2. A string containing the numeric ID of the desired list

If you take this approach, then it should guarantee that the lists are properly associated with the contact.

 

If you continue to have any problems, please let us know!

 

Sincerely,

Elijah G.
API Support Engineer