Today’s tutorial is pretty simple and easy to say the least.
At some point in the near future, you’ll find yourself wanting or needing to know the total number of CCB profiles for your account. And today’s your lucky day, because I’m going to show you how to retrieve that very number.
Let’s get started!
You’ll want to open your text editor, and create and save a file named total-number-ccb-profiles.php. Be sure to add the header comment, including the page name, and then include the config.php file found in your includes directory.
You are now ready to set the apiService variable, making it equal to the API service individual_profiles.
I’ll pause here to let you know that you can add additional individual_profiles fields to limit the number that is returned if you need too. For the sake of this example, I’ll not use those additional service parameters. You can find out more about individual_profiles additional parameters by reading the CCB API Documentation.
<?PHP /** * total-number-ccb-profiles.php */ // contains resusable globals and functions include("includes/config.php"); $apiService = 'individual_profiles'; // CCB api service ?>
Binding and prepping the URL data for CCB API call
You are now ready for binding the variables and their respective values to their respective service API labels. As performed in previous tutorials, create a urlData variable and assign it an array of binded service API labels and variables encapsulated in the array_filter function as shown below.
Okay, so now on forward to using our good ole’ ccbDetails function and passing it the urlData variable. So create a rss variable and assign it the ccbDetails function, having the first argument passed as get and the second argument passed as urlData.
And if you’re in the dark on the ccbDetails function, do have a review of the tutorial: Creating a reusable function for CCB API calls. In short, this function will return an XML response.
<? $urlData = array_filter( array( 'srv' => "$apiService" ) ); $rss = ccbDetails('get',$urlData); // transform to XML ?>
Parsing the XML Response
Okay, so you’re getting real close to the end of this tutorial. One of the final steps to take is parsing your XML response to retrieve the total number or count of CCB profiles in your church’s CCB account.
As performed in previous tutorials, you’ll want to use a XPATH query to parse your XML Response. To parse this tutorial’s response, use //individuals as your XPATH expression. Create a nodes variable and assign it the rss variable that is referencing the XPATH query and expression //individuals.
Afterwards, you’ll want to create a foreach statement to iterate through each nodes variable array response. In this instance, since we are only looking for the total number of CCB profiles, we’ll only have one node array response to perform a foreach iteration.
Dont’ forget to set the response_object equal to the node[‘count’] data which will contain the total number of CCB profiles. The final step is to echo the response_object to the web browser.
<?PHP $nodes = $rss->xpath('//individuals'); // xpath for individuals $response_object = ''; foreach ($nodes as $node) { $response_object .= 'Total Count: '.$node['count']; // now print the total count of CCB profiles (See CCB API documentation for more $apiService fields) } echo $response_object; ?>
Time to make these scripts one
Okay, let’s assemble this code and test away. This tutorial was a bit simple, but I’ll expand on it in the upcoming weeks to show you how to create a backup of your entire CCB account using PHP and MySQL. And not only that, I’ll show you a few other automation tips and tricks to make your CCB account allow you to work smarter and not harder.
<?PHP /** * total-number-ccb-profiles.php */ // contains resusable globals and functions include("includes/config.php"); $apiService = 'individual_profiles'; // CCB api service $urlData = array_filter( array( 'srv' => "$apiService" ) ); $rss = ccbDetails('get',$urlData); // transform to XML $nodes = $rss->xpath('//individuals'); // xpath for individuals $response_object = ''; foreach ($nodes as $node) { $response_object .= 'Total Count: '.$node['count']; // now print the total count of CCB profiles (See CCB API documentation for more $apiService fields) } echo $response_object; ?>