Welcome back to this week’s tutorial. My hope for you is that these tutorials have saved you and your church time while increasing operational productivity beyond measure.
Although it’s a labor of love, I truly enjoy helping churches bridge their technical gaps.
Speaking of technical gaps, the technical gap we’ll focus on bridging today is updating and managing group leaders using the manage_group_leaders CCB API service.
One thing to note about this tutorial is the main group leader WILL NOT be allowed to be changed through this service.
And should a main group leader’s id be included in the list of leader_ids, then it will be ignored when leaders are assigned.
Establishing Service and Required Parameter Variables
Open a text editor of your choice. Name and save the file as the following php file: manage-group-leaders.php.
As previous tutorials, be sure to include comments and the config.php file containing reusable globals and functions.
Immediately following these lines, create and define the apiService variable to be equal to the CCB API service for managing group leaders: manage_group_leaders.
After defining the apiService variable, the next variables to be defined are required fields that MUST be defined when managing group leaders.
The following variables, required parameters, can be found listed in CCB documentation pertaining manage_group_leaders API service:
- group_id (integer)
- leader_ids (array of integers)
Both variables to be defined, groupId and leaderIds, contain integer values as shown below.
The groupId variable value is a single integer while the leaderIds variable value is an array of integers.
<?PHP /** * manage group leaders */ // contains reusable globals and functions include("includes/config.php"); $apiService = 'manage_group_leaders'; // CCB api service // manage group leaders per CCB required and optional parameters // REQUIRED PARAMETERS $groupId = 24; // integer $leaderIds = array(5,7,9); // array of integers ?>
If you need assistance obtaining the group id and leader ids, then please refer to this previous tutorial.
Binding Labels and Variables to Make API Call and Parse Response
Before executing a CCB API call to retrieve data, defined variables and their respective API labels must be defined and bound together.
To bind variables and labels respectively, create a urlData variable, setting it equal to an encapsulated
key-value pair array using PHP’s built-in array_filter method.
Remember to include the binding of the srv label to the apiService variable, a step to not be overlooked.
Once labels and variables are bound accordingly, the CCB API call can be instantiated using the ccbDetails function.
Set the ccbDetails functions equal to the value of the rss variable, ensuring to pass get as the first argument and urlData as the second argument of the ccbDetails function.
<?PHP $urlData = array_filter( array( 'srv' => "$apiService", 'group_id' => "$groupName", 'leader_ids' => "$leaderIds" ) ); $rss = ccbDetails('get',$urlData); // transform to XML ?>
Ready to parse and display leader data
The CCB API can has been successfully executed. Now, it’s time to parse the XML response and prep the data for display via the web browser.
To parse the group leader data in the XML response, use PHP’s built-in XPATH query method and following expression: //leaders/leader.
Define a nodes variable and have it’s value be the rss variable pointing to the aforementioned path query expression.
Once the response data has been appropriately transformed, the nodes variable can be printed to the web browser using PHP’s built-in print_r method.
This method allows for arrays to be printed and displayed to the screen. When printed, you should be shown each leader’s full name and id.
<?PHP $nodes = $rss->xpath('//leaders/leader'); // xpath for managing group leaders print_r($nodes); ?>
Test and Display Your Technical CCB API Prowess
The time has come to save the manage-group-leaders.php file and execute the file a web browser of your choice.
Your code should look the following:
<?PHP /** * manage group leaders */ // contains reusable globals and functions include("includes/config.php"); $apiService = 'manage_group_leaders'; // CCB api service // manage group leaders per CCB required and optional parameters // REQUIRED PARAMETERS $groupId = 24; // integer $leaderIds = array(5,7,9); // array of integers $urlData = array_filter( array( 'srv' => "$apiService", 'group_id' => "$groupName", 'leader_ids' => "$leaderIds" ) ); $rss = ccbDetails('get',$urlData); // transform to XML $nodes = $rss->xpath('//leaders/leader'); // xpath for managing group leaders print_r($nodes); ?>
Once executed, the web browser should display a multi-dimensional array of group leader information you submitted for creation.
I hope this tutorial has helped you and moved your further down the line of understanding and using the CCB API.
Let me know if you have any questions or comments. Nevertheless, I’ll see you back here for next week’s tutorial.