Call us Toll-Free:
1-800-218-1525
Email us

 Sponsors

iContact 2.0 API Integration PHP Example

Mike Peters, 12-25-2009
One of the cool features of SPI's Autoresponder service, is its ability to seamlessly integrate with third party autoresponders.

Once you designate an autoresponder as a third-party one, SPI will continue applying your predefined rules for adding/removing members from that autoresponder, while notifying the third-party service (GetResponse, iContact, AWeber) automatically.

Over the last few days, we have battling with iContact's new API. iContact announced their 2.0 API back on February 2009, but it's still labeled as "beta" and there's little to no documentation available online.

The SPI-iContact integration was using version 1.0 and our intention was to continue using that version until 2.0 is officially released.

Unfortunately, with no prior warning, version 1.0 stopped working and is no longer honoring requests to add contacts / subscriptions records.

iContact's API external login page is down. The help section is down:

Image

And the developer forums are ghost town USA.

Image

Ok, rant over.

We really like the folks at iContact. They offer an easy to use and inexpensive entry-level autoresponder service. I just wish they would update the API documentation.

The purpose of this post is to help others who are going through integrating with the iContact 2.0 API by providing simple PHP examples for adding a contract, subscripting a contact to a list and unsubscribing.

We haven't been able to find anything like this online, so I'm pretty sure this will help others going through the integration process.

The code below is self-contained. No need to use any other libraries.


function IContactLogin($account_id, $key, $user, $pass, &$client_folder_id)
{
 
// Build iContact authentication
 
$headers = array(
 
'Accept: text/xml',
 
'Content-Type: text/xml',
 
'Api-Version: 2.0',
 
'Api-AppId: ' . $key,
 
'Api-Username: ' . $user,
 
'Api-Password: ' . $pass
 
);

 
// Connect to iContact to retrieve the client folder id
 
$ch=curl_init("https://app.icontact.com/icp/a/$account_id/c/");
 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 
$buf = curl_exec($ch);
 
curl_close($ch);

 
// Extract client folder id from response
 
$client_folder_id = "";
  if ((
$pos=strpos($buf,"<clientFolderId>"))!==false)
  {
   
$client_folder_id = substr($buf, strlen("<clientFolderId>")+$pos);
    if ((
$pos=strpos($client_folder_id,"<"))!==false)
    {
     
$client_folder_id = substr($client_folder_id, 0, $pos);
    }
  }

 
// If we have a non empty client_folder_id,
  // then everything worked well
 
$result = ($client_folder_id+0 > 0);

 
// Return result
 
return $result;
}


function
IContactSubscribe($account_id, $key, $user, $pass, $email, $list_id, &$result_str)
{
 
// Get client folder id
 
if (!IContactLogin($account_id, $key, $user, $pass, &$client_folder_id))
  {
   
$result_str = "Failed retrieving client_folder_id for '$user'";
    return
0;
  }

 
// Build iContact authentication
 
$headers = array(
 
'Accept: text/xml',
 
'Content-Type: text/xml',
 
'Api-Version: 2.0',
 
'Api-AppId: ' . $key,
 
'Api-Username: ' . $user,
 
'Api-Password: ' . $pass
 
);

 
// Find contact_id for the given 'email'
 
$ch=curl_init("https://app.icontact.com/icp/a/$account_id/c/$client_folder_id/contacts/?email=".URLEncode($email));
 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 
$buf = curl_exec($ch);
 
curl_close($ch);

 
// Extract contactId from response
 
$contact_id = "";
  if ((
$pos=strpos($buf,"<contactId>"))!==false)
  {
   
$contact_id = substr($buf, $pos+strlen("<contactId>"));
    if ((
$pos=strpos($contact_id,"<"))!==false)
    {
     
$contact_id = substr($contact_id,0,$pos);
    }
  }

 
// If we don't have a contactId, can't add subscription
 
if (empty($contact_id))
  { 
   
$result_str = "Failed finding a contact with the email address of '$email'";
    return
0;
  }

 
// Build subscription record
 
$data = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n<subscriptions>\r\n";
 
$data.= "<subscription>\r\n";
 
$data.= "<contactId>$contact_id</contactId>\r\n";
 
$data.= "<listId>$list_id</listId>\r\n";
 
$data.= "<status>normal</status>\r\n";
 
$data.= "</subscription>\r\n</subscriptions>";

 
// Add subscription
 
$ch=curl_init("https://app.icontact.com/icp/a/$account_id/c/$client_folder_id/subscriptions/");
 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 
$buf = curl_exec($ch);
 
curl_close($ch);

 
// Extract subscriptionID from response
 
$subscription_id = "";
  if ((
$pos=strpos($buf,"<subscriptionId>"))!==false)
  {
   
$subscription_id = substr($buf, $pos+strlen("<subscriptionId>"));
    if ((
$pos=strpos($subscription_id,"<"))!==false)
    {
     
$subscription_id = substr($subscription_id,0,$pos);
    }
  }

 
// If we have a subscription id OR this subscription already existed, we're good
 
$result = !empty($subscription_id) || strpos($buf,"could not be updated")!==false;

 
// Set result string
 
$result_str = ($result ? "Updated subscription $subscription_id" : $buf);

 
// Return result
 
return $result;
}

function
IContactAddContact($account_id, $key, $user, $pass, $email, $firstname, $lastname,
&
$result_str)
{
 
// Get client folder id
 
if (!IContactLogin($account_id, $key, $user, $pass, &$client_folder_id))
  {
   
$result_str = "Failed retrieving client_folder_id for '$user'";
    return
0;
  }
 
 
// Build iContact authentication
 
$headers = array(
 
'Accept: text/xml',
 
'Content-Type: text/xml',
 
'Api-Version: 2.0',
 
'Api-AppId: ' . $key,
 
'Api-Username: ' . $user,
 
'Api-Password: ' . $pass
 
);
         
 
// Build contact record
 
$data = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n<contacts>\r\n";
 
$data.= "<contact>\r\n";
 
$data.= "<email>$email</email>\r\n";
 
$data.= "<firstName>$firstname</firstName>\r\n";
 
$data.= "<lastName>$lastname</lastName>\r\n";
 
$data.= "<status>normal</status>\r\n";
 
$data.= "</contact>\r\n</contacts>";

 
// Add contact
 
$ch=curl_init("https://app.icontact.com/icp/a/$account_id/c/$client_folder_id/contacts/");
 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 
$buf = curl_exec($ch);
 
curl_close($ch);

 
// Extract contactId from response
 
$contact_id = "";
  if ((
$pos=strpos($buf,"<contactId>"))!==false)
  {
   
$contact_id = substr($buf, $pos+strlen("<contactId>"));
    if ((
$pos=strpos($contact_id,"<"))!==false)
    {
     
$contact_id = substr($contact_id,0,$pos);
    }
  }

 
// If we have a contact id, we're good
 
$result = !empty($contact_id);

 
// Set result string
 
$result_str = ($result ? "Added new contact $contact_id" : $buf);

 
// Return result
 
return $result;
}

function
IContactUnsubscribe($account_id, $key, $user, $pass, $email, $list_id, &$result_str)
{
 
// Get client folder id
 
if (!IContactLogin($account_id, $key, $user, $pass, &$client_folder_id))
  {
   
$result_str = "Failed retrieving client_folder_id for '$user'";
    return
0;
  }

 
// Build iContact authentication
 
$headers = array(
 
'Accept: text/xml',
 
'Content-Type: text/xml',
 
'Api-Version: 2.0',
 
'Api-AppId: ' . $key,
 
'Api-Username: ' . $user,
 
'Api-Password: ' . $pass
 
);

 
// Find contact_id for the given 'email'
 
$ch=curl_init("https://app.icontact.com/icp/a/$account_id/c/$client_folder_id/contacts/?email=".URLEncode($email));
 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 
$buf = curl_exec($ch);
 
curl_close($ch);

 
// Extract contactId from response
 
$contact_id = "";
  if ((
$pos=strpos($buf,"<contactId>"))!==false)
  {
   
$contact_id = substr($buf, $pos+strlen("<contactId>"));
    if ((
$pos=strpos($contact_id,"<"))!==false)
    {
     
$contact_id = substr($contact_id,0,$pos);
    }
  }

 
// If we don't have a contactId, can't add subscription
 
if (empty($contact_id))
  {
   
$result_str = "Failed finding a contact with the email address of '$email'";
    return
0;
  }

 
// Build subscription record
 
$data = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n<subscriptions>\r\n";
 
$data.= "<subscription>\r\n";
 
$data.= "<contactId>$contact_id</contactId>\r\n";
 
$data.= "<listId>$list_id</listId>\r\n";
 
$data.= "<status>unsubscribed</status>\r\n";
 
$data.= "</subscription>\r\n</subscriptions>";

 
// Add subscription
 
$ch=curl_init("https://app.icontact.com/icp/a/$account_id/c/$client_folder_id/subscriptions/");
 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 
$buf = curl_exec($ch);
 
curl_close($ch);

 
// Extract subscriptionID from response
 
$subscription_id = "";
  if ((
$pos=strpos($buf,"<subscriptionId>"))!==false)
  {
   
$subscription_id = substr($buf, $pos+strlen("<subscriptionId>"));
    if ((
$pos=strpos($subscription_id,"<"))!==false)
    {
     
$subscription_id = substr($subscription_id,0,$pos);
    }
  }

 
// If we have a subscription id OR this subscription already unsubscribed, we're good
 
$result = !empty($subscription_id) || strpos($buf,"could not be updated")!==false;

 
// Set result string
 
$result_str = ($result ? "Updated subscription $subscription_id" : $buf);

 
// Return result
 
return $result;
}

Peter, 01-08-2010
Hi Mike,

I'm the product manager for the API at iContact. I noticed your blog post over the holidays and wanted to reach out to you. Thank you for your sample PHP Code. I'm sure it will help our API v2.0 Developers.

We do have a developer portal dedicated to API v2.0. Please feel free to send me an email so I can follow up regarding your concerns.

Thanks,

Peter Ghali

Kandarp, 06-30-2010
right now i am using the Knowledge tree document tree management system (KTDMS)for document management , i want INTEGRATE my site to this KTDMS ,and i also want that , when user upload document from our site , it also upload in KTDMS ,
if u have any idea about this please help me in that case .....

henderpender, 07-15-2010
I am trying to decipher all of the variables in this and I am getting stuck at the $account_id

Can you help?

Shekhar, 10-05-2010
This post is good to get an idea of integration. is there any snippet for .net ?

sdf, 12-21-2010
fgjfgj

najam, 05-24-2011
hi,
i want to send users information in two forms. ie user signups with username and email address information is sent to icontact and a new form appears with some more fields now i need to update the fields against the sent username and email can any body tell me how to do so?

Rasmus, 08-16-2011
Awesome post man, I agree on the fact that iContact needs to update their API documentation, cause it's really poorly documented.

Keep up the good work, this helped me out lots after cursing for about an hour, at the iContact API documentation.

drums, 08-31-2011
Hey, nice post
How can i get my account id?

im trying to do what the website sais but i cant make it because i get a SSL error (SSL certificate problem)

Thanks

Nitesh Hardia, 11-08-2011
Hello peter
i want to know about unsubscribe contact from list
can u please tell me about that?
thanks in advance

Shyam, 09-19-2012
Is there any snippet for .net? to integrate iContact
Regards
Shyam

zhimei, 10-23-2012
Hello Peter:
Can you help me?
i want to knwo about unsubscribe contact from list.
But i couldn't find it from icontact api.

thanks!

Jim, 06-17-2013
Nice example!

Is there any example that gets statistics from the sent messages?

For example.. How do I check how many people opened the messages I have sent? How many clicked? etc etc..

Thanks!

Sally, 04-07-2014
Thanks for the information. I am using your code and getting a syntax error on line 95 "$data.= '<?xml version="1.0" encoding="UTF-8"?>'."
<subscriptions>
";"
Any help would be appreciated

Martin, 12-01-2014
Thanks for this - I'm unlucky enough to have a cheapskate client who can only afford iContact and it's legendary rubbish API. Rather than waste 10 hours trying to get their own crappy library to work, this hit the spot. Thanks.
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions