Skip to content

Instantly share code, notes, and snippets.

@bmcbride
Created November 10, 2014 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmcbride/c9ca94a0b2ba74d93928 to your computer and use it in GitHub Desktop.
Save bmcbride/c9ca94a0b2ba74d93928 to your computer and use it in GitHub Desktop.
Basic Fulcrum webhook endpoint written in PHP. Enter your form id & email, upload to a web server, and configure your webhook settings in Fulcrum to point to this endpoint. Default action emails webhook JSON payload, but realistically, you would want to do something with the recordArray and formArray variables.
<?php
$form_id = 'your-fulcrum-form-id-goes-here';
$email = 'me@gmail.com';
$input = file_get_contents('php://input'); # POST data from webhook
//$input = file_get_contents('payload.json'); # local file for testing
$webhook = json_decode($input, true);
# Webhook data
$webhookID = $webhook['id'];
$webhookType = $webhook['type'];
$webhookOwner = $webhook['owner_id'];
# Fetch record & form data
$recordArray = array(); # Empty array to hold standard record fields
$formArray = array(); # Empty array to hold custom form fields
foreach ($webhook['data'] as $key => $value) {
# Create an array of all non form_value object members (standard Fulcrum record fields)
if ($key != 'form_values') {
$recordArray[$key] = $value;
}
# Create an array of all form_value object members (custom record form fields)
else {
# Loop through form values and format values
foreach ($webhook['data']['form_values'] as $key => $value) {
# Join choice values & other values and convert to string
if (isset($value['choice_values']) && is_array($value['choice_values'])) {
if (isset($value['other_values'])) {
$value = array_merge($value['choice_values'], $value['other_values']);
}
$value = implode(', ', $value);
}
# If it's an array of objects (photos or repeatables), just give us a comma separated string of the id's
if (is_array($value)) {
$objectArray = array();
foreach ($value as $objectKey => $objectValue) {
if (isset($objectValue['photo_id'])) {
$object['id'] = $objectValue['photo_id'];
}
if (isset($objectValue['id'])) {
$object['id'] = $objectValue['id'];
}
array_push($objectArray, $object['id']);
}
$value = implode(', ', $objectArray);
}
$formArray[$key] = $value;
}
}
}
# Standard Fulcrum fields
$record_id = $recordArray['id'];
$record_created_at = $recordArray['created_at'];
$record_updated_at = $recordArray['updated_at'];
$record_client_created_at = $recordArray['client_created_at'];
$record_client_updated_at = $recordArray['client_updated_at'];
$record_created_by = $recordArray['created_by'];
$record_created_by_id = $recordArray['created_by_id'];
$record_updated_by = $recordArray['updated_by'];
$record_updated_by_id = $recordArray['updated_by_id'];
$record_status = $recordArray['status'];
$record_version = $recordArray['version'];
$record_form_id = $recordArray['form_id'];
$record_form_version = $recordArray['form_version'];
$record_project_id= $recordArray['project_id'];
$record_assigned_to = $recordArray['assigned_to'];
$record_assigned_to_id = $recordArray['assigned_to_id'];
$record_latitude = $recordArray['latitude'];
$record_longitude = $recordArray['longitude'];
$record_altitude = $recordArray['altitude'];
$record_speed = $recordArray['speed'];
$record_course = $recordArray['course'];
$record_horizontal_accuracy = $recordArray['horizontal_accuracy'];
$record_vertical_accuracy = $recordArray['vertical_accuracy'];
# Make sure it's the form we want
if ($webhook['data']['form_id'] == $form_id) {
# Do something with the data...
foreach ($recordArray as $key => $value) {
echo $key . ': ' . $value . '</br>';
}
foreach ($formArray as $key => $value) {
echo $key . ': ' . $value . '</br>';
}
# Write payload out to file for inspection (true to send email)
writePayload(true);
}
function writePayload($email) {
global $input;
$json = json_encode($input);
$webhook = json_decode($json, true);
$payload = fopen('payload.json', 'w+');
fwrite($payload, $webhook);
fclose($payload);
if ($email == 'true') {
sendEmail($webhook);
}
}
function sendEmail($webhook) {
global $email;
$subject = 'Fulcrum Webhook Payload';
$message = $webhook;
mail($email, $subject, $message);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment