Get data from Fingerprint device using PHP

Get data from Fingerprint device using PHP

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Reliable Integration Patterns for Granding Fingerprint Devices in PHP

Integrating a Granding fingerprint device into a PHP and MySQL attendance system requires bypassing the lack of an official PHP SDK. Since the vendor only provides C# and ASP.NET libraries, you must rely on their existing SOAP web services or HTTP-based data feeds. The most robust approach involves treating the device as a legacy API endpoint rather than trying to reverse-engineer binary protocols via raw sockets.

Leveraging SOAP Web Services

Granding devices typically expose SOAP (Simple Object Access Protocol) interfaces for authentication, user management, and attendance downloads. This is generally more reliable than attempting to parse raw HTTP streams or use PHP SOCKETS directly, which often result in incomplete data dumps due to undocumented binary headers.

To connect without a native PHP library, you can utilize the built-in SoapClient class. This allows your script to communicate with the device's IP address using standard XML-based requests.

First, ensure you have access to the WSDL (Web Services Description Language) file from the Granding documentation or the device admin panel. If a WSDL is unavailable, you must manually construct the SOAP envelope based on the C# SDK examples provided by the vendor. The process usually involves three main steps: authentication, session establishment, and data retrieval.

Here is a conceptual structure for downloading attendance records via SOAP in PHP:

$host = '192.168.2.201';
$port = '80'; // Or 8080 if configured
$user = 'admin';
$pass = 'password';

// Create a new SoapClient instance
$client = new SoapClient(null, [
    'location' => "http://{$host}:{$port}/SoapService.asmx",
    'uri'      => 'http://tempuri.org/Attendance',
]);

// Authenticate to get a session token (example method names vary by firmware)
$loginResult = $client->Login($user, $pass);

if ($loginResult->Success) {
    // Set the session ID for subsequent calls
    $sessionId = $loginResult->SessionID;

    // Download attendance data for a specific date range
    // Note: Method names and parameters depend on specific Granding firmware version
    $attendanceData = $client->DownloadAttendance(
        $sessionId,
        '2013-09-10',
        '2013-09-10'
    );

    // Process the returned XML or JSON structure into MySQL records
    foreach ($attendanceData as $record) {
        // Logic to insert into database
        echo "Record found: {$record->Time}, User: {$record->UserID}\n";
    }
} else {
    echo "Authentication failed.";
}

Handling the Data Stream with cURL

If your specific Granding model uses a proprietary HTTP endpoint for data export (such as the /form/Download path mentioned in community discussions), direct cURL requests are viable but require careful parsing. The raw output from these endpoints often contains mixed line endings, tabs, or hidden delimiters that cause "messy" data when split by simple string functions.

The previous attempt using a loop to construct the URL and splitting the response by newlines is fragile because it assumes a fixed text format. A more stable pattern is to fetch the raw content and use str_getcsv() to handle tab or comma delimiters automatically, provided the output isn't binary.

$host = '192.168.2.201';
$date = '2013-09-10';

$url = "http://{$host}/form/Download?uid=ALL&sdate={$date}&edate={$date}";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Prevent hanging indefinitely
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($response && strpos($response, '<') === false) { // Check if it's likely raw text
    $lines = str_getcsv($response, "\t", [',']); 
    // Further sanitization logic needed here based on actual column structure
} else {
    // Handle HTTP error or XML response differently
}

Critical Implementation Considerations

  1. Session Management: Unlike REST APIs that often return data in a single request with tokens, fingerprint devices frequently rely on stateful sessions. If the SOAP Login call times out or if the device requires periodic heartbeat packets to keep the session alive, your PHP script must manage these timeouts explicitly. Failing to refresh a session can result in empty datasets despite successful authentication.
  2. Data Integrity: The "messy" data issue often stems from using explode("\n", $data) on text streams that might contain carriage returns (\r) or varying whitespace. Always normalize the input string before parsing columns.
  3. Firmware Variance: Granding devices run different firmware versions. A method that works on a ZK-40 model might fail on a newer Granding unit if the endpoint path or SOAP action has changed. Always verify the WSDL definitions against the specific device model label.

Why This Approach Works

By leveraging standard web protocols (SOAP or HTTP/HTTPS) rather than low-level socket manipulation, you gain better visibility into errors via HTTP status codes and XML namespaces. This allows for easier integration with your MySQL database using prepared statements to prevent SQL injection when processing the imported user IDs and timestamps.

For teams building large-scale attendance systems where reliability is paramount, wrapping these raw calls into a dedicated service layer ensures that device-specific quirks are isolated from your core business logic.

Our network