cancel
Showing results for 
Search instead for 
Did you mean: 

Need help modifying this PHP Class for adding a contact

SOLVED
Highlighted
CTCT Employee

Re: Need help modifying this PHP Class for adding a contact

The code that you are using worked perfectly fine for me to move a contact from 'Removed' over to an 'Active' contact on a list.  It sounds like the contact is updating correctly, but you are looking at the contact object from when you initially pulled details. It is showing the two lists because you added:

 

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

 

So when you dump out $Contact, its showing you the lists you added with the status it had at the time, which was removed. Once you have updating the contact, try running:

 

// Search for a contact by Email Address
$search = $ConstantContact->searchContactsByEmail('example@example.com');

// Get details for the Contact
$UpdatedContact = $ConstantContact->getContactDetails($search[0]);

// dump out the results
print_r($UpdatedContact);

 

I hope this helps. Please let us know if this works for you.

David J

Advisor

Re: Need help modifying this PHP Class for adding a contact

Apparently the code only works if you have multiple lists because I deleted all lists except for my default list and now when I attempt to add a contact I get this:

 

 

Constant Contact HTTP Request Exception: Error 400: Contact List id: '' is not valid
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in /home/mysite/public_html/constantcontact/Collections.php on line 203

Warning: simplexml_load_string() [function.simplexml-load-string]: Error 400: Contact List id: '' is not valid in /home/mysite/public_html/constantcontact/Collections.php on line 203

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/mysite/public_html/constantcontact/Collections.php on line 203

Fatal error: Call to a member function Attributes() on a non-object in /home/mysite/public_html/constantcontact/Components.php on line 208

Advisor

Re: Need help modifying this PHP Class for adding a contact

If someone would be so kind as to tell me how to hardcode a sigle list ID that would be great and would probably solve the previous errors.

 

Instead of:

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

 

Something like:

 

 $contactObj-> listID = '10'; 

Advisor

Re: Need help modifying this PHP Class for adding a contact

Ok, I think I've got it working. Here's the final code. If you see any issues, please let me know:

 

-----------------

$email = '#####';
$firstname = '#####';
$lastname = '#####';

    $ConstantContact = new ConstantContact('basic', "#####", "#####", "#####");

    // Search for a contact by Email Address
    $search = $ConstantContact->searchContactsByEmail($email);


if (!$search) {
 
        $lists = $ConstantContact->getLists();
 
         $contactObj = new Contact();
         $contactObj->lists = array('https://api.constantcontact.com/ws/customers/#####/lists/10');
         $contactObj->emailAddress = $email;
         $contactObj->firstName = $firstname;
         $contactObj->lastName = $lastname;
 
        // Create the contact
         $Contact = $ConstantContact->addContact($contactObj);
     }
 
    else {
 $lists = $ConstantContact->getLists();
         // Get details for the Contact
         $Contact = $ConstantContact->getContactDetails($search[0]);
 
        // modify Contact properties
    $Contact->lists = array('https://api.constantcontact.com/ws/customers/#####/lists/10');
         $Contact->firstName = $firstname;
         $Contact->lastName = $lastname;
 
        //update the contact
         $Contact = $ConstantContact->updateContact($Contact);

// Search for a contact by Email Address
$search = $ConstantContact->searchContactsByEmail($email);

// Get details for the Contact
$UpdatedContact = $ConstantContact->getContactDetails($search[0]);

// dump out the results
#print_r($UpdatedContact);
 
    }

-------------------------

CTCT Employee

Re: Need help modifying this PHP Class for adding a contact

I'm glad you were able to resolve this. I did want to quickly point out that if you are going to be supplying the list id's manually (hard-coding them), then you can remove the $ConstantContact->getLists() calls as you are no longer using those values.

 

Please let us know if you have any other questions regarding our API.

David J

Advisor

Re: Need help modifying this PHP Class for adding a contact

Thanks for the great support! Just one more question...

 

Currently if I attempt to add an address that is on the Do Not Mail list, it returns the following:

 

Constant Contact HTTP Request Exception: Error 403: Contact ###@###.com has opted-out and may not be resubscribed

 

From my understanding I cannot automatically opt a subscriber back in, so how can I either

 

A) Insert them into the Awaiting Confirmation group and fire off the opt-in confirmation email (the preferred solution, if this is permitted)

 

or

 

B) Ignore the insert completely and suppress any error or message

 

 

CTCT Employee

Re: Need help modifying this PHP Class for adding a contact

Hello,

 

Contacts who have opted out of receiving all mails and have been placed on Do-Not-Mail list must opt themselves in.  The account owner cannot add a contact in Do-Not-Mail list back to a different list.  The action must be initiated by the contact.  When using the API, that means that the <OptInSource> must be ACTION_BY_CONTACT, which can only be used when the API call is the direct result of an action performed by the contact (e.g. clicking a Subscribe button in an application). It is a serious violation of the Constant Contact Terms of Service to use the Opt-in features of the API in any other way (i.e. opting in a contact without his or her action and consent).

 

In order to opt-in a contact, you must take the contact's detail data and modify <ContactLists> to include the contact contact lists in which the contact is signing up.  Then, you use the PUT method to apply the change against the contact resource.

 

Joseph D
Support Engineer, Constant Contact
Advisor

Re: Need help modifying this PHP Class for adding a contact

As for the statement that it opting back in "must be initiated by the contact", I don't think that is entirely true. Your own FAQ has this:

 

"To assist a contact who is having difficulty finding your signup box, you may begin the process by going to the box yourself and entering their email address. They will, however, need to follow the steps listed to confirm the subscription.:

 

My question was simply if there is a way I can do that automatically so that all they need to do is confirm that they wish to opt back in.

 

 

CTCT Employee

Re: Need help modifying this PHP Class for adding a contact

What Joseph is saying is exactly correct, and you want to make sure that you customers are the ones taking this action to opt themselves back in, or else that would be in violation of our terms and conditions. For the library that you are using, You would just need to set the opt in source if that contact was on the Do-Not-Mail list. For example, you could add something like:

 

if($Contact->status == "Do Not Mail"){
    $Contact->optInSource = "ACTION_BY_CONTACT";
}

 

 

I hope this helps to clear up any points of confusion. Please let us know if you have any other questions.

David J

Advisor

Re: Need help modifying this PHP Class for adding a contact

Ok, here is the final code with the latest change in bold. Please let me know if it's ok to do it this way:

 

------------------------------------------

if (!$search)
     {
         $contactObj = new Contact();
         $contactObj->lists = array('https://api.constantcontact.com/ws/customers/#####/lists/10');
         $contactObj->emailAddress = $email;
         $contactObj->firstName = $firstname;
         $contactObj->lastName = $lastname;
 
         // Create the contact
         $Contact = $ConstantContact->addContact($contactObj);
         }
 
    else

     {

         // Get details for the Contact
         $Contact = $ConstantContact->getContactDetails($search[0]);

        if($Contact->status == "Do Not Mail"){

                exit;
        }
 
         // modify Contact properties
     $Contact->lists = array('https://api.constantcontact.com/ws/customers/#####/lists/10');
         $Contact->firstName = $firstname;
         $Contact->lastName = $lastname;
 
         //update the contact
         $Contact = $ConstantContact->updateContact($Contact);
         }