PHP code example of smadeira / ministry-platform-api

1. Go to this page and download the library: Download smadeira/ministry-platform-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

smadeira / ministry-platform-api example snippets




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

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


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

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

// 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);
         



inistryPlatformAPI\MinistryPlatformTableAPI as MP;


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


// Attempt to authenticate to the MP API
$mp = new MP();
$mp->authenticate();

$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);
  
$idList = [385423, 385424];
$sel = "Donation_Date, Donor_ID_Table_Contact_ID_Table.Display_Name, Donation_Amount, Payment_Type_ID_Table.[Payment_Type]";
$filter = "Donation_Date > '2022-05-01' AND Donations.Payment_Type_ID <> 6 and Donations.Domain_ID = 1";

$donations = $mp->table('Donations')
  ->select($sel)
  ->filter($filter)
  ->orderBy('Donation_Amount')
  ->ids($idList)
  ->postGet(); 

// Create the array of records to POST
$rec = [];
$rec[] = ['Event_ID' => 12910, 'Participant_ID' => 46616, 'Participation_Status_ID' => 2];
$rec[] = ['Event_ID' => 12910, 'Participant_ID' => 46617, 'Participation_Status_ID' => 2];

$event = $mp->table('Event_Participants')
		->select("Event_Participant_ID, Event_ID, Participant_ID, Participation_Status_ID")
		->records($rec)			
		->post();

$rec = [];
$rec[] = ['Event_Participant_ID' => 278456, 'Participation_Status_ID' => 3];

$event = $mp->table('Event_Participants')
		->select("Event_Participant_ID, Event_ID, Participant_ID, Participation_Status_ID")
		->records($rec)			
		->put();

$contact = $mp->table('Contacts')->delete(24599);

// IDs are Event_Participant_IDs (primary Key)
$rec = ['IDs' => [309599,309598] ];

$ep = $mp->table('Event_Participants')
             ->records($rec)
             ->deleteMultiple();



inistryPlatformAPI\MinistryPlatformProcAPI as PROC;

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


// Attempt to authenticate to the MP API

$mp = new PROC();
$mp->authenticate();

$input = ['@SelectionID' => 26918];
$contacts = $mp->proc('api_MYGCC_PCOGetSelectedContacts')             
             ->procInput($input)
             ->exec();

print_r($contacts);



use MinistryPlatformAPI\MinistryPlatformFileAPI as MP;

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


// Get metadata for the file(s) based on table and record id
   $fm = $mp->table('Contacts')->recordID(55309)->get();
   $metadata = json_decode($fm, true);
   print_r($metadata);

// Get metadata for the file(s) based on table and record id
$fm = $mp->table('Contacts')->recordID(55309)->get();
$metadata = json_decode($fm, true);

// Loop through the metadata and retrieve each file
foreach ($metadata as $fileData) {

	echo 'Saving file: ' . $fileData['FileName'] . "\n";
	$fileID = $fileData['FileId'];

	$file = $mp->fileID($fileID)->get();
	$outfile = 'D:/Temp/' . $fileData['FileName'];

	file_put_contents($outfile, $file);
}

$filename = 'D:/Pictures/Profile_Picture.jpg';

$response = $mp->table('Contacts')
            ->recordID(55309)
            ->file($filename)
            ->default()
            ->longestDimension(300)
            ->description('New Picture 2020')
            ->post();

$file = $mp->fileId(63793)
              ->description('Second Picture From 2019')
              ->put();

$result = $mp->delete(63792);