SAMPLE COMMAND SENDER AUTOMATION FUNCTION

This is a sample function that sends well formed commands to 4Dem API and returns a decoded response. This custom function is used in forward samples. The input $data array must contain specific 4Dem API parameters used for automatic command string composition.

				<?php

					function sendCmdToAPI($data = array(), $isJson = false) {
						if ($isJson) {
							$argString = json_encode ( $data );
						} else {
							$argString = implode ( '&', $data );
						}
						
						//Insert here provided 4marketing server address
						$apiServerAddress = "4marketingServerAddress";
						
						$defaults = array (
								CURLOPT_POST => 1,
								CURLOPT_HEADER => 0,
								CURLOPT_URL => $apiServerAddress . '/api.php',
								CURLOPT_USERAGENT => '4marketing',
								CURLOPT_FRESH_CONNECT => 1,
								CURLOPT_RETURNTRANSFER => 1,
								CURLOPT_FORBID_REUSE => 1,
								CURLOPT_TIMEOUT => 2,
								CURLOPT_SSL_VERIFYPEER => 0,
								CURLOPT_SSL_VERIFYHOST => 0,
								CURLOPT_POSTFIELDS => $argString 
						);
						
						$ch = curl_init ();
						
						curl_setopt_array ( $ch, $defaults );
						
						$response = curl_exec ( $ch );
						
						curl_close ( $ch );
						
						$response = json_decode ( $response );
						
						return $response;
					}

				?>
				

LOGGIN IN AND GETTING A VALID SESSION ID

Try to login on the console and retrieve a valid SessionID used for further operations

				<?php

					function login($username, $password) {
						// Command array composition
						$login = array ();
						$login [] = "Command=User.Login";
						$login [] = "ResponseFormat=JSON";
						$login [] = "Username=" . $username;
						$login [] = "Password=" . $password;
						
						// Send command array to remote API via JSON custom function "sendCmdToAPI"
						$response = sendCmdToAPI ( $login );
						
						if ($response) {
							if ($response->Success == true) {
								// If logged in returns the SessionID
								return $response->SessionID;
							}
						}
						return false;
					}

				?>
				

RETRIEVING SUBSCRIPTION LISTS FROM CONSOLE

Get Lists from console

				<?php

					function getLists($yourSessionID) {
						// Command array composition
						$lists = array ();
						$lists [] = "Command=Lists.Get";
						$lists [] = "ResponseFormat=JSON";
						$lists [] = "SessionID=" . $yourSessionID;
						$lists [] = "OrderField=ListID";
						$lists [] = "OrderType=ASC";
						
						//Send command array to remote API via JSON custom function "sendCmdToAPI"
						$response = sendCmdToAPI ( $lists );
						
						if ($response) {
							if ($response->Success == true) {
								// On success returns console Lists
								return $response->Lists;
							}
						}
						return false;
					}

				?>
				

RETRIEVING CUSTOM FIELDS FROM CONSOLE

Gets custom fields associated to a specific List on the console

				<?php

					function getCustomFields($yourListId, $yourSessionID) {
						// Command array composition
						$customFields = array ();
						$customFields [] = "Command=CustomFields.Get";
						$customFields [] = "ResponseFormat=JSON";
						$customFields [] = "SessionID=" . $yourSessionID;
						$customFields [] = "OrderField=FieldName";
						$customFields [] = "OrderType=ASC";
						$customFields [] = "SubscriberListID=" . $yourListId;
						
						// Send command array to remote API via JSON custom function "sendCmdToAPI"
						$response = sendCmdToAPI ( $customFields );
						
						if ($response) {
							if ($response->Success == true) {
								// On success returns Custom Fields for selected List
								return $response->CustomFields;
							}
						}
						return false;
					}

				?>
				

SUBSCRIBING USERS

Subscribe an email address to a specific subscription List. Optionally sets custom fields values for current subscriber.

				<?php

					function subscribe($listId, $email, $customFields = false, $customerInfo = false) {
						// Command array composition
						$subscribe = array ();
						$subscribe [] = "Command=Subscriber.Subscribe";
						$subscribe [] = "ResponseFormat=JSON";
						$subscribe [] = "ListID=" . $listId;
						$subscribe [] = "EmailAddress=" . $email;
						
						// Check if a CustomFields array is passed
						// If it is, a well formatted array is generated using CustomerInfos details
						if ($customFields) {
							foreach ( $customFields as $key => $field ) {
								$subscribe [] = "CustomField" . $field . "=" . $customerInfo [$key];
							}
						}
						
						// Set the IPAddress required field
						$subscribe [] = "IPAddress=" . $_SERVER ['REMOTE_ADDR'];
						
						// Send command array to remote API via JSON custom function "sendCmdToAPI"
						$response = sendCmdToAPI ( $subscribe );
						
						if ($response) {
							if ($response->Success == true) {
								return true;
							}
						}
						return false;
					}

				?>
				

UPDATING SUBSCRIBED USERS

Updates personal information for subscribed email/user

				<?php

					function updSubscriber($sessionID, $subscriberID, $listId, $email, $customFields = false, $customerInfo = false) {
						// Command array composition
						$update = array ();
						$update [] = "SessionID=" . $sessionID;
						$update [] = "Command=Subscriber.Update";
						$update [] = "ResponseFormat=JSON";
						$update [] = "SubscriberID=" . $subscriberID;
						$update [] = "SubscriberListID=" . $listId;
						$update [] = "EmailAddress=" . $email;
						
						// Check if a CustomFields array is passed
						// If it is, a well formatted array is generated using CustomerInfos details
						if ($customFields) {
							foreach ( $customFields as $key => $field ) {
								$update [] = "Fields[CustomField" . $field . "]=" . $customerInfo [$key];
							}
						}
						
						// Setup access level
						$update [] = "Access=admin";
						
						// Send command array to remote API via JSON custom function "sendCmdToAPI"
						$response = sendCmdToAPI ( $update );
						
						if ($response) {
							if ($response->Success == true) {
								return true;
							}
						}
						return false;
					}

				?>
				

UNSUBSCRIBING USERS

Unsubscribe an email address from a specific list

				<?php

					function unsubscribe($sessionID, $listId, $email) {
						// Command array composition
						$unsubscribe = array ();
						$unsubscribe [] = "SessionID=" . $sessionID;
						$unsubscribe [] = "Command=Subscriber.Unsubscribe";
						$unsubscribe [] = "ResponseFormat=JSON";
						$unsubscribe [] = "ListID=" . $listId;
						$unsubscribe [] = "CampaignID=";
						$unsubscribe [] = "EmailAddress=" . $email;
						$unsubscribe [] = "Preview=0";
						$unsubscribe [] = "IPAddress=" . $_SERVER ['REMOTE_ADDR'];
						
						// Send command array to remote API via JSON custom function "sendCmdToAPI"
						$response = sendCmdToAPI ( $unsubscribe );
						
						if ($response) {
							if ($response->Success == true) {
								return true;
							}
						}
						return false;
					}

				?>
				

CALL API COMMANDS FROM ASP CLASSIC

API commands can be called from ASP classic code using a standard XMLHTTP object. Command feedback is stored as a JSON formatted string into connection object responseText property.

In the following example we implemented the aspJSON custom ASP library to manage JSON string as an object: aspJSON can be downloaded from http://www.aspjson.com/

				<!--#include virtual="/aspJSON1.17.asp" -->
				<%	
					
					'Define the API URL (maybe done globally) 
					url = "4marketingServerAddress" + "/api.php?"

					'Define the complete command string (in this case login command) with all the parameters'
					commandList = "Command=User.Login&ResponseFormat=JSON&Username=yourUsername&Password=yourPassword"
					
					'Create an XMLHTTP object, connect it (url + command list) and get the response as a string (resposeText property)	
					Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
					xmlhttp.Open "GET", url + commandList, false
					xmlhttp.Send
					
					'Create an aspJSON object, load JSON formatted text response in it and access JSON data
					Set oJSON = New aspJSON 
					oJSON.loadJSON(xmlhttp.responseText)
					response.write "Your session ID is: " oJSON.data("SessionID")
					
					set xmlhttp = nothing
					set oJSON = nothing

				%>