1. Go to this page and download the library: Download 0mithun/php-zkteco 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/ */
0mithun / php-zkteco example snippets
use Mithun\PhpZkteco\Libs\ZKTeco;
// Create instance with TCP protocol (recommended for stability)
$zk = new ZKTeco('192.168.1.100', 4370, protocol: 'tcp');
// Or use UDP protocol (default)
$zk = new ZKTeco('192.168.1.100', 4370, protocol: 'udp');
// Connect to device
if ($zk->connect()) {
// Get all users
$users = $zk->getUsers();
// Get attendance logs
$attendance = $zk->getAttendances();
// Disconnect when done
$zk->disconnect();
}
use Mithun\PhpZkteco\Libs\ZKTeco;
$zk = new ZKTeco(
host: 'device-1.proxy.example.com', // subdomain.base_domain
port: 4370, // ZKTeco device port (default)
tcpmux: [
'subdomain' => 'device-1',
'port' => 1337, // TCPMUX proxy port
]
);
if ($zk->connect()) {
$users = $zk->getUsers();
$zk->disconnect();
}
// Add a regular user
$zk->setUser(1, '10001', 'John Doe', '1234', 0, 0);
// Add an admin user
$zk->setUser(2, '10002', 'Admin User', '5678', 14, 0);
$zk->removeUser(1);
// Delete all users with role 0
$zk->deleteUsers(function($user) {
return $user['role'] == 0;
});
$zk->clearAllUsers();
$zk->clearAdminPriv();
// Get all attendance records
$logs = $zk->getAttendances();
// Each record contains:
// [
// 'uid' => 1,
// 'user_id' => 12345,
// 'state' => 1,
// 'record_time' => '2026-02-17 09:00:00',
// 'type' => 0,
// 'device_ip' => '192.168.1.100'
// ]
// With filter callback (e.g., today's records only)
$todayLogs = $zk->getAttendances(function($record) {
if (str_starts_with($record['record_time'], date('Y-m-d'))) {
return $record;
}
return null;
});
$zk->clearAttendance();
// Listen for real-time attendance events
$zk->getRealTimeLogs(function($log) {
echo "User {$log['user_id']} punched at {$log['record_time']}\n";
// $log contains:
// [
// 'user_id' => '12345',
// 'record_time' => '2026-02-17 09:00:00',
// 'state' => 1,
// 'device_ip' => '192.168.1.100'
// ]
}, timeout: 60); // Listen for 60 seconds
// Or listen indefinitely (Ctrl+C to stop)
$zk->getRealTimeLogs(function($log) {
// Process attendance in real-time
saveToDatabase($log);
});
use Mithun\PhpZkteco\Libs\Services\Util;
// Listen for attendance AND user enrollment events
$events = Util::EF_ATTLOG | Util::EF_ENROLLUSER | Util::EF_ENROLLFINGER;
$zk->getRealTimeEvents(function($event) {
echo "Event: {$event['event_name']}\n";
switch ($event['event_type']) {
case Util::EF_ATTLOG:
echo "Attendance: User {$event['user_id']} at {$event['record_time']}\n";
break;
case Util::EF_ENROLLUSER:
echo "New user enrolled: {$event['user_id']}\n";
break;
case Util::EF_ENROLLFINGER:
echo "Fingerprint enrolled for user {$event['user_id']}, finger {$event['finger_index']}\n";
break;
case Util::EF_UNLOCK:
echo "Door {$event['door_id']} unlocked\n";
break;
case Util::EF_ALARM:
echo "Alarm triggered: type {$event['alarm_type']}\n";
break;
}
}, $events, timeout: 3600); // Listen for 1 hour