1. Go to this page and download the library: Download andreracodex/php-zklib 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/ */
andreracodex / php-zklib example snippets
use ZKLib\ZKLib;
$zk = new ZKLib('192.168.1.100'); // Your device IP address
// Connect to device
if ($zk->connect()) {
echo "Connected successfully";
// Get device information
echo "Device Version: " . $zk->version();
echo "OS Version: " . $zk->osVersion();
echo "Serial Number: " . $zk->serialNumber();
// Set device time
$zk->setTime(date('Y-m-d H:i:s'));
// Disconnect
$zk->disconnect();
} else {
echo "Failed to connect";
}
$zk = new ZKLib('192.168.1.100');
if ($zk->connect()) {
$zk->disableDevice();
// Get all attendance records
$attendance = $zk->getAttendance();
foreach ($attendance as $record) {
echo "User ID: " . $record['user_id'] . "\n";
echo "Timestamp: " . $record['timestamp'] . "\n";
echo "Status: " . $record['status'] . "\n";
}
$zk->enableDevice();
$zk->disconnect();
}
$zk = new ZKLib('192.168.1.100');
if ($zk->connect()) {
// Get all users
$users = $zk->getUser();
foreach ($users as $user) {
echo "User ID: " . $user['uid'] . "\n";
echo "Name: " . $user['name'] . "\n";
echo "Privilege: " . $user['privilege'] . "\n";
}
$zk->disconnect();
}
namespace App\Services;
use ZKLib\ZKLib;
class ZKDeviceService
{
protected $zk;
protected $deviceIp;
public function __construct($deviceIp)
{
$this->deviceIp = $deviceIp;
$this->zk = new ZKLib($deviceIp);
}
public function connect()
{
return $this->zk->connect();
}
public function disconnect()
{
$this->zk->disconnect();
}
public function getAttendance()
{
if ($this->connect()) {
$this->zk->disableDevice();
$attendance = $this->zk->getAttendance();
$this->zk->enableDevice();
$this->disconnect();
return $attendance;
}
return [];
}
public function getUsers()
{
if ($this->connect()) {
$users = $this->zk->getUser();
$this->disconnect();
return $users;
}
return [];
}
public function getDeviceInfo()
{
if ($this->connect()) {
$info = [
'version' => $this->zk->version(),
'os_version' => $this->zk->osVersion(),
'platform' => $this->zk->platform(),
'serial_number' => $this->zk->serialNumber(),
];
$this->disconnect();
return $info;
}
return [];
}
}
use App\Http\Controllers\AttendanceController;
Route::post('/attendance/sync', [AttendanceController::class, 'syncAttendance']);
Route::get('/users', [AttendanceController::class, 'getUsers']);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::create('attendances', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->timestamp('timestamp');
$table->tinyInteger('status')->default(0);
$table->timestamps();
$table->unique(['user_id', 'timestamp']);
});
}
public function down()
{
Schema::dropIfExists('attendances');
}
};
namespace App\Jobs;
use App\Services\ZKDeviceService;
use App\Models\Attendance;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SyncAttendanceJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
$zkService = new ZKDeviceService(config('services.zk.device_ip'));
$records = $zkService->getAttendance();
foreach ($records as $record) {
Attendance::updateOrCreate(
[
'user_id' => $record['user_id'],
'timestamp' => $record['timestamp'],
],
[
'status' => $record['status'] ?? 0,
]
);
}
}
}
bash
composer
bash
composer
bash
php artisan make:job SyncAttendanceJob
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.