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;
}
?>
Very helpful, thanks.
ReplyDeleteWish the iContact documentation was as thorough.
Thanks Kathy, appreciate the compliment.
ReplyDeletehi,
ReplyDeleteCan you please tell me how can add contact in particular list .
Thanks ,
Sandeep Dhiman
Hi Sandeep, I have a function above add_contact, just call it and supply the parameter required.
ReplyDeleteThanks,
Hi,
ReplyDeletewhere is i have to you get the blow details
$app_id = '';
$user = '';
$pass = '';
$folder_id = '';
thanks
You can get it by registering an application to this URL - https://app.icontact.com/icp/core/registerapp
ReplyDeletePlease take note that you need to register an account first before you can create an application.
DeleteHello -
ReplyDeleteFor some reason the unsubscribe function isn't updating my contact. All variables have been filled in.
Make sure you check the account id, folder id and list id? Did you use my function here?
ReplyDeleteHi Paul -
ReplyDeleteYes. 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!
Hi Paul -
ReplyDeleteI 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."
Hi A2 -
ReplyDeleteReally? 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/
Hi there,
ReplyDeleteDo you know how to fix this issue. ?
http://stackoverflow.com/questions/12721989/icontact-subscription-unsubscription-using-api
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.
DeleteWell, 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.
ReplyDeleteThis 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!
Hi,
ReplyDeleteHow can I send emails using such scripts?