PHPackages                             smadeira/ministry-platform-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. smadeira/ministry-platform-api

ActiveLibrary

smadeira/ministry-platform-api
==============================

PHP API wrapper for the Ministry Platform REST API

v5.1.0(3y ago)10610↓100%7[2 issues](https://github.com/smadeira/ministry-platform-api/issues)[1 PRs](https://github.com/smadeira/ministry-platform-api/pulls)MITPHPPHP &gt;=7.3.0

Since Mar 5Pushed 1y ago5 watchersCompare

[ Source](https://github.com/smadeira/ministry-platform-api)[ Packagist](https://packagist.org/packages/smadeira/ministry-platform-api)[ RSS](/packages/smadeira-ministry-platform-api/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (5)Versions (44)Used By (0)

Ministry Platform API Wrapper
-----------------------------

[](#ministry-platform-api-wrapper)

#### For Laravel 8 support you need to download v5.x of the API wrapper

[](#for-laravel-8-support-you-need-to-download-v5x-of-the-api-wrapper)

#### For dotenv 4.x support (including Laravel 7.x) you need to download v4.x of the API wrapper.

[](#for-dotenv-4x-support-including-laravel-7x-you-need-to-download-v4x-of-the-api-wrapper)

#### Note for v3: This API wrapper has been updated to work with dotenv version 3.0 (July 2019) and the new Ministry Platform oAuth changes (Spring 2018)

[](#note-for-v3-this-api-wrapper-has-been-updated-to-work-with-dotenv-version-30-july-2019-and-the-new-ministry-platform-oauth-changes-spring-2018)

A PHP wrapper to access the Ministry Platform (MP) REST API. This version is updated to include the new changes to oAuth authentication rolling out in early 2018. Note that using the API implies a knowledge of the MP data model. The API gives you access to each table in the database. It is up to you to pull the right data (or POST the right data) to the tables and then make any connections.

For example, you could create groups using the API and then you could create Participants using the API. After that is done, you can create Group Participants to add your participants to a group. Because Group Participants depends on the IDs of the group and the participant, the order in which you add the data is important.

Installation
------------

[](#installation)

### Include the Package

[](#include-the-package)

This package is installed via Composer and the following assumes you have installed and initialized Composer for the project. Please refer to the [Composer](http://getcomposer.org) web site for help on getting composer installed and your initial composer.json created.

To add the Ministry Platform API to your project, simply require this package:

```
composer require smadeira/ministry-platform-api
```

Or, you can edit your composer.json file directly to add the Ministry Platform API:

```
"require": {
        "php": ">=8.0.0",
        "smadeira/ministry-platform-api": "^5"
    },

```

### Update the package

[](#update-the-package)

After including the API Wrapper with composer, do a composer update to download the dependencies required for the API wrapper to function.

```
composer update

```

The update command will download all the dependencies (including the API wrapper code) to the vendor diretory. Once this is done, you are ready to start development.

Mote: It's a good idea to run "composer update" every so often to download the latest version of the API wrapper and all of its dependencies. That's the beauty of Composer. It manages all of that for you so you don't have to.

Configuration
-------------

[](#configuration)

There are a few things that need to be done to configure the API wrapper to function in your environment.

### Connection Parameters

[](#connection-parameters)

This package makes use of vlucas/phpdotenv to manage configuration variables. In the root of your project, create a .env file with the following contents. Ensure you are using the correct URIs, client ID and secret for your installation.

```
# Current System Info
MP_API_ENDPOINT="https://connect.example.com/ministryplatformapi"
MP_OAUTH_DISCOVERY_ENDPOINT="https://connect.example.com/ministryplatform/oauth"
MP_API_SCOPE="http://www.thinkministry.com/dataplatform/scopes/all"

# Data from Ministry Platform API Client
MP_CLIENT_ID="churchapi"
MP_CLIENT_SECRET="4064ec5d-f9e6-secret-code-89406642abc7"
MP_OAUTH_REDIRECT_URL="https://example1.com/oAuth"

```

### Loading the API Wrapper

[](#loading-the-api-wrapper)

At the top of your code you will need to do a couple things to get access to the API Wrapper. You need to include autoload capabilities and load the config settings from the .env file

This is an example of what the top of a script might look like to use the Table API and the Stored Procedures API.

```
require_once __DIR__ . '/vendor/autoload.php';

use MinistryPlatformAPI\MinistryPlatformTableAPI as MP;
use MinistryPlatformAPI\MinistryPlatformProcAPI as PROC;

// Get environment variables
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->load();
```

Usage
-----

[](#usage)

Usage is straight forward for client credentials flow. Authenticate and execute your request.
NOTE: if you are using Laravel and Authorization Code flow, check the laravel documentation in the documentation folder for a working code example.

### Authentication

[](#authentication)

Assuming your .env parameters are correct, this will authenticate your code.

```
// For the Table API enpoints
$mp = new MP();
$mp->authenticate();

// For the Procedures API endpoint
$proc = new PROC();
$mp->authenticate();
```

### Execute select query

[](#execute-select-query)

The API Wrapper uses the same syntax as the swagger page. You can define the table, the select statement, filter and orderBy clauses. This will return an array of events and then dump them to the screen. Note that the data uses the familiar MP brand of SQL which is consistent with the platform.

```
// Get all Approved events happening in the next 30 days that are not cancelled and order by the Event Start Date
$events = $mp->table('Events')
         ->select("Event_ID, Event_Title, Event_Start_Date, Meeting_Instructions, Event_End_Date, Location_ID_Table.[Location_Name], dp_fileUniqueId AS Image_ID")
         ->filter('Events.Event_Start_Date between getdate() and dateadd(day, 30, getdate()) AND Featured_On_Calendar = 1 AND Events.[_Approved] = 1 AND ISNULL(Events.[Cancelled], 0) = 0')
         ->orderBy('Event_Start_Date')
         ->get();

print_r($events);

```

### The whole script

[](#the-whole-script)

Here is a whole script that gets events in the next 30 days.

```
