How To Search For Individuals

We’ve learned how to create individual profiles and how to parse CCB API XML response in our latest tutorials.

Next, we’ll learn how to search for individuals using the CCB CPI.  Of course, coming from someone who has master this tutorial, searching for individuals via the CCB API is quite simple.

We’ll build upon the lessons and takeaways you’ve learned up until now.  This tutorial will guide and instruct you as I show you how to build a reusable PHP/CURL function for sending (POST) and receiving (GET) data.  We’ll cover the function a bit later.

But if you’re ready to get started, then so am I!  Let’s start…

Prepping the essential variables

As we’ve done before in previous tutorials, create a PHP file to work within.  Today’s file will be name search-individuals.php.

As you have discovered from previous tutorials, variables are very much the lifeline of any php code or application you’ll create.  They make the code more manageable in the event you need to make a change.  Instead of changing code 100 different places, you essentially change the variable.  One time, one place, and it propagates throughout the application.

So, let’s create a churchdomain, apiUsername, apiPassword and baseAPIUrl.  We’ve used each of these variables with the exception of baseAPIUrl.  Going forward, you’ll want to always make sure you include a baseAPIUrl variable, so that you don’t have to concern yourself with remembering a long URL address and having to repetitiously typing it throughout your applications.  In short, using the baseAPIUrl variable will save you time.  Just trust me on this one.

Continuing right ahead, the baseAPIUrl variable is the url address to your CCB application’s API.  You’ll notice that the baseAPIUrl is comprised of the churchdomain variable.  It also allows you to send data known as a POST method and receive data known as a GET method.

Setting API Service and Service Details

Moving right along, we’re not ready to set our apiService variable and the urlData variable that contains our service details.  Since we’re searching for individuals, you’ll set the apiService variable to use the individual_search API service.

Although there are numerous search fields we could used to find individuals using the CCB API (i.e., first name, last name, phone, email, address, city, state, zip – see CCB API Documentation for full list of searchable parameters), we’ll narrow our selection for this tutorial to search for the last name Brown.  As shown below, set the lastname variable to Brown.

Next, you’ll prepare the urlData variable, passing and binding the apiService and lastname variables to their respective key fields of srv and last_name as an array using the array_filter php function without a callback.

The array_filter function is use to remove empty or null values from an array.  We don’t want to pass key fields with no data to CCB API.  For one, it doesn’t make sense to send empty variables, and secondly, it just don’t make sense. 🙂  Simply put, if there is not a thing to send, then don’t send.

Time for a bit of functional magic

Ok, so I told you when we began this tutorial that I would introduce you to functions and why you should start using them ASAP.  Functions will make your life easier.

Think of a function as something equivalent to a cookie cutter.  If you had to make star cookies by hand, it would take you forever to create 100 star cookies as opposed to having a pre-cut star design to use to create the same 100 star cookies.  Well, that’s what creating a function will do for you.

There are two types of functions: built in and user defined.  We’ve used a few built in functions along the way.  But for this tutorial we’ll create our first user-defined function.  This function is going to help save you time, keystrokes and make for a much more easy and manageable codebase.  So let’s get started with the creation of our PHP/CURL function for sending and receiving data using the CCB API.

First, when creating a user-defined function, you’ll want to start or declare the function using… wait for it… function.  Afterwards, add a space after function and give your function a ver descriptive name.  Function names can contain letters and underscores, but should never contain numbers.  A function can also accept or be passed a wide number of arguments to be used within the function itself much like variables.

I’m not going to go into great detail about function as there are numerous websites to read from and on more about PHP User-Defined Functions.

For this tutorial, we’ll create a xml_response variable and set it equal to our function ccbDetails, passing it the urlData variable as the first argument and a second argument of either post or get.  For now, set the second argument to get.  When working with the various CCB API services, you’ll have to review the API documentation to know when to use post or get when making calls.  Now that your xml_response variable is set, let’s construct your ccbDetails function, and assign it paramdata and posttype arguments as shown below.

Continuing on, we are now ready to focus on the logic within the ccbDetails function now that we have set a variable using our ccbDetails function, and passed to it the urlData variable as the first argument and the ‘get’ string as the second argument.

To be able to make a successful CCB API call, we’ll need to access variables outside of our function.  Variables that are set inside functions are known as local variables while variables outside of the function are global variables.  We’ll need to access global variables apiUsername, apiPassword and baseAPIUrl.

In addition, we’ll add a little error checking into the function to ensure that the global variables are set before we begin trying to move the world to find and search for individuals.  If any of the global variables are empty, the function will stop and die, rendering the following error message via web browser to alert the user: “Error: CCB API details missing.”.

Next, we’ll clean our paramdata variable one more time using array_filter function and reset the paramdata variable to itself.  Input data can never be too clean, and coding 101 says to never blindly accept anything from the client browser as the truth! 🙂

Afterwards, we are now ready to set the query_string variable, and build the encoded web URL string using the http_build_query function with the paramdata variable as the argument.

Now you’re ready to create the url variable for when using a CCB API service that uses the get method to retrieve, concatenating the baseAPIUrl to a ? string and then to the query_string variable.  If making a CCB API call using a post method, you’ll see a php if statement to account for the posttype being equal to post.  When this happens, we create a different url variable that only concatenates the baseAPIUrl to a ? string, and not the query_string.

For the next part of the ccbDetails function we’ve created, I’m not going to give great detail on it other than to say that you’ll need to read up on php curl and the curl_setopt options.

In short, we’re now ready to take the appropriate get or post url variable, make a PHP/CURL call comprised of curl_init(), curl_setopt() and curl_exec() options,  and set a response_object variable equal to the PHP/CURL return content.  Finally, end the function with a return statement that returns the response_object variable.

Congratulations! You now have created your first PHP user-defined function: ccbDetails.  But we’re not done.  We have a few more lines of code to complete and wrap up this tutorial up with a pretty red bow.

Before we started and completed the ccbDetails function, we last set the xml_response variable, so we’ll pick back up at that point to head towards the finish line.

The xml_response variable needs to be transformed to XML using SimpleXMLElement.  Set the rss variable for XML transformation. If you were to print or echo the rss variable, the XML structure of the data return when we search for individuals with the last name equal to Brown would look like the following example below.

I changed the individual attribute from id to age.  Don’t forget to change ‘age’ to ‘id’.  CCB API does not return the age.  I only used ‘age’ as an example.

For the sake of space, I’ve truncated the transformed XML response structure a bit, removing a good bit of the fields.

We’re now ready to parse the XML response using an XPATH query and foreach statement to return a bulleted list of individuals with their full names and age in parenthesis.

  • Alvin Brown (34)
  • Mallary Brown (31)
  • Ian Brown (1)
  • Seth Brown (0)

And that’s it! You’ve made it to the end of this tutorial that taught you how to search for individuals using the CCB API.   Now let’s piece together all of the codebase in its entirety into your search-individuals.php file, so that you can go test your newly found technical prowess.

This has been a very intense tutorial, and hats off to those of you who have made it this far, and have been able to execute the tutorial code and view live data from your CCB.

By the way, I deem this tutorial as the long way to grandma’s house. 🙂  Yes, there is an easier and more effective method that doesn’t consume hundreds of lines of code.

In upcoming tutorials, I’ll show you how to bypass most of this codebase, condensing it to only a few lines of code.  But for the sake of the various types of CCB API projects you may find yourself encountering, I wanted you to get familiar with a few additional PHP concepts. Well, that’s it for now!  Cheers!


Please comment if this tutorial has helped you.