diff --git a/README.md b/README.md index dd13d97..42915a3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,89 @@ -# ZK Soap PHP Library +ZK Soap PHP Library +====== + A PHP Library For Manage Data From Fingerprint Machine with SOAP Protocol + +## Features + + * Get Attendance Log with DateRange + * Get User Information + +## Requirements + + * PHP version 7.2 or higher + * Fingerprint Machine Support ZK Web Service + +## Easy Installation + +### Install with composer + +To install with [Composer](https://getcomposer.org/), simply require the +latest version of this package. + +```bash +composer require fahriztx/zksoapphp +``` + +Make sure that the autoload file from Composer is loaded. + +```php +// somewhere early in your project's loading, require the Composer autoloader +// see: http://getcomposer.org/doc/00-intro.md +require 'vendor/autoload.php'; + +``` + +## Quick Start + +Just pass your IP, Port and Comkey : + +* Get Attendance + +```php +// reference the ZK Soap PHP namespace +use Fahriztx\Zksoapphp\Fingerprint; + +// initial +$machine = Fingerprint::connect('192.168.1.175', '80', '123456'); + +// get machine status +echo "Machine Status : ".$machine->getStatus(); // connect | disconnect + +// get all log data +print_r($machine->getAttendance()); // return Array of Attendance Log + +// get all log data with date +print_r($machine->getAttendance('all', '2022-05-01')); // return Array of Attendance Log + +// get all log data with date range +print_r($machine->getAttendance('all', '2022-05-01', '2022-05-10')); // return Array of Attendance Log + +// get specific pin log data +print_r($machine->getAttendance(1)); // return Array of Attendance Log +// OR Array +print_r($machine->getAttendance([1, 2])); // return Array of Attendance Log + +``` + +* Get User Information + +```php +// reference the ZK Soap PHP namespace +use Fahriztx\Zksoapphp\Fingerprint; + +// initial +$machine = Fingerprint::connect('192.168.1.175', '80', '123456'); + +// get machine status +echo "Machine Status : ".$machine->getStatus(); // connect | disconnect + +// get all user data +print_r($machine->getUserInfo()); // return Array of User Info Data + +// get specific pin user data +print_r($machine->getUserInfo(1)); // return Array of User Info Data +// OR Array +print_r($machine->getUserInfo([1, 2])); // return Array of User Info Data + +``` + diff --git a/examples/getAttendance.php b/examples/getAttendance.php index 8cca50e..01cd708 100644 --- a/examples/getAttendance.php +++ b/examples/getAttendance.php @@ -6,5 +6,5 @@ $machine = Fingerprint::connect('192.168.1.175', '80', '123456'); echo "Machine Status : ".$machine->getStatus().PHP_EOL; -print_r($machine->getAttendance()); +print_r($machine->getAttendance('all', '2022-05-24')); print_r($machine->getUserInfo()); \ No newline at end of file diff --git a/src/Fingerprint.php b/src/Fingerprint.php index d9bbe3f..eca54f0 100644 --- a/src/Fingerprint.php +++ b/src/Fingerprint.php @@ -6,14 +6,14 @@ */ class Fingerprint { - protected static $conn; - protected static $ip; - protected static $port; - protected static $comkey; - protected static $isConnected = false; - protected static $NL = "\r\n"; - - protected static $payload = [ + private static $conn; + private static $ip; + private static $port; + private static $comkey; + private static $isConnected = false; + private static $NL = "\r\n"; + + private static $payload = [ 'GetAttLog' => '#COMKEY#PIN', 'GetUserInfo' => '#COMKEY#PIN' ]; @@ -31,7 +31,7 @@ public function connect($ip, $port=80, $comkey=0) }else{ static::$isConnected = false; } - + return (new static); } @@ -78,7 +78,7 @@ public function getUserInfo($pin='all') return static::parseUserInfoData($buffer); } - static function parseUserInfoData($data="") { + private static function parseUserInfoData($data="") { $dataRow = explode("", $data); array_shift($dataRow); @@ -110,8 +110,12 @@ static function parseUserInfoData($data="") { return $userData; } - public function getAttendance($pin='all') + public function getAttendance($pin='all', $date_start=null, $date_end=null) { + if ($date_start != null && $date_end == null) { + $date_end = $date_start; + } + $payload = self::$payload['GetAttLog']; static::connect(static::$ip, static::$port, static::$comkey); @@ -145,11 +149,10 @@ public function getAttendance($pin='all') $buffer = str_replace($gaRes, "", $buffer); fclose(static::$conn); - return static::parseAttendanceData($buffer); + return static::parseAttendanceData($buffer, $date_start, $date_end); } - static function parseAttendanceData($data="") { - + private static function parseAttendanceData($data="", $date_start=null, $date_end=null) { $dataRow = explode("", $data); array_shift($dataRow); @@ -164,19 +167,37 @@ static function parseAttendanceData($data="") { $status = static::getValueFromTag($endRow, "", ""); $workcode = static::getValueFromTag($endRow, "", ""); - $fingerData[] = [ - 'pin' => $fid, - 'datetime' => $datetime, - 'verified' => $verified, - 'status' => $status, - 'workcode' => $workcode, - ]; + if ($date_start != null && $date_end != null) { + + $rangeDate = static::dateRange($date_start, $date_end); + + $dateCheck = explode(' ', $datetime)[0]; + + if (in_array($dateCheck, $rangeDate)) { + $fingerData[] = [ + 'pin' => $fid, + 'datetime' => $datetime, + 'verified' => $verified, + 'status' => $status, + 'workcode' => $workcode, + ]; + } + + } else { + $fingerData[] = [ + 'pin' => $fid, + 'datetime' => $datetime, + 'verified' => $verified, + 'status' => $status, + 'workcode' => $workcode, + ]; + } } return $fingerData; } - static function getValueFromTag($a, $b, $c) + private static function getValueFromTag($a, $b, $c) { $a = " ".$a; $hasil = ""; @@ -190,7 +211,25 @@ static function getValueFromTag($a, $b, $c) return $hasil; } - public function generateRequest($payload) + private static function dateRange($startDate, $endDate) + { + $dateRange = []; + $period = new \DatePeriod( + new \DateTime($startDate), + new \DateInterval('P1D'), + new \DateTime($endDate) + ); + + foreach ($period as $key => $value) { + array_push($dateRange, $value->format('Y-m-d')); + } + + array_push($dateRange, date('Y-m-d', strtotime($endDate))); + + return $dateRange; + } + + private function generateRequest($payload) { $payload = str_replace("#COMKEY", static::$comkey, $payload); fputs(static::$conn, "POST /iWsService HTTP/1.0".self::$NL);