The last type I wrote a tutorial in regards to using a web-based survey platform, it was how to access Wufoo form data.

In that tutorial, I said “Before we know it, Thanksgiving and Christmas will be here and it’ll be the end of the year.”

Well, Thanksgiving down, and Christmas and the end of the year are near.

To follow up the Wufoo API tutorial, there have been many requests for Typeform.

Unlike CCB forms, Typeform allows for a customized design and experience that is easy to use and responsive across multiple device platforms.

In addition, Typeform offers an API to access submitted form data.

So yes, you can customize the Typeform experience to match your church’s website’s look and feel, yet capture data in CCB too.

Although I didn’t cover the full integration to CCB with the Wufoo form, I’m doing so today with the Typeform tutorial.

Let’s get started with today’s tutorial

Your Typeform Account and Form

For starters, if you haven’t already created a Typeform account and your first form, please do so now. Make sure your from has the following fields:

  • Full Name
  • Email
  • Phone

We’ll use the Typeform API to access your form data. Go ahead and complete the form a few times to ensure you have data to access.

Setting Typeform API and PHP variables

If you plan on integrating today’s tutorial with CCB data, then I recommend starting by including the normal file inclusions and comments as we have in previous tutorials.

Next, using the Typeform API documentation, define the following variables:

  • tfApiKey – set this variable equal to the Typeform API key
  • tfUID – set this variable equal to the Typeform form Id
  • tfBaseAPIUrl – set this variable equal to the Typeform base url for forms
<?PHP

/*
	SYNCING CCB PROFILE DATA TO TYPEFORM
	Simple tutorial to add CCB profiles from Typeform survey(s)
*/

// contains resusable globals and functions
include("includes/config.php");

// Typeform Credentials
// set the Typeform API key - read About API Keys: https://www.typeform.com/help/data-api/
$tfApiKey = 'XxXxXxXxXxXxXx';

// set the id of desired typeform form ID 
$tfUID = '2THATZl';

// set the base API URL
$tfBaseAPIUrl  = 'https://api.typeform.com/v1/form/';

?>

Time to make Typeform API call using PHP

After setup credentials have been successfully defined, it’s time to create the target url that will be passed to the Typeform function to make the API call.

Create a targetUrl variable and set the value to look like the code below:

<?PHP

// set taret url
$targetUrl = $tfBaseAPIUrl.$tfUID.'?key='.$tfApiKey.'&completed=true';

?>

Note: The Typeform API can return data using json or xml. Read the Typeform API for more details.

Next, create a responseArr variable and set it equal to the Typeform function, passing get as first argument and the targetUrl as the second argument.

<?PHP

$responseArr = Typeform('get',$targetUrl);

?>

I’ll cover the Typeform function details here shortly.

Parsing Typeform API and PHP response

Next, set a response variable, and set it to use PHP’s built-in json_decode method, setting the first argument to pass the previous responseData variable and the second argument to true.

Next, set an empty displayData variable.

<?PHP

$response = json_decode($responseArr,true);

$displayData = '';

?>

Because Typeform API returns a status code when an API call is made, set an if statement to check the response variable’s http_status for being equal to 200.

For now, we’ll focus on the if statement being successful, but you could easily add additional error validation rules if you would like.

Once inside the if statement, then create an ids variable and set it to an empty array.

Next, create a foreach statement to traverse each Typeform question, passing the response variable questions element. Within the foreach statement, set the ids variable to add each questions respective id to the ids array. Then, close the foreach statement.

<?PHP
if($response['http_status'] == 200){

//echo sizeof($response['questions']);

$ids = array();

foreach($response['questions'] as $queData){

	$ids[] = $queData['id'];

	}

}

?>

Next, create another foreach statement to traverse each Typeform response. Within the foreach statement, you can now access each responses answers by placing the ids array values within each answer array as shown below:

<?PHP

if($response['http_status'] == 200){

//echo sizeof($response['questions']);

$ids = array();

foreach($response['questions'] as $queData){

	$ids[] = $queData['id'];

	}

foreach($response['responses'] as $resData){

	echo 'Name: '.$resData['answers'][$ids[0]].'<br>';
	echo 'Email: '.$resData['answers'][$ids[1]].'<br>';
	echo 'Phone: '.$resData['answers'][$ids[2]].'<br><br>';


}

}

?>

Next, it’s time to check to see whether the person submitting the form has a valid CCB profile.

To do so, create a userExist variable and set it equal to the checkUserExistence function, passing the second answer array index as the first argument and the third answer array index as the second argument.

<?PHP

if($response['http_status'] == 200){

//echo sizeof($response['questions']);

$ids = array();

foreach($response['questions'] as $queData){

	$ids[] = $queData['id'];

	}

foreach($response['responses'] as $resData){

	echo 'Name: '.$resData['answers'][$ids[0]].'<br>';
	echo 'Email: '.$resData['answers'][$ids[1]].'<br>';
	echo 'Phone: '.$resData['answers'][$ids[2]].'<br><br>';

	$userExist = checkUserExistence($resData['answers'][$ids[1]],$resData['answers'][$ids[2]]);

}

}

Now that the userExist variable exists, create an if statement that checks the value of userExist variable. If the value of userExist variable is less than 1, then there is not a valid CCB profile for the person and a profile will need to be created.

To create the profile, set the displayData variable to concatenate and be equal to createPerson function person, passing all three arrays (i.e., full name, email and phone) as function arguments in their respective order.

<?PHP

if($response['http_status'] == 200){

//echo sizeof($response['questions']);

$ids = array();

foreach($response['questions'] as $queData){

	$ids[] = $queData['id'];

	}

foreach($response['responses'] as $resData){

	echo 'Name: '.$resData['answers'][$ids[0]].'<br>';
	echo 'Email: '.$resData['answers'][$ids[1]].'<br>';
	echo 'Phone: '.$resData['answers'][$ids[2]].'<br><br>';

	$userExist = checkUserExistence($resData['answers'][$ids[1]],$resData['answers'][$ids[2]]);

	if($userExist < 1)
		$displayData .= createPerson($resData['answers'][$ids[0]],$resData['answers'][$ids[1]],$resData['answers'][$ids[2]]);

}

}

?>

Next, close the foreach statement and the if statement, and then we finally echo the displayData variable.

<?PHP

echo $displayData;

?>

Put it together and test away

Yes, you’re ready to test your Typeform API prowess and gain a quite a trove of manual migration hours back.

Save your file and open it via a web browser. Here’s what your code should look like:

<?PHP

/*
	SYNCING CCB PROFILE DATA TO TYPEFORM
	Simple tutorial to add CCB profiles from Typeform survey(s)
*/

// contains resusable globals and functions
include("includes/config.php");

// Typeform Credentials
// set the Typeform API key - read About API Keys: https://www.typeform.com/help/data-api/
$tfApiKey = 'XxXxXxXxXxXxXx';

// set the id of desired typeform form ID 
$tfUID = '2THATZl';

// set the base API URL
$tfBaseAPIUrl  = 'https://api.typeform.com/v1/form/';

// set taret url
$targetUrl = $tfBaseAPIUrl.$tfUID.'?key='.$tfApiKey.'&completed=true';


$responseArr = Typeform('get',$targetUrl);

$response = json_decode($responseArr,true);

$displayData = '';

if($response['http_status'] == 200){

//echo sizeof($response['questions']);

$ids = array();

foreach($response['questions'] as $queData){

	$ids[] = $queData['id'];

	}

foreach($response['responses'] as $resData){

	echo 'Name: '.$resData['answers'][$ids[0]].'<br>';
	echo 'Email: '.$resData['answers'][$ids[1]].'<br>';
	echo 'Phone: '.$resData['answers'][$ids[2]].'<br><br>';

	$userExist = checkUserExistence($resData['answers'][$ids[1]],$resData['answers'][$ids[2]]);

	if($userExist < 1)
		$displayData .= createPerson($resData['answers'][$ids[0]],$resData['answers'][$ids[1]],$resData['answers'][$ids[2]]);

}

}

echo $displayData;

?>

You now have a fully integrated Typeform to CCB tutorial that allows for you to customize the user experience of your survey form anyway you would like, yet collect your data in CCB.

Typeform, checkUserExistence and createPerson Functions

If you’re integrating the following functions with CCB, then don’t forget to include the functions code below in the general.php file.

<?PHP

/**************************************************************
** FUNCTIONS
**************************************************************/

function Typeform($type,$targetUrl,$personData=NULL){

// set global variables
global $tfApiKey, $tfBaseAPIUrl;

// set error checking of global variables. if any are empty, die on the spot and display error message to web browser
if (!$tfApiKey || !$tfBaseAPIUrl) die('Error: Typeform API details missing.');

$apiUrl = $targetUrl;
 
$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($ch, CURLOPT_USERPWD, "username:$mcApiKey");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,2); 
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

if($type == 'post'){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $personData);
}

if($type == 'put'){
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($personData)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $personData);
}

$contents = curl_exec($ch);

if(!$contents){
	$contents =  'Error: '.curl_error($ch);
}
 
curl_close($ch);
 
return $contents;

}

/******************************************************************************************/
// function to create a person in CCB using info from Typeform form
/******************************************************************************************/

function createPerson($uname,$uemail,$uphone){

    $campusid = 1;
    $familyid = '0';
    $familyposition = 'c';
    $membershiptype = 3;
    $uname = explode(" ", $uname, 2);
    $user_firstname = $uname[0];
    $user_lastname = $uname[1];
    $contactphone = $uphone;
    $emailaddress = $uemail;

    $urlData = array_filter( array(
    'srv' => 'create_individual',
'family_id' => "$familyid",
'campus_id' => "$campusid",
'family_position' => "$familyposition",
'first_name' => "$user_firstname",
'last_name' => "$user_lastname",
'email' => "$emailaddress",
'contact_phone' => "$contactphone",
'membership_type_id' => "$membershiptype"
    ) );


$rss = ccbDetails('post',$urlData); // transform to XML

$nodes = $rss->xpath('//individuals/individual');  // xpath for individuals->individual

$response_object = 0;

$theCount = count($nodes);

$response_object = $theCount;

if( $theCount > 0){

    foreach ($nodes as $node){

        if($node->family_position != 'Child'){

            //$response_object .= $node->full_name.' ('.$node['id'].')'."<br/>";  // now print the person's full name and age (See CCB API documentation for more $apiService fields)
            $response_object = $node['id'];
        } 

    }

}

return $response_object;

}


/******************************************************************************************/
// function to check user's existence in CCB
/******************************************************************************************/

function checkUserExistence($uemail,$uphone,$utype=NULL){

//check phone number
//check email
//check both

$apiService = 'individual_search'; // CCB api service


switch($utype){

case 'phone':
$urlData = array_filter( array(
    'srv' => "$apiService",
    'phone' => "$uphone"
    ) );
break;

case 'email':
$urlData = array_filter( array(
    'srv' => "$apiService",
    'email' => "$uemail"
    ) );
break;

default:
$urlData = array_filter( array(
    'srv' => "$apiService",
    'email' => "$uemail",
    'phone' => "$uphone"
    ) );
break;

}

$rss = ccbDetails('get',$urlData); // transform to XML

$nodes = $rss->xpath('//individuals/individual');  // xpath for individuals->individual

$response_object = 0;

$theCount = count($nodes);

//$response_object .= $theCount;

if( $theCount > 0){

    foreach ($nodes as $node){

        if($node->family_position != 'Child'){

            //$response_object .= $node->full_name.' ('.$node['id'].')'."<br/>";  // now print the person's full name and age (See CCB API documentation for more $apiService fields)
            $response_object = $node['id'];
        } 

    }

}

return $response_object;

}

?>

 

Related Posts

Subscribe and receive the following...

  • Inside CCB tips and tricks
  • Instant CCB tutorial alerts and updates
  • CCB How To's, Videos, Webinars and more...