tessguefen
9/1/2015 - 3:08 PM

Email Direct PHP sign up form. Currently Hard Coded values.

Email Direct PHP sign up form. Currently Hard Coded values.

<?php
	// Email Direct API Subscriber

	$postContactUrl = 'https://rest.emaildirect.com/v1/Subscribers';

	$EmailAddress = 'tguefen@miva.com';
	$FirstName = 'Tess';
	$LastName = 'Guefen';
	$Gender = 'F';
	$Country = 'United States';
	$Birthday = '01/01/1901';
	$Riding = '2001 Bike';

	$data = array(
			'EmailAddress' => $EmailAddress,
			'Force' => true,
			'CustomFields' => array(
				array(
					'FieldName' => 'FirstName',
					'Value' => $FirstName 
				),
				array (
					'FieldName' => 'LastName',
					'Value' => $LastName
				),
				array(
					'FieldName' => 'independentironUser',
					'Value' => 1
				),
				array (
					'FieldName' => 'Gender',
					'Value' => $Gender
				),
				array(
					'FieldName' => 'Country',
					'Value' => $Country
				),
				array(
					'FieldName' => 'Birthday',
					'Value' => $Birthday
				),
				array(
					'FieldName' => 'Riding',
					'Value' => $Riding
				),
			),
	);

	$book =  execute_post($postContactUrl, $data);

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

	//Function to initiate curl, set curl options, execute and return the response
	function execute_post($url, $data){
			//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_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', 'ApiKey: xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxx'));

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

			//close curl session
			curl_close($ch);
			return $responseBody;
	}