Welcome to the exciting world of MSP API authentication using PHP! Whether you’re a coding ninja or just starting your coding journey, this tutorial will easily guide you through the process of authenticating the MSP REST API.
PHP and MySQL have been trusted technologies for decades, powering entities of all sizes, from small businesses to global conglomerates and churches like yours. With their versatility and power, integrating APIs like MSP becomes a breeze.
Before diving into authentication, ensure you’ve reviewed the Getting Started page to familiarize yourself with the basics. Once you have a text editor and a local or public-facing PHP and MySQL development environment set up, you’re ready to embark on your authentication journey.
Define MSP API Variables.
First, let’s define the essential MSP API variables:
$mspAPIKey
: This is where you’ll enter your unique MSP API Key. It’s the key to unlocking the API’s potential.$apiBaseUrl
: The base URL for the MSP API, typically “https://api.ministryschedulerpro.com/2/“.$endPoint
: This is where you’ll enter the desired API endpoint. Check the MSP Collections and Endpoint page for the available options.$url
: Combine the$apiBaseUrl
and$endPoint
to create the complete URL, such as “https://api.ministryschedulerpro.com/2/volunteers/list” as an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?PHP /*********************************** **** **** auth.php **** **** This file allows for authentication using your MSP API Key. **** ***********************************/ // define MSP API variables $mspApiKey = 'Enter key area'; // Your MSP API Key $apiBaseUrl = "https://api.ministryschedulerpro.com/2/"; // MSP API base URL $endPoint = 'Enter API Endpoint'; // MSP API Endpoints // if the endpoint is not defined and still equals the default of `Enter API Endpoint`, then create an empty $endpoint variable. if($endPoint == "Enter API Endpoint");{ $endPoint = ''; } // although commented out below, all MSP API endpoint query parameters will be defined as multidimensional array key=>value pairs. //$data = array("Enter array key" => "Enter array value","Enter array key" => array ("Enter array key" => "Enter array value" ) ); // MSP API uses JSON objects as a means of exchange for all endpoints; therefore, json_encode the PHP $data variable value. //$postData = json_encode($data); $url = $apiBaseUrl.$endPoint; ?> |
Define MSP API Key Variable.
Next, let’s focus on the MSP API Key variable, a crucial element in every API request. The MSP API authentication utilizes a basic auth username value that is base64 encoded.
Luckily, your MSP API Key is already encoded. Use the following lines of code for actual authorizations and authentication of MSP REST API:
1 2 3 4 5 |
// set your MSP API Key. Also uncomment the content type if you are attempting to instantiate a request using an MSP API endpoint. $header = array( "Authorization: Bearer $mspApiKey" //,"Content-Type: application/json" ); |
Now it’s time to authenticate the MSP REST API using cURL. Don’t worry if you’re unfamiliar with cURL. It’s a handy built-in PHP library that lets you send and receive data over HTTP/HTTPS and FTP.
To make the cURL request for authentication, pass the $url
and $header
variables as arguments to the appropriate cURL settings. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
//Time to initiate cURL request using MSP API variables $ch = curl_init(); $timeout=60; //set the url and other options for curl curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE //curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); // uncomment if you are using MSP API endpoint query parameters -- see lines 24-30 //curl_setopt($ch, CURLOPT_POST, true); // uncomment if the previous line is uncommented. ;) curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //execute call and return response data. $result = curl_exec($ch); //close curl connection curl_close($ch); |
Congratulations! You’ve successfully made the cURL request to authenticate the MSP REST API using PHP. To access the response data, use json_decode
since the MSP API returns JSON objects.
1 2 3 4 5 6 |
// decode the json response $rez = json_decode($result, true); var_dump($rez); ?> |
Remember, this tutorial focuses on simplicity, so error checking has been omitted. But don’t worry, we’ll cover error handling in more advanced tutorials.
Feel free to reach out if you have any questions or encounter technical roadblocks along the way. Now, it’s time to unlock your wildest MSP API integration dreams and turn them into reality!
Happy coding!
P.S. If PHP isn’t your cup of tea, then by all means, please review MSP REST API using Python. 😉
Entire Codebase for MSP REST API Using PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?PHP /*********************************** **** **** auth.php **** **** This file allows for authentication using your MSP API Key. **** ***********************************/ // define MSP API variables $mspApiKey = 'Enter key area'; // Your MSP API Key $apiBaseUrl = "https://api.ministryschedulerpro.com/2/"; // MSP API base URL $endPoint = 'Enter API Endpoint'; // MSP API Endpoints // if the endpoint is not defined and still equals the default of `Enter API Endpoint`, then create an empty $endpoint variable. if($endPoint == "Enter API Endpoint");{ $endPoint = ''; } // although commented out below, all MSP API endpoint query parameters will be defined as multidimensional array key=>value pairs. //$data = array("Enter array key" => "Enter array value","Enter array key" => array ("Enter array key" => "Enter array value" ) ); // MSP API uses JSON objects as a means of exchange for all endpoints; therefore, json_encode the PHP $data variable value. //$postData = json_encode($data); $url = $apiBaseUrl.$endPoint; // set your MSP API Key. Also uncomment the content type if you are attempting to instantiate a request using an MSP API endpoint. $header = array( "Authorization: Bearer $mspApiKey" //,"Content-Type: application/json" ); //Time to initiate cURL request using MSP API variables $ch = curl_init(); $timeout=60; //set the url and other options for curl curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE //curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); // uncomment if you are using MSP API endpoint query parameters -- see lines 24-30 //curl_setopt($ch, CURLOPT_POST, true); // uncomment if the previous line is uncommented. ;) curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //execute call and return response data. $result = curl_exec($ch); //close curl connection curl_close($ch); // decode the json response $rez = json_decode($result, true); var_dump($rez); ?> |