Ejemplo en PHP: almacenar asistencia biométrica en MySQL
Un controlador de callback en PHP funcional que guarda las marcaciones de asistencia en MySQL, en tres pasos.
Información adicional
Demo con programa PHP
Un ejemplo sencillo en PHP: cree la tabla, despliegue el script y empiece a recibir datos de asistencia.
Paso 1 — Cree la tabla de MySQL
cams_db_setup.sql
CREATE DATABASE IF NOT EXISTS cams_biometric;
USE cams_biometric;
CREATE TABLE IF NOT EXISTS attendance_logs (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id VARCHAR(50) NOT NULL,
log_time DATETIME NOT NULL,
punch_type VARCHAR(20) DEFAULT NULL,
input_type VARCHAR(20) DEFAULT NULL,
temperature DECIMAL(4,1) DEFAULT NULL,
face_mask TINYINT(1) DEFAULT NULL,
serial_number VARCHAR(50) DEFAULT NULL,
label_name VARCHAR(100) DEFAULT NULL,
raw_json TEXT DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY (user_id, log_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;Paso 2 — Controlador de callback en PHP
cams-callback.php
<?php
// Database config
$pdo = new PDO("mysql:host=localhost;dbname=cams_biometric;charset=utf8mb4", "root", "");
// Read incoming JSON
$rawInput = file_get_contents("php://input");
$data = json_decode($rawInput, true);
// Parse PunchLog and insert
if (isset($data['RealTime']['PunchLog'])) {
$log = $data['RealTime']['PunchLog'];
$stmt = $pdo->prepare("
INSERT INTO attendance_logs
(user_id, log_time, punch_type, input_type, temperature, face_mask, serial_number, label_name, raw_json)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE punch_type=VALUES(punch_type), input_type=VALUES(input_type)
");
$stmt->execute([
$log['UserId'] ?? '',
date('Y-m-d H:i:s', strtotime($log['LogTime'] ?? 'now')),
$log['Type'] ?? null,
$log['InputType'] ?? null,
$log['Temperature'] ?? null,
isset($log['FaceMask']) ? ($log['FaceMask'] ? 1 : 0) : null,
$data['RealTime']['SerialNumber'] ?? null,
$data['RealTime']['LabelName'] ?? null,
$rawInput
]);
}
// Always respond with this
header("Content-Type: application/json");
echo '{"status":"done"}';
?>Paso 3 — Despliegue y pruebe
1
Cree la tabla
Ejecute: mysql -u root -p < cams_db_setup.sql
2
Suba el archivo PHP
Edite las credenciales de la BD si es necesario y súbalo a su servidor.
3
Establezca la URL de callback
En API Monitor, establezca: https://yourserver.com/cams-callback.php
4
Marque y verifique
Genere una marcación en el dispositivo y luego compruebe: SELECT * FROM attendance_logs;