How the API works #
Since ChurchTools controls all communication via AJAX, every function can also be called via API.
For example https://xxx.church.tools/index.php?q=churchresource/ajax
And then via POST: func=getBookings (returns all bookings ) or e.g. func=getMasterData returns all necessary master data.
It is important that the call is sent via POST command and that the desired function is contained in the parameter “func=functioname”. There are different calls per module. This can be looked up under system/modulename/classes/CTModulename.class.php.
Further examples of API calls #
For example, Login via
https://xxx.church.tools/?q=login
Then POST the parameters:
password
directtool=yes
Query from all persons
https://xxx.church.tools/?q=churchdb/ajax&func=getAllPersonData
Query a person
https://xxx.church.tools/?q=churchdb/ajax&func=getPersonDetails&id=736
Example of an API call in PHP #
<?php /** * Example API call to ChurchTools Installation for PHP. The cookies will be stored to current directory * Author: Jens Martin Rauen * Copyright: 2015 **/
/** * Get saved Cookies from filesystem and returns an array * @return array of Cookies in string */ function getCookies() { $aCookies = glob("*.txt"); $n = count($aCookies); if ($n !== 0) { $counter = 0;
while ($counter < $n) { $sCombined .= file_get_contents($aCookies["$counter"]) . ';'; ++$counter; } return $sCombined; } else { return $n; }}
/** * Save cookies from $aRH (http response header) to the filesystem * @param $aRH */ function saveCookies($aRH) { $n = count($aRH); // Number of Pieces $counter = 0; while ($counter <= $n) { if(preg_match('@Set-Cookie: (([^=]+)=[^;]+)@i', $aRH["$counter"], $matches)) { $fp = fopen($matches["2"].'.txt', 'w'); fwrite($fp, $matches["1"]); fclose($fp); } ++$counter; } }
/**
* Send request to $url with $data * @param [type] $url [description] * @param [type] $data [description] */ function sendRequest($url, $data) { $options1 = array( 'http' => array( 'header' => "Cookie: ".getCookies() . "rnContent-type: application/x-www-form-urlencodedrn", 'method' => 'POST', 'content' => http_build_query($data), ), ); $context = stream_context_create($options1); $result = file_get_contents($url, false, $context); print_r($result); saveCookies($http_response_header); }
// Login to mghh churchtools $url = 'https://mghh.churchtools.de/?q=login'; $data = array('email' => 'sabine@mghh.churchtools.de', 'password' => '1234', 'directtool' => 'yes'); sendRequest($url, $data);
// Now get all Person data $url = 'https://mghh.churchtools.de/index.php?q=churchdb/ajax'; $data = array('func' => 'getAllPersonData'); sendRequest($url, $data);