tessguefen
8/24/2015 - 5:15 PM

DotMailer API Example

DotMailer API Example

<?php
  // This has customfields with DotMailer IF the source is checkout (aka: osel/ ocst)
	// DotMailer API URL
	$postContactUrl = 'https://apiconnector.com/v2/contacts/';
	$addressBookID = 'ADDRESSBOOKID';
	$user = 'apiuser-xxxxxxxxxxxxx@apiconnector.com';
	$pass = 'PASSWORD';

	// Source
	$PROSPECTSOURCE = $_POST['source'];

	// Form Elements
	// First Name -----------------------
	if ( isset($_POST['ShipFirstName'])) {
		$FIRSTNAME = $_POST['ShipFirstName'];
	} else {
		$FIRSTNAME = '';
	}
	// Last Name -----------------------
	if ( isset($_POST['ShipLastName'])) {
		$LASTNAME = $_POST['ShipLastName'];
	} else {
		$LASTNAME = '';
	}
	// Email -----------------------
	if (isset($_POST['ShipEmail'])) {
		$email = $_POST['ShipEmail'];
	} else {
		$email = '';
	}
	// ZipCode -----------------------
	if ( isset($_POST['ShipZip'])) {
		$ZIPCODE = $_POST['ShipZip'];
	} else {
		$ZIPCODE = '';
	}
	// Address 1 -----------------------
	if ( isset($_POST['ShipAddress1'])) {
		$ADDRESS1 = $_POST['ShipAddress1'];
	} else {
		$ADDRESS1 = '';
	}
	// Address 2 -----------------------
	if ( isset($_POST['ShipAddress2']) ) {
		$ADDRESS2 = $_POST['ShipAddress2'];
	} else {
		$ADDRESS2 = '';
	}
	// City -----------------------
	if ( isset($_POST['ShipCity']) ) {
		$CITY = $_POST['ShipCity'];
	} else {
		$CITY = '';
	}
	// ShipState -----------------------
	if ( isset($_POST['ShipStateSelect']) ) {
		$STATEPROVINCE = $_POST['ShipStateSelect'];
	} elseif (isset($_POST['ShipState']) ) {
		$STATEPROVINCE = $_POST['ShipState'];
	} else {
		$STATEPROVINCE = '';
	}
	// Country -----------------------
	if ( isset($_POST['ShipCountry']) ) {
		$COUNTRY = $_POST['ShipCountry'];
	} else {
		$COUNTRY = '';
	}

	$data = array(
			'Email' => $email, //email to post
			'EmailType' => 'Html', //other option is PlainText
			'optInType' => 'Single',
			'dataFields' => array(
				array(
					'Key' => 'PROSPECTSOURCE',
					'Value' => $PROSPECTSOURCE,
					),
				array(
					'Key' => 'FIRSTNAME',
					'Value' => $FIRSTNAME,
				),
				array(
					'Key' => 'LASTNAME',
					'Value' => $LASTNAME,
				),
				array(
					'Key' => 'ADDRESS1',
					'Value' => $ADDRESS1,
				),
				array(
					'Key' => 'ADDRESS2',
					'Value' => $ADDRESS2,
				),
				array(
					'Key' => 'STATEPROVINCE',
					'Value' => $STATEPROVINCE,
				),
				array(
					'Key' => 'CITY',
					'Value' => $CITY,
				),
				array(
					'Key' => 'ZIPCODE',
					'Value' => $ZIPCODE,
				),
				array(
					'Key' => 'COUNTRY',
					'Value' => $COUNTRY,
				),
			),
	);

	//post email and response will be contact object from dotmailer
	$contact = execute_post($postContactUrl, $data, $user, $pass);

	/** ADD CONTACT TO ADDRESS BOOK */
	$addContactToAddressBookUrl = 'https://apiconnector.com/v2/address-books/' . $addressBookID . '/contacts';
	//post contact to address book and response will be address book object from dotmailer
	$book =  execute_post($addContactToAddressBookUrl, $contact, $user, $pass);

	// Uncomment for Debugging.
	//echo "<pre>" . print_r($contact, true) . "</pre>";
	//echo "<pre>" . print_r($book, true) . "</pre>";

	//Function to initiate curl, set curl options, execute and return the response
	function execute_post($url, $data, $user, $pass){
			//encode the data as json string
			$requestBody = json_encode($data);

			//initialise curl session
			$ch = curl_init();

			//curl options
			curl_setopt($ch, CURLAUTH_BASIC, CURLAUTH_DIGEST);
			curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass); // credentials
			curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_TIMEOUT, 10);
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . 'application/json' ,'Content-Type: application/json'));

			//curl execute and json decode the response
			$responseBody = json_decode(curl_exec($ch));

			//close curl session
			curl_close($ch);
			return $responseBody;
	}
	//if successful: 'Subscribed'
	echo $contact->status;
<mvt:comment>
|
| DotMailer Signup
|
</mvt:comment>
<mvt:if expr="g.dm_subscribe EQ 'yes'">
	<mvt:assign name="g.newsletter_url" value="'http://' $ g.domain:name $ '/forms/dotmailerform.php'" />
	<!-- Testing -->

	<mvt:assign name="g.ShipEmail" value="l.settings:customer:ship_email" />
	<mvt:assign name="g.ShipFirstName" value="l.settings:customer:ship_fname" />
	<mvt:assign name="g.ShipLastName" value="l.settings:customer:ship_lname" />
	<mvt:assign name="g.ShipZip" value="l.settings:customer:ship_zip" />
	<mvt:assign name="g.ShipAddress1" value="l.settings:customer:ship_addr1" />
	<mvt:assign name="g.ShipAddress2" value="l.settings:customer:ship_addr2" />
	<mvt:assign name="g.ShipCity" value="l.settings:customer:ship_city" />
	<mvt:assign name="g.ShipStateSelect" value="l.settings:customer:ship_state" />
	<mvt:assign name="g.ShipState" value="l.settings:customer:ShipState" />
	<mvt:assign name="g.ShipCountry" value="l.settings:customer:ship_cntry" />
	<mvt:assign name="g.source" value="'Checkout'" />

	<mvt:call action="g.newsletter_url" method="'POST'" fields="'ShipEmail,ShipFirstName,ShipLastName,ShipZip,ShipAddress1,ShipAddress2,ShipCity,ShipStateSelect,ShipState,ShipCountry,source'">
	</mvt:call>
</mvt:if>