1. Go to this page and download the library: Download taha-rafi/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/ */
taha-rafi / php-zkteco example snippets
use TahaRafi\ZKTeco\ZKTeco;
use TahaRafi\ZKTeco\Constants;
declare(strict_types=1);
use TahaRafi\ZKTeco\ZKTeco;
use TahaRafi\ZKTeco\Constants;
// Protocol options: Constants::PROTOCOL_AUTO (default), Constants::PROTOCOL_TCP, Constants::PROTOCOL_UDP
$zk = new ZKTeco('192.168.1.201', 4370, commKey: 0, protocol: Constants::PROTOCOL_AUTO);
if ($zk->connectWithRetry(maxAttempts: 3, retryDelayMs: 500)) {
echo "✅ Connected via: " . strtoupper((string)$zk->getActiveProtocol()) . "\n"; // TCP or UDP
$users = $zk->getUsersStructured();
$logs = $zk->getAttendanceStructured(since: '2026-10-01 00:00:00');
$zk->disconnect();
}
use TahaRafi\ZKTeco\ZKTeco;
use TahaRafi\ZKTeco\Constants;
$zk = new ZKTeco('192.168.1.201', 4370, commKey: 0, protocol: Constants::PROTOCOL_AUTO);
if ($zk->connect()) {
// A. Mark Check-In / Check-Out directly inside the physical ZKTeco machine's ATT_LOG memory!
$res = $zk->markAttendance(
userId: '1', // Enrolled User ID on the machine
state: Constants::ATT_STATE_CHECK_OUT, // Check-In (0), Check-Out (1), Break-Out (2), Break-In (3)
timestamp: '2026-10-18 18:00:00', // null = current time, or pass any 'YYYY-MM-DD HH:MM:SS'
verifyType: Constants::VERIFY_FINGERPRINT,
options: [
'write_to_device' => true, // Writes directly to physical machine ATT_LOG (CMD_SET_DATA 3001)
'device_feedback' => true, // Shows on device LCD & triggers "Thank You" voice
]
);
// $res['device_written'] === true
// B. Bulk-write multiple attendance records into the physical machine
$zk->pushAttendance([
['user_id' => '1', 'state' => Constants::ATT_STATE_CHECK_IN, 'timestamp' => '2026-10-18 09:00:00'],
['user_id' => '1', 'state' => Constants::ATT_STATE_CHECK_OUT, 'timestamp' => '2026-10-18 18:00:00'],
]);
// C. Selectively delete attendance records by time range (without wiping all logs!)
$zk->deleteAttendanceByTimeRange('2026-10-18 00:00:00', '2026-10-18 23:59:59');
$zk->disconnect();
}
use TahaRafi\ZKTeco\ZKTeco;
use TahaRafi\ZKTeco\Constants;
$zk = new ZKTeco('192.168.1.201', 4370, 0, Constants::PROTOCOL_AUTO);
if ($zk->connect()) {
// A. POST new attendance punches to your API in batches
$sync = $zk->postAttendance('https://erp.example.com/api/biometric/attendance', [
'token' => 'your-bearer-api-token',
'secret' => 'whsec_signing_secret',
'state_file' => __DIR__ . '/.zk_sync_cursor.json', // Tracks last synced timestamp automatically!
'batch_size' => 500,
'retries' => 3,
'clear_after_post' => false, // Set true to wipe device logs ONLY if all POST batches succeed
]);
// B. Bulk Push/POST employees (with RFID card numbers) FROM your app TO the ZKTeco device
$zk->pushUsers([
['uid' => 1, 'user_id' => '1001', 'name' => 'Taha Rafi', 'role' => Constants::LEVEL_SUPERADMIN, 'card_no' => 12345678],
['uid' => 2, 'user_id' => '1002', 'name' => 'Ali Khan', 'role' => Constants::LEVEL_USER, 'card_no' => 87654321],
]);
$zk->disconnect();
}
use TahaRafi\ZKTeco\ZKTeco;
use TahaRafi\ZKTeco\WebhookDispatcher;
$zk = new ZKTeco('192.168.1.201', 4370, 0);
$zk->addWebhook(
url: 'https://your-app.com/webhooks/zkteco',
secret: 'whsec_your_secret_key',
events: [
WebhookDispatcher::EVENT_ATTENDANCE_PUNCH,
WebhookDispatcher::EVENT_USER_CREATED,
WebhookDispatcher::EVENT_DOOR_UNLOCKED,
WebhookDispatcher::EVENT_DEVICE_ALARM,
]
);
if ($zk->connect()) {
// Listen continuously and fire webhooks in real time on every biometric scan
$zk->listenRealTime(function (array $punch, string $event): bool {
echo "⚡ Live Punch: User {$punch['user_id']} ({$punch['state_label']} via {$punch['verify_label']}) at {$punch['timestamp']}\n";
return true; // Return false to stop listening
});
}
use TahaRafi\ZKTeco\AdmsServer;
use TahaRafi\ZKTeco\WebhookDispatcher;
$webhooks = (new WebhookDispatcher())->addEndpoint('https://your-app.com/webhooks/zkteco', 'whsec_secret');
$adms = new AdmsServer($webhooks, __DIR__ . '/.adms_queue.json', timezoneOffset: 5);
$adms->onAttendance(function (string $serialNumber, array $records): void {
// Device POSTed real-time ATTLOG records directly to your server!
});
// Queue commands to push back to the device on its next /iclock/getrequest poll:
// $adms->queueUpdateUser('SERIAL123', '1001', 'Taha Rafi');
// $adms->queueUnlockDoor('SERIAL123');
$response = $adms->handleRequest();
http_response_code($response['status']);
echo $response['body'];
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.