PHP code example of rcconsulting / myfmapilibrary-for-php
1. Go to this page and download the library: Download rcconsulting/myfmapilibrary-for-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rcconsulting / myfmapilibrary-for-php example snippets
use \RCConsulting\FileMakerApi\DataApi;
// Please note that the library does not currently support external data source authentication
$dataApi = new \RCConsulting\FileMakerApi\DataApi('https://test.fmconnection.com/fmi/data', 'MyDatabase');
$dataApi->login('filemaker api user', 'filemaker api password');
// Arguments: DAPI URL, Database Name, DAPI User, DAPI Password, SSL Verification Enabled?, Force Legacy HTTP 1.1?, Return Response Object?, HTTP Client Type?, Data API Version?
$dataApi = new \RCConsulting\FileMakerApi\DataApi('https://test.fmconnection.com/fmi/data','MyDatabase', "DAP_User", "DAPI_Pass_1234~", True, False, False, HttpClient::CURL, DapiVersion::V1);
// Please note that RCC has not tested OAuth logins. We welcome any PR's which improve support.
$dataApi = new \RCConsulting\FileMakerApi\DataApi('https://test.fmconnection.com/fmi/data', 'MyDatabase');
$dataApi->loginOauth('oAuthRequestId', 'oAuthIdentifier');
// It is highly recommended to log out at each script or large process end. The Data API tokens last 15 minutes after last use, and so will hold a slot open for 15 minutes after your last connection if you do not clean up after yourself. Additionally, logout() does not destroy the DataAPI object you created (via new) previously, but it does clean out any authorization tokens from the object.
$dataApi->logout();
use RCConsulting\FileMakerApi\DataApi;
// Using DataApi\HttpClientType::CURL (default)
$dataApi = new DataApi(
'https://test.fmconnection.com/fmi/data',
'MyDatabase',
'username',
'password',
true, // SSL verification
false, // Force legacy HTTP
false, // Return response object
HttpClient::CURL, // HTTP client type (enum)
DapiVersion::V1 // Data API version (enum)
);
// Using DataApi\HttpClientType::GUZZLE
$dataApi = new DataApi(
'https://test.fmconnection.com/fmi/data',
'MyDatabase',
'username',
'password',
true, // SSL verification
false, // Force legacy HTTP
false, // Return response object
HttpClient::GUZZLE, // HTTP client type (enum)
DapiVersion::VLATEST // Data API version (enum)
);
// Default behavior (uses HttpClientType::CURL)
$dataApi = new DataApi(
'https://test.fmconnection.com/fmi/data',
'MyDatabase',
'username',
'password'
);
// Using string 'curl'
$dataApi = new DataApi(
'https://test.fmconnection.com/fmi/data',
'MyDatabase',
'username',
'password',
true, // SSL verification
false, // Force legacy HTTP
false, // Return response object
'curl', // HTTP client type (string)
DapiVersion::V1 // Data API version (enum)
);
// Using string 'guzzle'
$dataApi = new DataApi(
'https://test.fmconnection.com/fmi/data',
'MyDatabase',
'username',
'password',
true, // SSL verification
false, // Force legacy HTTP
false, // Return response object
'guzzle', // HTTP client type (string)
DapiVersion::V1 // Data API version (enum)
);
new DataApi(
string $apiUrl, // FileMaker Data API URL
string $apiDatabase, // Database name
string $apiUser = null, // Username (optional)
string $apiPassword = null, // Password (optional)
bool $sslVerify = true, // Verify SSL certificates
bool $forceLegacyHTTP = false, // Force HTTP/1.1
bool $returnResponseObject = false, // Return full response objects
HttpClient|string $httpClient = HttpClient::CURL // HTTP client (enum or string)
DapiVersion $dapiVersion = DapiVersion::V1 //specify Data API version (enum) currently v1, v2 or vLatest
);
// Call login method first
$containerFieldName = 'Picture';
$containerFieldRepetition = 1;
// replace 'upload' below with the name="value" of the file input element of your web form
$filepath = $_FILES['upload']['tmp_name'];
$filename = $_FILES['upload']['name'];
try {
$dataApi->uploadToContainer('layout name', $recordId, $containerFieldName, $containerFieldRepetition, $filepath, $filename);
} catch(\Exception $e) {
// handle exception
}
// useful when not explicitly logging into data api, but already have valid session token
// this implicitly sets the token retrieval date to now()
$dataApi->setApiToken($token);
// here, $tokenDate is a unix timestamp
$dataApi->setApiToken($token, $tokenDate);
// returns current api token (without checking if it's valid)
$token = $dataApi->getApiToken();
// check if token is expired using previously-stored last use-date
// this is NOT a live check with FMS
if ($dataApi->isApiTokenExpired()) {
// token expired
} else {
// do stuff
}
// refresh token when it has expired
// this logs in again, which creates a new session, so should only be used after the previous token has expired
// you must have used the login() method previously. oAuth not tested.
if ($dataApi->refreshToken()) {
// success
} else {
// failure - either the login itself failed, or you have no stored credentials
}
// check if the session token we are using is valid according to FileMaker Server
if ($dataApi->validateTokenWithServer()){
// token is valid according to FMS!
} else {
// token has expired according to FMS!
}
//use this if you need to explicitly use v1 or v2. The default is v1, any future versions will be added. vLatest is also supported.
$dataApi->setDAPIVersion(DapiVersion::V2)
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.