\GiveItARest

GiveItARest class for FileMaker Data REST API

Resources:
FileMaker Data API Guide: https://fmhelp.filemaker.com/docs/17/en/dataapi/
PHP Source Code Download: https://harmonic-data.com/productions/GiveItARest/GiveItARestAPI.zip
Sample FileMaker Database File: https://harmonic-data.com/productions/GiveItARest/GiveItARest_SampleFile.zip

This class is a library is created to interact with the FileMaker Data API engine

Class is designed to be a replacement for FileMaker API for PHP. This will help developers to easily migrate the existing projects from the XML based request FileMaker PHP API to GiveItARest(REST).

Summary

Methods
Properties
Constants
__construct()
isError()
setProperty()
newFindCommand()
newAddCommand()
newEditCommand()
newDeleteCommand()
newCompoundFindCommand()
getContainerData()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

__construct()

__construct(string  $host, string  $db, string  $extDataSource) 

Constructor.

$extDataSource is a JSON string containing database credentials. The JSON string can be setup to include multiple databases for multi-file applications.

  database => (string) The name of the database to user
  username => (string) Connect to the database as this username.
  password => (string) Password associated with the username.

Parameters

string $host

FileMaker Server URL

string $db

FileMaker Database name

string $extDataSource

JSON string

Examples

/**
 * sample code multi-file access
 */
$dbStr = '[{"database": "GiveItARest_SampleFile", "username": "api_user", "password": "password"}]';
$fmConnection = new GiveItARest('https://localhost','GiveItARest_SampleFile', $dbStr);
/**
 * sample code multi-file access
 */
$dbStr = '[{"database": "GiveItARest_SampleFile", "username": "api_user", "password": "password"},
		   {"database": "DBFile2", "username": "api_user", "password": "password"}]';
$fmConnection = new GiveItARest('https://localhost','DBFile1', $dbStr);

isError()

isError(object  $var) : boolean

Validates if the argument/object is an instance of GiveItARest_Error

Parameters

object $var

Returns

boolean

Examples

$findCmd = $fmConnection->newFindCommand('_PHP_contact');
$findCmd->addFindCriterion('___c_ID', "33444824151288018922307016345148437207682741855872941767892452"");

$findResult = $findCmd->execute();

if (GiveItARest::isError($findResult)) {
	echo 'Received an error from FileMaker Data API, code:' . $findResult->code . ' Message:'. $findResult->message;
	//print_r sample output GiveItARest_Error Object ( [code] => 401 [message] => No records match the request )
} else {
	//do something
}

setProperty()

setProperty(string  $key, string  $val) : void

Set additonal properties neeeded for FileMaker DATA API request

This function can be use to property to store the DATA API token storeTokenDirectory - (local directory) when property is set, GiveItARest library will store the API token to a physical file for the given directory. By default, GiveItARest token will be in stored in COOKIE object

Parameters

string $key

Property name

string $val

Property value

newFindCommand()

newFindCommand(string  $lay) : \GiveItARest_Find

Create a find command object for the given layout

Parameters

string $lay

FileMaker layout name

Returns

\GiveItARest_Find

Examples

$findCmd = $fmConnection->newFindCommand('_PHP_contact');
$findCmd->addFindCriterion('___c_ID', "4824151288018922307016345148437207682741855872941767892452");

$findResult = $findCmd->execute();

if (GiveItARest::isError($findResult)) {
	echo 'Received an error from FileMaker Data API, code:' . $findResult->code . ' Message:'. $findResult->message;
} else {
	//do something
	echo 'Record count is: ' .$findResult->getFoundSetCount();
	$records = $findResult->getRecords();
	echo "<br><br>Field List: ".implode(', ', $findResult->getFields());
	foreach ($records as $record) {
		echo "<br><br>My Name is: " . $record->getField('Name_First') . '  '. $record->getField('Name_Last');
		
		//get related records
		$relatedSets = $record->getRelatedSet('Comms');
		foreach ($relatedSets as $relatedRecord) {
			echo "<br><br>Contact: " . $relatedRecord->getField('Comms::Contact_info_Type') . ': ' . $relatedRecord->getField('Comms::Contact_info');
		} 
	}
}

newAddCommand()

newAddCommand(string  $layout, array  $values = null) : \GiveItARest_Add

Create a add command object for the given layout. Sets the fields the in layout when $values array is provided (key value pair where key is FileMaker column name)

Parameters

string $layout

FileMaker layout name

array $values

Returns

\GiveItARest_Add

Examples

$queryCmd = $fmConnection->newAddCommand('_PHP_contact');
$queryCmd->setField('Name_First', 'New First');
$queryCmd->setField('Name_Last', 'New Last');


$qryResult = $queryCmd->execute();	
if (GiveItARest::isError($qryResult)) {
	echo 'Error an updating record from FileMaker Data API, code:' . $findResult->code . ' Message:'. $findResult->message;
} else {
	// $qryResult value is equal to modification id 
	echo 'Record Added';
}
$queryCmd = $fmConnection->newAddCommand('_PHP_contact');
$queryCmd->setField('Name_First', 'New First Again');
$queryCmd->setField('Name_Last', 'New Last Again');

// Set add/edit command returnRecordOnExecute() to true to return the updated result object on execute
// This option is set to false by default due to performance consideration
$queryCmd->returnRecordOnExecute(true);
$qryResult = $queryCmd->execute();	
if (GiveItARest::isError($qryResult)) {
	echo 'Error an updating record from FileMaker Data API, code:' . $findResult->code . ' Message:'. $findResult->message;
} else {
	echo 'Record Added to ' . $qryResult->getRecords()[0]->getField('Name_First') . ' ' .  $qryResult->getRecords()[0]->getField('Name_Last')   ;
}

newEditCommand()

newEditCommand(string  $layout, integer  $recordId) : \GiveItARest_Edit

Create a edit command object for the given layout and record id.

Parameters

string $layout

FileMaker layout name

integer $recordId

Returns

\GiveItARest_Edit

Examples

$findCmd = $fmConnection->newFindCommand('_PHP_contact');
$findCmd->addFindCriterion('___c_ID', "4824151288018922307016345148437207682741855872941767892452");
$findResult = $findCmd->execute();

if (GiveItARest::isError($findResult)) {
	echo 'Received an error from FileMaker Data API, code:' . $findResult->code . ' Message:'. $findResult->message;
} else {
	//do something
	if ($findResult->getFoundSetCount() == 1) {
		$record = $findResult->getRecords()[0];
		$queryCmd = $fmConnection->newEditCommand('_PHP_contact', $record->getRecordId());
		$queryCmd->setField('Name_First', 'Testing');
		
		$qryResult = $queryCmd->execute();	
		if (GiveItARest::isError($qryResult)) {
			echo 'Error an updating record from FileMaker Data API, code:' . $findResult->code . ' Message:'. $findResult->message;
		} else {
			// $qryResult value is equal to modification id 
			echo 'Record updated';
		}
	}
}
$findCmd = $fmConnection->newFindCommand('_PHP_contact');
$findCmd->addFindCriterion('___c_ID', "4824151288018922307016345148437207682741855872941767892452");
$findResult = $findCmd->execute();


if (GiveItARest::isError($findResult)) {
	echo 'Received an error from FileMaker Data API, code:' . $findResult->code . ' Message:'. $findResult->message;
} else {
	//do something
	if ($findResult->getFoundSetCount() == 1) {
		$record = $findResult->getRecords()[0];
		$queryCmd = $fmConnection->newEditCommand('_PHP_contact', $record->getRecordId());
		$queryCmd->setField('Name_First', 'Testing2');
		
		// Set add/edit command returnRecordOnExecute() to true to return the updated result object on execute
		// This option is set to false by default for increase performance
		$queryCmd->returnRecordOnExecute(true);
		$qryResult = $queryCmd->execute();	
		if (GiveItARest::isError($qryResult)) {
			echo 'Error an updating record from FileMaker Data API, code:' . $findResult->code . ' Message:'. $findResult->message;
		} else {
			echo 'Record updated to ' . $qryResult->getRecords()[0]->getField('Name_First') . ' ' .  $qryResult->getRecords()[0]->getField('Name_Last')   ;
		}
	}
}

newDeleteCommand()

newDeleteCommand(string  $layout, integer  $recordId) : \GiveItARest_Delete

Generates a delete command object for the given layout and record id.

Parameters

string $layout

FileMaker layout name

integer $recordId

Returns

\GiveItARest_Delete

Examples

$findCmd = $fmConnection->newFindCommand('_PHP_contact');
$findCmd->addFindCriterion('___c_ID', "596568792341906065317284418899968269018552331809146224272");
$findResult = $findCmd->execute();

if (GiveItARest::isError($findResult)) {
	echo 'Received an error from FileMaker Data API, code:' . $findResult->code . ' Message:'. $findResult->message;
} else {
	//do something
	if ($findResult->getFoundSetCount() == 1) {
		$record = $findResult->getRecords()[0];
		$queryCmd = $fmConnection->newDeleteCommand('_PHP_contact', $record->getRecordId());
		
		$qryResult = $queryCmd->execute();	
		if (GiveItARest::isError($qryResult)) {
			echo 'Error an deleting record from FileMaker Data API, code:' . $findResult->code . ' Message:'. $findResult->message;
		} else {
			// $qryResult value should be equal to 1 when transaction is successfuly
			echo 'Record Deleted ';
		}
	}
}

newCompoundFindCommand()

newCompoundFindCommand(string  $layout) : \GiveItARest_CompoundFind

Generates a compound find object

Parameters

string $layout

FileMaker layout name

Returns

\GiveItARest_CompoundFind

Examples

$findCommandSearch = $fmConnection->newCompoundFindCommand('_PHP_contact');
$findreq1 =$fmConnection->newFindRequest('_PHP_contact');
$findreq1->addFindCriterion('___c_ID', "4824151288018922307016345148437207682741855872941767892452");

$findreq2 = $fmConnection->newFindRequest('_PHP_contact');
$findreq2->addFindCriterion('___c_ID', "2645622429013204167967022827307841901954743188579480174958");

$findCommandSearch->add(1,$findreq1);
$findCommandSearch->add(2,$findreq2); 

$findCommandSearch->addSortRule('Name_First', FILEMAKER_SORT_ASCEND);			
$findResult = $findCommandSearch->execute();


if (GiveItARest::isError($findResult)) {
	echo 'Received an error from FileMaker Data API, code:' . $findResult->code . ' Message:'. $findResult->message;
} else {
	//do something
	echo 'Record count is: ' .$findResult->getFoundSetCount();
	$records = $findResult->getRecords();
	echo "<br><br>Field List: ".implode(', ', $findResult->getFields());
	foreach ($records as $record) {
		echo "<br><br>My Name is: " . $record->getField('Name_First') . '  '. $record->getField('Name_Last'); 
	}
}

getContainerData()

getContainerData(string  $url) : string

Retrieves the base64 string equivalent of the container field. Note that URL provided in the REST API Container field request is intended for single use. There is a need to request a new link once the URL has been accessed.

Parameters

string $url

Container field single access URL

Returns

string —

BASE64 encoded string (file contents)

Examples

$findCmd = $fmConnection->newFindCommand('_PHP_contact');
$findCmd->addFindCriterion('___c_ID', "4824151288018922307016345148437207682741855872941767892452");
$findResult = $findCmd->execute();


if (GiveItARest::isError($findResult)) {
	//echo 'Received an error from FileMaker Data API, code:' . $findResult->code . ' Message:'. $findResult->message;
} else {
	//do something
	
	$records = $findResult->getRecords();
	foreach ($records as $record) {//echo $record->getField('Pic_Profile');
		header("content-type: image/jpeg");
		echo $fmConnection->getContainerData($record->getField('Pic_Profile')); 
	}
}