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;
}
?>