Funktionsweise der API #
Da ChurchTools die gesamte Kommunikation über AJAX steuert, kann jede Funktion auch über API aufgerufen werden.
Also z. B. https://xxx.churchtools.de/index.php?q=churchresource/ajax
Und per POST dann: func=getBookings (liefert alle Buchungen) oder z.B. func=getMasterData liefert alle notwendigen Stammdaten.
Wichtig ist, dass der Aufruf über POST-Befehl gesendet wird und die gewünschte Funktion in dem Parameter „func=functioname“ enthalten ist. Pro Modul gibt es verschiedene Aufrufe. Hierzu kann unter system/modulename/classes/CTModulename.class.php nachgeschaut werden.
Weitere Beispiele für API-Calls #
Also z. B. Login über
https://xxx.churchtools.de/?q=login
Per POST dann die Parameter:
password
directtool=yes
Abfrage von allen Personen
https://xxx.churchtools.de/?q=churchdb/ajax&func=getAllPersonData
Abfrage einer Person
https://xxx.churchtools.de/?q=churchdb/ajax&func=getPersonDetails&id=736
Beispiel eines API-Calls 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);