php Fulcrum Webhook脚本,用于将数据共享同步到MySQL数据库。需要PHP并启用PDO和allow_url_fopen。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Fulcrum Webhook脚本,用于将数据共享同步到MySQL数据库。需要PHP并启用PDO和allow_url_fopen。相关的知识,希望对你有一定的参考价值。
<?php
/**
* Title: Fulcrum Webhook for syncing data shares to MySQL database
* Notes: Requires PHP with PDO & allow_url_fopen enabled
* Author: Bryan R. McBride
* Source: https://gist.github.com/bmcbride/44afdc10ee943b4e7b92
*/
# Fulcrum app information
$formID = 'your-fulcrum-form-id';
$shareToken = 'your-data-share-token';
# MySQL database connection info
$host = 'localhost:3306';
$dbname = 'your-database';
$user = 'your-user';
$password = 'your-pass';
$table = 'your-table';
$db = new PDO('mysql:host=' . $host . ';dbname=' . $dbname, $user, $password);
# Webhook JSON payload
//$input = file_get_contents('mysql_webhook_payload.json'); # Local file for debugging
$input = file_get_contents('php://input'); # POST data from webhook
$payload = json_decode($input, true);
# Make sure it's the form we want
if ($payload['data']['form_id'] == $formID) {
# Fetch record info from data share
$geojson = file_get_contents('https://web.fulcrumapp.com/shares/' . $shareToken . '.geojson?fulcrum_id=' . $payload['data']['id']);
$features = json_decode($geojson)->features;
$properties = $features[0]->properties;
$fields = [];
$values = [];
# Push feature properties to fields and values arrays
foreach($properties as $property => $value) {
if ($property != 'marker-color') {
array_push($fields, $property);
array_push($values, $value);
}
}
# Build SQL statement based on record event type
if ($payload['type'] === 'record.create') {
$sql = 'INSERT INTO ' . $table . ' (' . implode(', ', $fields) . ') VALUES (' . ':' . implode(', :', $fields) . ')';
$properties = (array) $properties;
unset($properties['marker-color']);
} elseif ($payload['type'] === 'record.update') {
$updates = [];
foreach($properties as $property => $value) {
if ($property != 'fulcrum_id' && $property != 'marker-color') {
array_push($updates, $property . " =:" . $property);
}
}
$sql = 'UPDATE ' . $table . ' SET ' . join(', ', $updates) . ' WHERE fulcrum_id = :fulcrum_id';
$properties = (array) $properties;
unset($properties['marker-color']);
} elseif ($payload['type'] === 'record.delete') {
$sql = 'DELETE FROM ' . $table . ' WHERE fulcrum_id = :fulcrum_id';
$properties = ['fulcrum_id'=>$payload['data']['id']];
}
try {
array_walk($properties, function (&$item) {
$item = strval($item);
}); # Replace nulls with empty strings
$stmt = $db->prepare($sql);
$stmt->execute($properties);
$db = null;
echo '{"success":{"text":"Success!"}}';
} catch(PDOException $e) {
echo '{"error":{"text":' . $e->getMessage() . '}}';
}
}
?>
以上是关于php Fulcrum Webhook脚本,用于将数据共享同步到MySQL数据库。需要PHP并启用PDO和allow_url_fopen。的主要内容,如果未能解决你的问题,请参考以下文章
接收不到shopify webhook 发送post请求
无法将我的网站注册为用于配置来自 google 的 webhook 通知的 webhook
在 PHP 中使用 webhook 将文件上传到 Discord
Telegram 向 webhook 发送重复的 POST JSON 请求
如何将标头(用于 jenkins crumb)添加到 github webhook?
如何使用 PHP 发送不和谐的 webhook?