SaveDataToWEB
Back to ListDescription: Save data from FM applicatin to Web-site rest API
FileMaker Prototype:
Set Variable [$res; ACF_Run("SaveDataToWEB"; string_PrimaryKey)]
Category: REST API
Function source:
function SaveDataToWEB (string pkey )
array string aName, aDesc, aCat, aCode, afmCalc, aLongDesc;
array int aRemoteID;
string sql, res;
JSON data, wsres;
int insID;
string url="https://domain.no/path-to-api-script/libacf.php";
res = ExecuteSQL ("SELECT FunctionName, Description, Category, Code, fmCalc, longDesc, RemoteID FROM ACF_Functions WHERE PrimaryKey = :pkey
INTO :aName, :aDesc, :aCat, :aCode, :afmCalc, :aLongDesc, :aRemoteID");
if (sizeof ( aName ) > 0 ) then
data = JSON ( "id", string (aRemoteID[1]), "name", aName[1], "category", aCat[1],
"description", aDesc[1], "code", aCode[1], "fmCalc", afmCalc[1], "longDesc", aLongDesc[1], "APIkey", "MyAPIKey");
print string(data)+"\n\n";
res = HTTP_POST(url,string(data));
print res;
wsres = res;
if ( wsres ["status"] == "success" ) then
insID = int(wsres["id"]);
if ( insID != 0) then
res = ExecuteSQL ( "UPDATE ACF_Functions SET RemoteID = :insID WHERE PrimaryKey = :pkey");
end if
else
alert ( res);
end if
else
alert ("SQL feil:" + res);
Return res;
end if
return "OK";
end
SaveDataToWEB is part of a REST-API implementation to send data to a remote site and for update of the site.
It can serve as a template for similar functionality in your solution.
We do a SQL query to pull data from FM, construct a JSON object and send it to the remote site.
The data returned from the WEB site is in JSON format, telling about the status of the request. It also contains the insert ID from the remote site, that is updated in the local FM table.
Example
Set Variable [$res; ACF_Run("SaveDataToWEB"; myTable::PrimaryKey)]
The Counterpart is a PHP script on the web-server holding the WordPress site in question. Here is the Source for the Rest-API implementation on the remote site.
< ?php
// Include WordPress to access database credentials
require_once('../../wp-config.php');
// Set headers for JSON response and CORS
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
// Connect to the database using WordPress credentials
global $wpdb;
$table_name = $wpdb->prefix . 'acf_functions'; // Adjusted for table name with prefix
// Check the request method
$method = $_SERVER['REQUEST_METHOD'];
$response = [];
if ($method === 'GET') {
// Get record by ID
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$id = intval($_GET['id']);
$record = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $id), ARRAY_A);
if ($record) {
$response = ['status' => 'success', 'data' => $record];
} else {
$response = ['status' => 'error', 'message' => 'Record not found'];
}
} else {
$response = ['status' => 'error', 'message' => 'Invalid or missing ID'];
}
} elseif ($method === 'POST') {
// Save record (update if ID exists, insert if ID is missing)
$input = json_decode(file_get_contents('php://input'), true);
// "APIkey", "MyAPI-Key"
if (isset($input['APIkey'])) {
$APIkey = sanitize_text_field($input['APIkey']);
} else {
$APIkey = "";
}
if ($APIkey == "MyAPI-Key") {
if (isset($input['name'], $input['category'], $input['description'], $input['code'],$input['fmCalc'],$input['longDesc'])) {
// Sanitize input data
$name = sanitize_text_field($input['name']);
$category = sanitize_text_field($input['category']);
$description = sanitize_text_field($input['description']);
$code = sanitize_textarea_field($input['code']);
$fmCalc = sanitize_textarea_field($input['fmCalc']);
$longDesc = sanitize_textarea_field($input['longDesc']);
if (!empty($input['id'])) {
// Update existing record
$id = intval($input['id']);
$updated = $wpdb->update(
$table_name,
['name' => $name, 'category' => $category, 'description' => $description, 'code' => $code, 'fmCalc' => $fmCalc, 'longDesc' => $longDesc],
['id' => $id],
['%s', '%s', '%s', '%s', '%s', '%s'],
['%d']
);
$response = $updated ? ['status' => 'success', 'message' => 'Record updated'] : ['status' => 'error', 'message' => 'Failed to update record'];
} else {
// Insert new record
$inserted = $wpdb->insert(
$table_name,
['name' => $name, 'category' => $category, 'description' => $description, 'code' => $code, 'fmCalc' => $fmCalc, 'longDesc' => $longDesc],
['%s', '%s', '%s', '%s', '%s', '%s']
);
$response = $inserted ? ['status' => 'success', 'message' => 'Record inserted', 'id' => $wpdb->insert_id] : ['status' => 'error', 'message' => 'Failed to insert record'];
}
} else {
$response = ['status' => 'error', 'message' => 'Missing required fields'];
}
} else {
$response = ['status' => 'error', 'message' => 'Something invalid, ahhhhh....'];
}
} else {
$response = ['status' => 'error', 'message' => 'Invalid request method'];
}
// Output the JSON response
echo json_encode($response);
exit;
?>
