Welcome back and I hope you’re ready to extend your CCB tutorial knowledge.
Today’s tutorial focuses on leverage CCB’s form_list API service.
There comes a point in time when your church is likely needing to access a list of forms via another system external to CCB.
If you’re church contains most everything within CCB, then you’ll likely not find this tutorial that beneficial.
However, if your church makes use of 3rd party tools and technologies for event resource management beyond CCB capabilities, then this tutorial is for you.
Let’s get started with retrieving a list of forms.
Setup Variables and Includes
This tutorial is fairly simple, so I won’t bore you with all the setup, comment, and includes details. Feel free to read previous tutorials or start here.
Go ahead and open a text editor of your choice, naming and saving the file as form-list.php.
The main variable to define is the apiService variable. It’s value should be set or defined equal to form_list as shown below.
<?PHP /** * Form List */ // contains resusable globals and functions include("includes/config.php"); $apiService = 'form_list'; // CCB api service ?>
One thing to note about the form_list API service is that it has no required fields, but it does have the following optional fields to be passed as parameters when making CCB API call:
- form_id (integer) – the id of a specific form
- campus_id (integer) – the id of a specific campus
- modified_since (string[20]) – the date since a form was last modified
- include_archive (boolean) – whether or not to include archived forms
Review CCB’s Public Web API documentation for more in depth details and review of service in its entirety.
Making the API call and preparing response data
We’re almost ready to initiate or execute our CCB API call. But before we do, the variables defined in the last section are in need of binding to the appropriate API parameter labels (if optional parameters applicable).
To bind labels and variables, create a urlData variable and it is equal to an array that binds the following label(s) to their respective varaible(s).
This binded is a key-value pair array encapsulated using PHP’s built-in array_filter method.
- srv label binds to apiService variable
Don’t forget to bind optional parameters should you make use of them.
Create a rss variable and set it equal to the ccbDetails function. Now it’s time to pass the urlData function as second argument of the ccbDetails function. Don’t forget the first argument value, which should be get.
To learn more about ccbDetails function magic, click here.
<?PHP $urlData = array_filter( array( 'srv' => "$apiService" ) ); $rss = ccbDetails('get',$urlData); // transform to XML ?>
Parsing and display form_list data
At this point, you’ve successfully created the CCB API call to retrieve the data for form_list API service.
Now, it’s time to parse the API call response data using PHP’s built-in XPATH query method and the following expression: //items/form.
Define a nodes variable, setting it equal to the rss variable pointed to the xpath query of the expression in the previous sentence.
Next, define an empty response_object variable to be used to display the form_list data from the API call response data.
<?PHP $nodes = $rss->xpath('//items/form'); // xpath for forms $response_object = ''; ?>
Next, let’s create a foreach statement to iterate through each collected form object.
Within the foreach statement, let’s apply a bit of logic checking. Without this logic, CCB will return ALL forms.
Review CCB’s documentation, specifically the form_list API service, for all fields returned when executing API call to narrow and reduce time to retrieve and display data.
Yes, it can be a long API call wait time waiting for ALL forms to be returned.
In fact, I encourage you to place data in a database table if you plan on using within a web app or website.
If you need database help, then review the following tutorials: MySQL DB/Tables and Church Dashboard.
Both tutorials help to get acclimated with MySQL database and table schema as well as overall database interactions.
Nevertheless, notice how I use if statement logic to ensure that ONLY forms marked public and published are going to be display.
Within the if statement, I’ve redefined the response_object variable to equal a string of concatenated fields and html, using the “.=“ operator to concatenate each form response.
Finally, close both the if and foreach statements, and then echo the response_object variable as the last step. That’s it! It’s time to save and test.
<?PHP foreach ($nodes as $node) { if($node->public == true && $node->published == true){ $response_object .= '<p><a href="'.$node->url.'">'.$node->title.'</a></p>'; } } echo $response_object; ?>
Time to test your tech prowess
Let’s not waste any more time. Your code should look like the following:
<?PHP
/**
* Form List
*/
// contains resusable globals and functions
include("includes/config.php");
$apiService = 'form_list'; // CCB api service
$urlData = array_filter( array(
'srv' => "$apiService"
) );
$rss = ccbDetails('get',$urlData); // transform to XML
$nodes = $rss->xpath('//items/form'); // xpath for forms
$response_object = '';
foreach ($nodes as $node)
{
if($node->public == true && $node->published == "true" && $node->status == "Available"){
$response_object .= '<p><a href="'.$node->url.'">'.$node->title.'</a></p>';
}
}
echo $response_object;
?>
Save your file and open it using a web browser.
You should see something that looks like the following page below (a list of text links that open’s a new browser tab to respective CCB form).
There’s so much you could do with this tutorial in regards to extending its use.
For example, you could create the following:
- A custom church kiosk that displays the last events to register for
- A customer registration calendar via website or mobile app
The sky is the limit with what you can do with this API service and other API services.
Let your imagination run wild, pushing and taking the CCB API limits to the next level.
That’s it for today’s tutorial. Let me know if you have any questions. I look forward to seeing you back here for the next tutorial.
Have a blessed week!