Today’s tutorial is a bit simple in nature and geared towards processes, process managers to be exact.
This week, I’ll share with you how to use the CCB API process_managers service to retrieve a full list of process managers and the total number of processes managed.
I won’t dive into the details of displaying each process managed by their respective process manager. You’ll have to wait until next week’s tutorial or the following week.
Or perhaps, you’ll get a bit curious and courageous to figure how out how to display each process managed by their respective process manager. It’s really nothing to it, but to do it.
Speaking of doing and getting things done, let’s get started with today’s tutorial. 😉
Variable and Comment Includes Setup
Using the text editor of your choice, create and save the following php file: process-managers.php.
Be sure to include the comments and file inclusions as shown below.
If you’re just joining us for the first time, then feel free to review previous tutorials for more in-depth information about including the config.php file.
Now that you’ve commented and included the necessary files, it’s time to move forward with variable definition.
Create and define the apiService variable with a value equal to the following CCB API service: process_managers.
<?PHP /** * Process Managers List */ // contains resusable globals and functions include("includes/config.php"); $apiService = 'process_managers'; // CCB api service //$personId = 3224; // specific person's id ?>
Notice that I included a commented line two lines below for the personId variable as shown below. This is an optional parameter for the CCB API process_managers service.
- id is the parameter used to return a specific person as process manager
Using the personId variable allows for you to retrieve a single person while not using it retrieves everyone.
So, please be mindful to read CCB’s API Documentation and be well aware of what your technical requirements are to reduce API call overhead.
For this tutorial, I chose to not use the personId variable and return ALL process managers listed within CCB.
Time to Make API Call and Parse Response
Before the CCB API call be executed, variables and their respective API labels must be defined and bound together.
I’ve included a commented out line for the personId variable that you can uncomment should you want to only return a specific process manager using the individual id.
To bind variables and labels respectively, create a urlData variable and set it equal to an encapsulated key-value pair array using PHP’s built-in array_filter method.
Don’t forget to include the binding of the srv label to the apiService variable. This is a very important step that must not be overlooked.
Once labels and variables are bound accordingly, the CCB API call can be instantiated using the ccbDetails function.
Set the ccbDetails function as the value of the rss variable, ensuring to pass get as the first argument and urlData as the second argument of the ccbDetails function.
If you’re in question about the ccbDetails function, read a detailed overview of the ccbDetails function and all it’s PHP glory.
<?PHP $urlData = array_filter( array( 'srv' => "$apiService" //,'id' => $personId ) ); $rss = ccbDetails('get',$urlData); // transform to XML ?>
Making ready for parsing and display data
The CCB API call has been successfully made. Now what?
It’s time to parse the XML response and prep the data for display via the web browser.
To parse the process managers in the XML response, we’ll use PHP’s built-in XPATH query method and the following expression: //managers/manager.
Define a nodes variable and have it’s value be the rss variable pointing to the aforementioned xpath query expression.
After this, it’s time to define an empty response_object variable that is used to capture parsed data points to the browser for each process manager.
<?PHP $nodes = $rss->xpath('//managers/manager'); // xpath for resources $response_object = ''; ?>
At this point, you now have an empty response_object variable and a nodes variable that contains ALL of the process managers and their respective data in a multi-dimensional PHP array.
To parse each manager and their respective data to the browser, create a foreach statement to iterate over the the nodes variable as shown below.
With the foreach statement, redefine the response_object variable to a string of concatenated files and html that use the “.=“ operator to concatenate each process manager and their respective process attributes.
In addition, I’ve commented out variables that you may or may not choose to use.
In this tutorial, I’ve opted to show the person’s id, person’s name, and the total number of processes they manage using PHP’s built-in sizeof function.
As I stated at the beginning, I’ll show you next week how to modify the code to return each process for each process owner in a bulleted list.
For now, close the foreach statement and don’t forget to echo the response_object variable. Should you forget to echo, then your screen will be as blank or white as a sheet of paper.
<?PHP foreach ($nodes as $node) { $response_object .= $node['id'].' - '.$node->name.' (Number of Processes Managed: '.sizeof($node->processes->process).')<br>'; /* $node->processes // collective grouping of processes $node->processes->process // each individual process name */ } echo $response_object; ?>
Bring it all home and together for testing
That’s all there is to retrieving a list of process managers and their respective number of processes managed.
I told you that today’s tutorial was rather easy. We’ll go a bit deeper next week and modify the codebase in its entirety.
Save the following code in your process-managers.php file and open, rather execute, the same file via a web browser:
<?PHP /** * Process Managers List */ // contains resusable globals and functions include("includes/config.php"); $apiService = 'process_managers'; // CCB api service //$personId = 3224; // specific person's id $urlData = array_filter( array( 'srv' => "$apiService" //,'id' => $personId ) ); $rss = ccbDetails('get',$urlData); // transform to XML $nodes = $rss->xpath('//managers/manager'); // xpath for resources $response_object = ''; foreach ($nodes as $node) { $response_object .= $node['id'].' - '.$node->name.' (Number of Processes Managed: '.sizeof($node->processes->process).')<br>'; /* $node->processes // collective grouping of processes $node->processes->process // each individual process name */ } echo $response_object; ?>
Without any errors, you should see the following display or something like it.
Keep it mind that it may take some time to execute the code, depending on the number of process managers and processes you have within your CCB account.
If you want to test a single person, then uncomment the personId variables in their respective sections (see previous sections).
Well, that’s it for today. Let me know if you have comments or questions. Until we meet again next week, be blessed!