How to Retrieve All Batches or Batches by Modified Date

Hello, CCB API coding friends! Grab your favorite beverage, sit back, and let’s tackle today’s API adventure.

We’re diving into the CCB (Church Community Builder) API to retrieve batch profiles.

Whether you’re a seasoned coder or someone figuring out what PHP even stands for (it’s Hypertext Preprocessor, by the way), this guide will walk you through every step. Let’s get started!

First things first, Dev Environment.

First, open your favorite code editor (VS Code, Sublime Text, Notepad++—whatever sparks joy). Create and save a new file as batch_profiles.php.

Done? Great! Let’s move on.

The Basics—Include and Define

Here’s where we set up the foundation of our script.

  1. Include your configuration file
    This file should house reusable globals and helper functions. (If you’re unsure how to create this, check out previous tutorials or holler for help.)
  2. Define the API service
    For this task, our API service is batch_profiles. This will tell CCB what we’re trying to access.
  3. Add optional parameters
    We’ll use the modified_since parameter to specify a date. If you skip this, the API will return all batches.

Here’s what that looks like in code:

<?php 

/** * Batch Profiles Script */ 

// Include reusable configs and functions 
include("includes/config.php"); 

// Define the API service 
$apiService = 'batch_profiles'; 

// Optional parameter: Modified date (YYYY-MM-DD) 
$modifiedDate = '2010-05-18'; 

?>

Prep for the API Call

Now, we bind our service and parameters into an array. For this, we’ll use PHP’s array_filter to ensure only non-empty values are included (just in case you leave the date blank).

<?php
// Create an array of key-value pairs for the API call
$urlData = array_filter(array(
'srv' => $apiService,
'modified_since' => $modifiedDate
));
?>

Time to make the API Call

This is the moment we’ve been waiting for—calling the API! We’ll use a custom ccbDetails function (defined in your config file) to handle sending the request and receiving the response. It’ll look something like this:

<?php
// Call the API and store the response
$rss = ccbDetails('get', $urlData);
?>

Time to parse the Response

The response comes back in XML format, so we need to parse it. Using PHP’s xpath method, we’ll extract all <batch> nodes. This makes it easy to loop through and display the batch data.

<?php
// Extract batch nodes using XPATH
$nodes = $rss->xpath('//batches/batch');

// Print the nodes (for debugging or displaying data)
print_r($nodes);
?>

And only if you’re curious, here’s an example of what the raw XML might look like:

<ccb_api>
<response>
<batches count="2">
<batch id="1">
<campus id="1">Main Campus</campus>
<post_date>2024-11-10</post_date>
<status>Open</status>
<!-- More data here -->
</batch>
<batch id="2">
<campus id="2">East Campus</campus>
<post_date>2024-11-11</post_date>
<status>Closed</status>
<!-- More data here -->
</batch>
</batches>
</response>
</ccb_api>

Time to Display the Results

Want to make the data user-friendly? Iterate over the nodes and display relevant fields like this:

<?php
// Loop through and display batch details
foreach ($nodes as $batch) {
echo "Batch ID: " . $batch->attributes()->id . "\n";
echo "Campus: " . $batch->campus . "\n";
echo "Post Date: " . $batch->post_date . "\n";
echo "Status: " . $batch->status . "\n";
echo "\n"; // Add spacing between batches
}
?>

Time to Test and Celebrate

Save your file and run it in a browser or terminal. If everything’s set up correctly, you’ll see the batch profiles displayed. 🎉 Here’s the full script for easy reference:

<?php
/**
* Batch Profiles Script
*/

// Include reusable configs and functions
include("includes/config.php");

// Define the API service
$apiService = 'batch_profiles';

// Optional parameter: Modified date
$modifiedDate = '2010-05-18';

// Create an array of key-value pairs
$urlData = array_filter(array(
'srv' => $apiService,
'modified_since' => $modifiedDate
));

// Call the API and store the response
$rss = ccbDetails('get', $urlData);

// Extract and display batch nodes
$nodes = $rss->xpath('//batches/batch');

foreach ($nodes as $batch) {
echo "Batch ID: " . $batch->attributes()->id . "\n";
echo "Campus: " . $batch->campus . "\n";
echo "Post Date: " . $batch->post_date . "\n";
echo "Status: " . $batch->status . "\n";
echo "\n";
}
?>

And there you have it! You’re officially a pro at fetching batch profiles with PHP and the CCB API. Wasn’t that fun? (Or at least less painful than debugging an undefined variable error?)

Keep coding and conquering, my friend. See you in the next tutorial!

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