cancel
Showing results for 
Search instead for 
Did you mean: 

Adding Contact

Occasional Contributor

Adding Contact

Below is a snippet of code I'm using to add a contact.

 

$cc = new ConstantContact(APIKEY);

$contact = new Contact();
$contact->addEmail($email);
$contact->addList($list_id);
$contact->first_name = $first_name;
$contact->last_name = $last_name;
$contact->company_name = $company_name;
$contact->addresses = array("line1"=>$street,"city"=>$city,"state"=>$state,"state_code"=>$state_code,"postal_code"=>$zip);

 All of the contact info was added, except the address. From the API documentation, I see that addresses are stored as an array. What is the correct way to send the address info?

8 REPLIES 8
Honored Contributor

Re: Adding Contact

Hello,

 

The correct way to add an address is as follows:

$cc = new ConstantContact(APIKEY);

$contact = new Contact();
$contact->addEmail($email);
$contact->addList($list_id);
$contact->first_name = $first_name;
$contact->last_name = $last_name;
$contact->company_name = $company_name;
$contact->addAddress(Address::create( array("line1"=>$street,"city"=>$city,"state"=>$state,"state_code"=>$state_code,"postal_code"=>$zip));

 

With the SDK, many data types such as addresses have been defined and have a static method called create that accepts an associative array containing the information to be stored. In this case, the change is very simple.

 

If you have any more questions, please let us know!

 

Sincerely,

Elijah G.
API Support Engineer
Occasional Contributor

Re: Adding Contact

Thanks for the reply.

 

I ran into trouble implementing the suggested change.

 

The first is minor. One closing parenthesis was missing, so it needs to be:

 

$contact->addAddress(Address::create( array("line1"=>$street,"city"=>$city,"state"=>$state,"state_code"=>$state_code,"postal_code"=>$zip)));

 More significantly, after running the updated code:

 

Class 'Address' not found in /home/myacct/public_html/constant_contact_api/apps/sync_contacts.php on line 104

 

Where is the Address class supposed to be?

CTCT Employee

Re: Adding Contact

Hi Thomas,

 

Sorry for the delay. The address class is in the Components/Contacts folder, and you will need to add this use statement to the top of your file:

 

use Ctct\Components\Contacts\Address;

 

Let me know if you continue to have problems. Thanks!

 

Best Regards,

Shannon Wallace

Partner API Support Engineer

Occasional Contributor

Re: Adding Contact

Yes, this worked! I appreciate that you're busy and responses can take time.

 

I'm now using this block of code to add a contact:

 

$contact = new Contact();
$contact->addEmail($_POST['email']);
$contact->addList($_POST['list']); 
$contact->first_name = $_POST['first_name'];
$contact->last_name = $_POST['last_name'];
$contact->company_name = $_POST['company_name'];
$contact->addAddress(Address::create( array("address_type"=>PERSONAL,"line1"=>$_POST['street'],"city"=>$_POST['city'],"state"=>$_POST['state'],"state_code"=>$state_code,"postal_code"=>$_POST['zip'])));

 Notice I added this to the start of the array: 

 

"address_type"=>PERSONAL,

I found that without designating the address type, it defaults to Other. When retrieving the address info from the API, I'm only able to retrieve the values if it was set to PERSONAL. I think the code processing the call to the API needs to be refined to get the addresses regardless of type. 

 

Here is what I'm now using to retrieve contacts:

 

$cc = new ConstantContact(APIKEY);
  
//list all active contacts
$cc_contacts = $cc->getContacts(ACCESS_TOKEN, array('status' => 'ACTIVE')); 

$new = $cc_contacts->results;

foreach($new as $item)
{ 
  $cc_id = $item->id;
  $first_name = stripslashes($item->first_name); 
  $middle_name = stripslashes($item->middle_name);  
  $last_name = stripslashes($item->last_name); 
  $company_name = stripslashes($item->company_name);

  $email_addresses = $item->email_addresses; 

  foreach($email_addresses as $item2)
  {
    $email = $item2->email_address;
  }

  $addresses = $item->addresses;	  

  foreach($addresses as $item3)
  {
    $street = $item3->line1; 
    $city = $item3->city; 
    $state = $item3->state_code; 
    $zip = $item3->postal_code;
  }
}
	 

 How can the address values be retrieved regardless of their type?

 

 

CTCT Employee

Re: Adding Contact

Hi Thomas,

 

I'm glad that helped! That address type issue is an API (not an SDK) limitation, and I should have thought about that. We're currently only able to access 'personal' physical addresses through the API.

 

We're working on coming up to parity on this, but for now the only address type accessible through the API is personal. We should modify the SDK to default to that. Thank YOU for taking the time to help us determine the issues around this.

 

Best Regards,

Shannon Wallace

Partner API Support Engineer

Occasional Contributor

Re: Adding Contact

Okay good.

 

It would be good to be notified in some way when this has been updated, so I'd know to update my code to allow the address type to be set by my users, and to be able to display multiple address if more than one type has been added for a given contact.

 

Thanks again!

CTCT Employee

Re: Adding Contact

Hi Thomas,

 

I was actually wrong. Looking at the documentation page here, we actually do tell you we accept 1 personal and 1 business physical address for each contact through the API, but no addresses with the "Unknown" type. I tested, and both BUSINESS and PERSONAL addresses are getting retrieved and are able to be pushed through the API. Here's the line I'm using to set either business or personal:

 

$contact->addAddress(Address::create( array("address_type"=>"BUSINESS","line1"=>$street,"city"=>$city,"state"=>$state,"state_code"=>$state_code,"postal_code"=>$zip)));

 

When I use this line to retrieve and print an email address added like that, I see in our response that the address type is business, so you should be able to retrieve both types as well.

 

$response = $cc->getContactByEmail(ACCESS_TOKEN, "test_01122015_12@test.com");
print_r($response); 

Let me know if you have trouble with that.

 

Best Regards,

Shannon Wallace

Partner API Support Engineer

Occasional Contributor

Re: Adding Contact

Thanks Shannon. 

 

Yes, it works to send/retrieve the address_type as PERSONAL or BUSINESS.