Good day my fellow CCB Tutorials community! Yet another week has flown by and it’s time for a new tutorial.

Today’s tutorial covers how to access CCB form detail using the CCB API service.

There are moments that you may want to access certain form details to create your very own form.

In fact, there are many of you that use Wufoo, Typeform, and other form services that allow for responsive, user-friendly designs.

And if you haven’t stumbled upon those tutorials, I’ve provided tutorials for both Wufoo and Typeform.

Nevertheless, I’ll teach you how to use the CCB API service to access form information so that you can use the detail on your website or in other applications in use across your church.

Setting Up Variables, Comments and Inclusion Files

Open a text editor of your choice, saving and naming a PHP file the following: form-detail.php.

Be sure to include the necessary comments with the appropriate file inclusion of the config.php.

Once completed, then define the following variables and their respective values:

  • apiService – required API parameter that expects CCB API service value of form_detail
  • formId – required API parameter that expects an integer value

If you’re in search of a specific form’s id, then review how to find a person’s id.

Although different objects, both form and person ids function the same for their respective objects.

<?PHP

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

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

$formId = 24; // sample field to search for (See CCB API documentation for more $apiService fields)

?>

Prepare and Make the CCB API Call

Once setup and variables have been defined with their respective values, data can be prepared to be passed as arguments to the ccbDetails functions to make the API call.

To prepare data, the defined variables must be bound to their respective CCB API parameter labels.

To bind data, define a urlData variable and assign it an array encased within PHP’s built-in array_filter method, binding the following to their respective variables and labels:

  • srv => apiService variable
  • id => formId variable

Once bounded, the urlData variable can be passed to the ccbDetails function as the second argument while get is passed as the second argument.

The ccbDetails functions is the value of the rss variable, which we’ll reference next.

Define the nodes variable equal to $rss->xpath function, using the following XPATH expression: //forms/form.

<?PHP

$urlData = array_filter( array(
	'srv' => "$apiService",
	'id' => "$formId"
	) );

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

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

?>

Parsing and Displaying Form Data Detail

You’re almost home free. Up to this point, you’ve setup and completed the necessary steps to successfully create and make the CCB API call to retrieve form detail data.

One the first steps to complete when parsing and displaying data is to create and set an empty response_object variable.

This response_object variable is used to capture data for display as I’ll soon show you.

To parse data, create a foreach statement that iterates the nodes variable that was defined in the previous section. The nodes variable contains the response from the form detail data.

Within the foreach statement, I’ve listed a number of available form detail data points to access. I capture the data points by concatenating each using the response_object variable.

Notice I’ve added “array” comments next to some of the parameters. To access the data for these parameters, use a foreach statement just as we did to access the first level data of the nodes variable.

Another thing to note: if you wanted to access and pass data along to another program, then you could simply pass the nodes variable to your application.

Finally, close the foreach statement, and echo the response_object variable should you want to display data to the web browser.

<?PHP

$response_object = '';

foreach ($nodes as $node)
{

	// now print form data (See CCB API documentation for more $apiService fields)
	$response_object .= $node["id"]; 
	$response_object .= $node->title;
	$response_object .= $node->description;
	$response_object .= $node->campus['id'];
	$response_object .= $node->campus;
	$response_object .= $node->confirmationText;
	$response_object .= $node->showConfirmationCode;
	$response_object .= $node->start;
	$response_object .= $node->end;
	$response_object .= $node->status;
	$response_object .= $node->public;
	$response_object .= $node->published;
	$response_object .= $node->disabled;
	$response_object .= $node->hasDiscountCodes;
	$response_object .= $node->creator['id'];
	$response_object .= $node->creator['value'];
	$response_object .= $node->creator;
	$response_object .= $node->modifier['id'];
	$response_object .= $node->modifier['value'];
	$response_object .= $node->modifier;
	$response_object .= $node->created;
	$response_object .= $node->modified;
	$response_object .= $node->profile['id'];
	$response_object .= $node->profile->title;
	$response_object .= $node->profile->explanation;
	$response_object .= $node->profile->fields; // array
	$response_object .= $node->profile->fields->field; // array, use a foreach statement to access specific data pertaining to contact information: contact phone, email, mobile, first and last name, etc.
	$response_object .= $node->questions; // array
	$response_object .= $node->questions->question; // array, use a foreach statement to access each question and it's respective attributes
	$response_object .= $node->attachments;
	$response_object .= $node->discountCodes;
	$response_object .= $node->paymentOptions; // array
	$response_object .= $node->paymentOptions->paymentOption; // array, use a foreach statement to access each payment option
	
}

echo $response_object;

?>

Ready to test your code?

It’s time to set your technical prowess in motion in testing your code. Here’s what your code should look like in it’s entire, keeping in mind that you may have slight modifications:

<?PHP

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

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

$formId = 24; // sample field to search for (See CCB API documentation for more $apiService fields)

$urlData = array_filter( array(
	'srv' => "$apiService",
	'id' => "$formId"
	) );

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

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

$response_object = '';

foreach ($nodes as $node)
{

	// now print form data (See CCB API documentation for more $apiService fields)
	$response_object .= $node["id"]; 
	$response_object .= $node->title;
	$response_object .= $node->description;
	$response_object .= $node->campus['id'];
	$response_object .= $node->campus;
	$response_object .= $node->confirmationText;
	$response_object .= $node->showConfirmationCode;
	$response_object .= $node->start;
	$response_object .= $node->end;
	$response_object .= $node->status;
	$response_object .= $node->public;
	$response_object .= $node->published;
	$response_object .= $node->disabled;
	$response_object .= $node->hasDiscountCodes;
	$response_object .= $node->creator['id'];
	$response_object .= $node->creator['value'];
	$response_object .= $node->creator;
	$response_object .= $node->modifier['id'];
	$response_object .= $node->modifier['value'];
	$response_object .= $node->modifier;
	$response_object .= $node->created;
	$response_object .= $node->modified;
	$response_object .= $node->profile['id'];
	$response_object .= $node->profile->title;
	$response_object .= $node->profile->explanation;
	$response_object .= $node->profile->fields; // array
	$response_object .= $node->profile->fields->field; // array, use a foreach statement to access specific data pertaining to contact information: contact phone, email, mobile, first and last name, etc.
	$response_object .= $node->questions; // array
	$response_object .= $node->questions->question; // array, use a foreach statement to access each question and it's respective attributes
	$response_object .= $node->attachments;
	$response_object .= $node->discountCodes;
	$response_object .= $node->paymentOptions; // array
	$response_object .= $node->paymentOptions->paymentOption; // array, use a foreach statement to access each payment option
	
}

echo $response_object;

?>

Save the file and open it via a web browser. The output likely looks like gibberish, but that’s only because we didn’t add line (i.e., </br>) or paragraph breaks (i.e., <p></p>) after each data point. You can add those in though, thank goodness.

That’s all for this week, but Lord willing, we’ll be back here with another tutorial next week.

Let me know if you have questions. Thanks and see you back here next week.

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...