Thursday, July 19, 2012

iContact API Integration on PHP

This post will teach you how to integrate your app with iContact. The API requires JSON for doing requests and responses. On this implementation, I used CURL which I commonly use when doing API call.

First thing we need to do is to register on iContact then get an API credential. Register your app and get your application id, folder id, username and password - https://app.icontact.com/icp/core/registerapp

Once we have all the credentials, we are now ready to start building our codes.

First, we need to create a function that we will commonly use such as the headers, please see function I created for setting the headers.

function set_headers($app_id, $user, $pass) {
        $headers = array(
                'Accept: application/json',
                'Content-Type: application/json',
                'Api-Version: 2.0',
                'Api-AppId: ' . $app_id,
                'Api-Username: ' . $user,
                'Api-Password: ' . $pass
        );

        return $headers;


Next is to get information from our iContact account. Since we need the account id for all our API call, we will first request for our account information.

This function will return JSON response which we need to parse to get the information we need.

$app_id = '<put your app id>';
$user = '<put your username/email address>';
$pass = '<put your password>';
$folder_id = '<put your folder id>';

$data = get_account_info($app_id, $user, $pass);
var_dump($data);

function get_account_info($app_id, $user, $pass) {
        $headers = set_headers($app_id, $user, $pass);

        $ch = curl_init("https://app.icontact.com/icp/a");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json = curl_exec($ch);

        if (curl_errno($ch)) echo curl_error($ch);
        curl_close($ch);

        return $json;
}


Sample output will be like this, which is a JSON response.

string(582) "{"accounts":[{"billingStreet":"","billingCity":"","billingState":"","billingPostalCode":"","billingCountry":"","city":"Caloocan","accountId":"#######","companyName":"Hotshots Point of View","country":"Philippines","email":"paulgonzaga80@gmail.com","enabled":"1","fax":"","firstName":"Paul","lastName":"Gonzaga","multiClientFolder":"0","multiUser":"0","phone":"639213189673","postalCode":"1421","state":"NCR","street":"#409 B1 L5, Bagumbong Itaas, Progressive Village, Novaliches Caloocan City1","title":"","accountType":"1","subscriberLimit":"500"}],"total":1,"limit":20,"offset":0}"

This will return our account information which we will use for other API call. Once we have the Account ID, we can call now our client folders, though we get it already upon registration but just for the benefit of this post, we will try to get it via API call. The parameters we passed on this function is came from above call and credentials.

$data = json_decode($data);
$account_id = $data->accounts[0]->accountId;

$data = get_client_folders($app_id, $user, $pass, $account_id);
var_dump($data);

function get_client_folders($app_id, $user, $pass, $account_id) {
        $headers = set_headers($app_id, $user, $pass);

        $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);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json = curl_exec($ch);

        if (curl_errno($ch)) echo curl_error($ch);
        curl_close($ch);

        return $json;
}


Sample output will be like this which is the same folder id when we do our registration.

string(69) "{"clientfolders":[{"clientFolderId":"####","logoId":null}],"total":1}"

After this, we can now proceed adding contact, getting contact information, list all our contacts, subscriptions, etc..

The sample script I made below will help you be able to move forward with other functions. Happy coding!!

<?php

$app_id = '<put your app id>';
$user = '<put your username/email address>';
$pass = '<put your password>';
$folder_id = '<put your folder id>';

$data = get_account_info($app_id, $user, $pass);
var_dump($data);

$data = json_decode($data);
$account_id = $data->accounts[0]->accountId;

$data = get_client_folders($app_id, $user, $pass, $account_id);
var_dump($data);


function set_headers($app_id, $user, $pass) {
        $headers = array(
                'Accept: application/json',
                'Content-Type: application/json',
                'Api-Version: 2.0',
                'Api-AppId: ' . $app_id,
                'Api-Username: ' . $user,
                'Api-Password: ' . $pass
        );

        return $headers;
}

function get_account_info($app_id, $user, $pass) {
        $headers = set_headers($app_id, $user, $pass);

        $ch = curl_init("https://app.icontact.com/icp/a");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json = curl_exec($ch);

        if (curl_errno($ch)) echo curl_error($ch);
        curl_close($ch);

        return $json;
}

function get_client_folders($app_id, $user, $pass, $account_id) {
        $headers = set_headers($app_id, $user, $pass);

        $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);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json = curl_exec($ch);

        if (curl_errno($ch)) echo curl_error($ch);
        curl_close($ch);

        return $json;
}

function add_contact($app_id, $user, $pass, $account_id, $folder_id, $email, $fname, $lname, $status='normal') {
        $headers = set_headers($app_id, $user, $pass);

        $data = array(
                'contact' => array(
                        'email'         => $email,
                        'firstName'     => $fname,
                        'lastName'      => $lname,
                        'status'        => $status
                )
        );

        $data = json_encode($data);

        $ch = curl_init("https://app.icontact.com/icp/a/$account_id/c/$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);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json = curl_exec($ch);

        if (curl_errno($ch)) echo curl_error($ch);
        curl_close($ch);

        return $json;
}

function get_contact_info($app_id, $user, $pass, $account_id, $folder_id, $email) {
        $headers = set_headers($app_id, $user, $pass);

        $ch = curl_init("https://app.icontact.com/icp/a/$account_id/c/$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);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json = curl_exec($ch);

        if (curl_errno($ch)) echo curl_error($ch);
        curl_close($ch);

        return $json;
}

function get_contact_info_by_id($app_id, $user, $pass, $account_id, $folder_id, $contact_id) {
        $headers = set_headers($app_id, $user, $pass);

        $ch = curl_init("https://app.icontact.com/icp/a/$account_id/c/$folder_id/contacts/?contactId=".urlencode($contact_id));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json = curl_exec($ch);

        if (curl_errno($ch)) echo curl_error($ch);
        curl_close($ch);

        return $json;
}

function list_contacts($app_id, $user, $pass, $account_id, $folder_id) {
        $headers = set_headers($app_id, $user, $pass);

        $ch = curl_init("https://app.icontact.com/icp/a/$account_id/c/$folder_id/contacts/");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json = curl_exec($ch);

        if (curl_errno($ch)) echo curl_error($ch);

        if (curl_errno($ch)) echo curl_error($ch);
        curl_close($ch);

        return $json;
}

function subscribe($app_id, $user, $pass, $account_id, $folder_id, $email, $list_id) {
        $json = get_contact_info($app_id, $user, $pass, $account_id, $folder_id, $email);
        $obj = json_decode($json);
        $contact_id = $obj->contacts[0]->contactId;

        $headers = set_headers($app_id, $user, $pass);

        $data = array(
                'subscription' => array(
                        'contactId'     => $contact_id,
                        'listId'        => $list_id,
                        'status'        => 'normal'
                )
        );

        $data = json_encode($data);

        $ch = curl_init("https://app.icontact.com/icp/a/$account_id/c/$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);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json = curl_exec($ch);

        if (curl_errno($ch)) echo curl_error($ch);
        curl_close($ch);

        return $json;
}

function unsubscribe($app_id, $user, $pass, $account_id, $folder_id, $email, $list_id) {
        $json = get_contact_info($app_id, $user, $pass, $account_id, $folder_id, $email);
        $obj = json_decode($json);
        $contact_id = $obj->contacts[0]->contactId;

        $headers = set_headers($app_id, $user, $pass);

        $data = array(
                'subscription' => array(
                        'contactId'     => $contact_id,
                        'listId'        => $list_id,
                        'status'        => 'unsubscribed'
                )
        );

        $data = json_encode($data);

        $ch = curl_init("https://app.icontact.com/icp/a/$account_id/c/$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);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json = curl_exec($ch);

        if (curl_errno($ch)) echo curl_error($ch);
        curl_close($ch);

        return $json;
}

function list_subscribers($app_id, $user, $pass, $account_id, $folder_id) {
        $headers = set_headers($app_id, $user, $pass);

        $ch = curl_init("https://app.icontact.com/icp/a/$account_id/c/$folder_id/subscriptions/");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json = curl_exec($ch);

        if (curl_errno($ch)) echo curl_error($ch);
        curl_close($ch);

        return $json;
}


?>

16 comments:

  1. Very helpful, thanks.
    Wish the iContact documentation was as thorough.

    ReplyDelete
  2. Thanks Kathy, appreciate the compliment.

    ReplyDelete
  3. hi,

    Can you please tell me how can add contact in particular list .


    Thanks ,
    Sandeep Dhiman

    ReplyDelete
  4. Hi Sandeep, I have a function above add_contact, just call it and supply the parameter required.

    Thanks,

    ReplyDelete
  5. Hi,

    where is i have to you get the blow details

    $app_id = '';
    $user = '';
    $pass = '';
    $folder_id = '';

    thanks

    ReplyDelete
  6. You can get it by registering an application to this URL - https://app.icontact.com/icp/core/registerapp

    ReplyDelete
    Replies
    1. Please take note that you need to register an account first before you can create an application.

      Delete
  7. Hello -

    For some reason the unsubscribe function isn't updating my contact. All variables have been filled in.

    ReplyDelete
  8. Make sure you check the account id, folder id and list id? Did you use my function here?

    ReplyDelete
  9. Hi Paul -

    Yes. Account ID, Folder ID have been inputted. You can view my code here: http://pastebin.com/EAh8dgDG

    Variables such as account, password, etc. have been scrubbed (ex: $app_id = 'xxx') for pastebin.

    Thanks for the help!

    ReplyDelete
  10. Hi Paul -

    I received a message from iContact regarding the code. They said it's not possible:

    "Unfortunately there isn't a way to generate an Unsubscribe page on a website, as the those links and the info required per user is generated dynamically by iContact. Also, once a Subscriber has been set to Unsubscribed, only they can revert themselves back to a subscribed (normal) status from any previously received message. If you are looking to manage Subscribers, as you discovered, you may want to just move them to an unused list."

    ReplyDelete
  11. Hi A2 -

    Really? Coz I tested this before but it's been a long time already. Anyway, you can check this document and check out data fields "status" and there's "unsubscribe" which I used here. not sure if they really remove it coz it's really useful, don't know what their objective if this is true.

    http://www.icontact.com/developerportal/documentation/subscriptions/

    ReplyDelete
  12. Hi there,

    Do you know how to fix this issue. ?

    http://stackoverflow.com/questions/12721989/icontact-subscription-unsubscription-using-api

    ReplyDelete
    Replies
    1. Looks like they change already their API.. They are now using subscription id on the URL which is equivalent to listId_contactId but the status that you put in should be "normal" to subscribe then "unsubscribed" to unsubscribe. The accountId, folderId that you have on the URL should also be correct for you not to get a forbidden error.

      Delete
  13. Well, you can use my unsubscribe function here. Please take note as well of the api you are connecting to, it should be the same. So, if you are using their sandbox api, you should use it to all, just change app.icontact.com to app.sandbox.icontact.com. Also, the status value you put in is wrong, the subscribe status should be "normal" then the unsubscribe should be "unsubscribed". You should also follow the data structure you will pass to icontact, first level array should be subscriptions, and under that level is another array with conytactId, listId, and status.

    This is for subscription:
    $data = array(
    'subscription' => array(
    'contactId' => $contact_id,
    'listId' => $list_id,
    'status' => 'normal'
    )
    );

    This will be for unsubscription:
    $data = array(
    'subscription' => array(
    'contactId' => $contact_id,
    'listId' => $list_id,
    'status' => 'unsubscribed'
    )
    );

    Hope this helps!

    ReplyDelete
  14. Hi,
    How can I send emails using such scripts?

    ReplyDelete

Leadership 101


  • Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
  • Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
  • Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
  • Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
  • Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
  • Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.