Skip to table dataSkip to menuSkip to searchHotkeys reference
The PHP scripts below all work in conjuction with the FileMaker Data API to perform a variety of tasks.
The first section (line 18 - line 47) encodes the FileMaker user's credentials, logs in to the server, and generates an access token.
The second section (line 50 - line 68) retrieves a JSON array containing data from the first 100 records in the database.
The third section (line 71 - line 93) looks up a currency exchange rate using 3 character currency code appended to the URL.
The fourth section (line 96 - line 109) URL-encodes a parameter and sends it to the server for use in the specified script.
The fifth section (line 112 - line 129) uploads a file to a specified record's container field.
The sixth section (line 132 - line 155) retrieves a JSON array containing data from 10 records, offset by 10, sorted by field name, descending.
The last section (line 158 - line 169) deletes the access token. If not deleted it will expire 15 minutes after generation.
//Double slashes comment out single lines, /* and */ comment out multiple lines. Add or remove comments to execute script sections.

You need to change "yourwebserver.com" to reference your PHP-equipped web server (this example uses a 2mhost SSL-enabled LiteSpeed server).
You need to change "a12345" to reference your FMP DATA API-enabled FileMaker server (this example uses a database hosted by fmphost.com).
You need to change "filemakeruserwithAPIenabledaccount" to the name of an FMP DATA API-enabled account.
You need to change "filemakeruserwithAPIenabledaccountpassword" to the password of an FMP DATA API-enabled account.
You need to change "yourfilemakerdatabasename" to the name of the FMP DATA API-enabled database.
You need to change "yourfilemakerdatabaselayoutname" to the name of the FMP DATA API-enabled layout.

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
//error_reporting(E_ALL);
//Encode FMP Account User Name and Password, Retrieve a Token to Use In Subsequent Requests
$username = 'filemakeruserwithAPIenabledaccount';
$userpass = 'filemakeruserwithAPIenabledaccountpassword';
$credentials = base64_encode($username.":".$userpass);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://a12345.fmphost.com/fmi/data/vLatest/databases/yourfilemakerdatabasename/sessions',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{}',
  CURLOPT_HTTPHEADER => array('Content-Type: application/json','Authorization: Basic '.$credentials),
));
$response = curl_exec($curl);
curl_close($curl);
$json = json_decode($response, true);
$token = $json['response']['token']; 
//echo $token;


/*Retrieve a JSON Array Containing 100 Records
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://a12345.fmphost.com/fmi/data/vLatest/databases/yourfilemakerdatabasename/layouts/yourfilemakerdatabaselayoutname/records',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array('Authorization: Bearer '.$token),
));
$response = curl_exec($curl);
curl_close($curl);
$json = json_decode($response, true);
echo "<pre>";
print_r($json);
*/


/*Retrieve A Single Currency Exchange Rate Using 3 Character Currency Code(CAD,EUR,RUB)
$code = $_GET['code'];
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://a12345.fmphost.com/fmi/data/vLatest/databases/yourfilemakerdatabasename/layouts/yourfilemakerdatabaselayoutname/_find',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{"query": [{"CurrencyCode": "='.$code.'"}]}',
  CURLOPT_HTTPHEADER => array('Authorization: Bearer '.$token,'Content-Type: application/json'),
));
$response = curl_exec($curl);
curl_close($curl);
$json = json_decode($response, true);
//echo "<pre>";
//print_r($json);
$exchangerate = $json['response']['data'][0]['fieldData']['ExchangeRate']; 
echo $exchangerate;
*/


///*Send Parameters To and Run A Script
$scriptParameters = urlencode('Monrovian Etherium');
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://a12345.fmphost.com/fmi/data/vLatest/databases/yourfilemakerdatabasename/layouts/yourfilemakerdatabaselayoutname/script/SetCurrencyName?script.param='.$scriptParameters,
  CURLOPT_HTTPHEADER => array('Content-Type: application/json','Authorization: Bearer '.$token),
  CURLOPT_HEADER => 0,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_RETURNTRANSFER => true),
);
$response = curl_exec($curl);
curl_close($curl);
//echo $response;
//*/


/*Upload a File to a Specified Record's Container Field
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://a12345.fmphost.com/fmi/data/vLatest/databases/yourfilemakerdatabasename/layouts/yourfilemakerdatabaselayoutname/records/61787/containers/file/1',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('upload'=> new CURLFILE('testimage.jpg','image/jpg','testimage.jpg')),  
  CURLOPT_HTTPHEADER => array('Authorization: Bearer '.$token,'Content-Type: multipart/form-data'),
));
$response = curl_exec($curl);
curl_close($curl);
//echo $response;
*/


/*Retrieve a JSON Array of 10 Records, Offset By 10, Sort By A Named Field, In Descending Order
$curl = curl_init();
$sort = urlencode('[{"fieldName":"CurrencyName","sortOrder":"descend"}]');
$entities = ['%3A','%2C'];
$replacements = [":",","];
$sort = str_replace($entities, $replacements, $sort);
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://a945171.fmphost.com/fmi/data/vLatest/databases/APIforms/layouts/Rates/records?_offset=10&_limit=10&_sort='.$sort,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array('Authorization: Bearer '.$token),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
$json = json_decode($response, true);
echo "<pre>";
print_r($json);
*/


//Delete the Token
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://a12345.fmphost.com/fmi/data/vLatest/databases/yourfilemakerdatabasename/sessions/'.$token,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'DELETE',
  CURLOPT_POSTFIELDS =>'{}',
  CURLOPT_HTTPHEADER => array('Content-Type: application/json','Authorization: Basic '.$credentials),
));
$response = curl_exec($curl);
curl_close($curl);
?>