FMCostaRica

PHP Class for FileMaker Server 17 Data API

Discussion created by FMCostaRica on May 20, 2018
Latest reply on Jun 20, 2018 by HOnza

I had a dream that somewhere out there was a well written PHP class that will help me easily interact with the new FMS data API in FMS 17 and I believe also in FileMaker Cloud 17, well that dream came true and I found this awesome object oriented class that makes it easy to work with the new data API.

 

Big thanks to Masayuki Nii for creating and sharing this with the rest of the world.

 

Here are the goodies: GitHub - msyk/FMDataAPI: FMDataAPI is a class developed by PHP to access FileMaker database with FileMaker Data API.

 

Documentation for this PHP class is here: API Documentation

 

I plan to use the PHP class for for many things, here is a simple example: In our WordPress powered website when a customer contacts us after the form is submitted the code below performs a script in our FileMaker database, that script in FIleMaker checks if the email exists, if it does not then we create a new customer in our database with the data that got submitted via the form witch is passed with the script as a parameter, this is a very basic example because the PHP class allows you to do a lot more but here it is:

 

<?php

 

// Include FMDataAPI.php

include_once "FMDataAPI.php";


// The main class name FMDataAPI is defined at the current namespace

use INTERMediator\FileMakerServer\RESTAPI\FMDataAPI as FMDataAPI;


// Wrap the code in a try/catch block to show exceptions and errors

try

{

   // Instanticate the class FMDataAPI with params (database name, user name, password and host name)

   $fmdb = new FMDataAPI("dataBaseName", "user", "password", "host");


   // If true, the debug mode is activated. Debug mode echos all the results one by one

   $fmdb->setDebug(true);


   // Script to execute. Note: Parameters must be JSON encoded

   $script = array(

   'script' => 'scriptName',

   'script.param' => json_encode(array(

   'formData' => 'Form data to use in FileMaker',

  ))

  );


   // Perform the script and pass data as parameters

   $result = $fmdb

   // Layout name

   ->someLayoutName

   // Action and parameters

   ->getRecord(null, null, $script);

}

catch(Exception $e)

{

   echo '<div><h3>An exception has occurred</h3>', $e->getMessage() , "<div>";

}

 

That code will authenticate, get a token, use that token to execute a script in FMS 17 via the new data API and at the end logout and kill that token with just those few lines.

 

Here is another example that creates a new record in FileMaker and puts that data in a field. The data was sent to the URL where the php file/page is located, this data is then later on processed via a FMS script schedule.

 

 

<?php

 

// Include FMDataAPI.php

include_once "FMDataAPI.php";


// The main class name FMDataAPI is defined at the current namespace

use INTERMediator\FileMakerServer\RESTAPI\FMDataAPI as FMDataAPI;


// Wrap the code in a try/catch block to show exceptions and errors

try

{

   // Instanticate the class FMDataAPI with params (database name, user name, password and host name)

   $fmdb = new FMDataAPI("dataBaseName", "user", "password", "host");


   // If true, the debug mode is activated. Debug mode echos all the results one by one

   $fmdb->setDebug(true);


   // Capture data comming in

   $PayloadJSON = file_get_contents('php://input');


   // Data

   $fieldsData = array(

   "someFieldNameInFileMaker" => $PayloadJSON

  );


   // Create the record with the JSON Data

   $result = $fmdb

   // Layout name

   ->someLayoutNameInFileMaker

   // Action and parameters

   ->create($fieldsData, null, null);

}

catch(Exception $e)

{

   echo '<div><h3>An exception has occurred</h3>', $e->getMessage() , "<div>";

}

 

Again the code above will authenticate, get a token, use that token to create a new record in FileMaker and logout when done, if debug is set to true and you load the php on a server or local server environment you will see all the details of what the PHP script did and show errors if any.

 

This again is super handy and easy to use if, I am not a PHP developer but have invested some time in learning the basics so I can do basic things like this and this PHP class pretty much fell from heaven to help me transition from the old PHP and XML or CWP that I was using to the new future of FMS CWP.

 

Enjoy and please read the documentation first before posting questions for how to do something with the PHP class, I am not the creator of the PHP class, Im just sharing that I found it and a couple of examples I created with it.

 

Hope this helps someone.

Outcomes