Production-Grade PHP Driver's License OCR via REST API

Get structured results in seconds without maintaining a single regex pattern.

Why Driver's License OCR is Difficult

Building in-house Driver's License OCR is a significant engineering challenge beyond generic text extraction. Open-source tools like Tesseract fail on low-quality, real-world images due to glare, shadows, and non-standard fonts. The core problem lies in parsing the PDF417 barcode, which contains critical data but is susceptible to distortion and damage. Furthermore, each jurisdiction has unique layouts and field names, requiring a complex and brittle web of RegEx patterns. Manually implementing checksum validation for MRZ or document numbers adds another layer of complexity, leading to high maintenance costs and inconsistent accuracy.

Enterprise-Grade Extraction with StructOCR

StructOCR bypasses the limitations of generic OCR engines, offering a specialized driver license verification api. Our API leverages pre-trained Deep Learning models specifically designed for identity documents, including various forms of driving permits. Upon receiving an image, our system performs automatic pre-processing, including deskewing, denoising, and glare removal, before analysis. This ensures high accuracy even on suboptimal inputs. Unlike Tesseract, which returns unstructured lines of text, StructOCR provides a standardized JSON output with validated fields like `date_of_birth` and `document_number`. This eliminates the need for post-processing and manual data correction, reducing development time from months to hours. To check the output on your own sample first, upload a photo to the free online driver license scanner.

Production Use Cases

  • Digital Onboarding (KYC): Reduce drop-off rates by pre-filling user data from Driver's Licenses in < 2 seconds.
  • Fraud Prevention: Detect tampered fonts or mismatched PDF417 checksums automatically.
  • Vehicle & Equipment Rental: Instantly verify driver age and extract vehicle class endorsements (A, B, C, M) to streamline rentals.
Need more testing? Create a free account to get 200 free credits (equals 100 Driver License scans).

Implementation: Raw API Request

The following PHP code demonstrates a complete extraction flow using cURL. It handles image encoding, sets the required 'x-api-key' header, and parses the structured JSON response.

Prerequisite: PHP 7.4+ with cURL extension

PHP EXAMPLE
<?php

// Get 200 free credits:
// 👉 https://structocr.com/register

$apiKey = 'YOUR_API_KEY_HERE';
$apiUrl = 'https://api.structocr.com/v1/driver-license';
$imagePath = 'license.jpg';

// 1. Validate and Encode Image
if (!file_exists($imagePath)) {
    die('Error: File not found.');
}

$imageData = file_get_contents($imagePath);
$base64Image = base64_encode($imageData);

// 2. Prepare JSON Payload
$payload = json_encode(['img' => $base64Image]);

// 3. Initialize cURL
$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => $apiUrl,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $payload,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'x-api-key: ' . $apiKey // Required Authentication Header
    ]
]);

// 4. Execute and Parse
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if (curl_errno($ch)) {
    die('cURL Error: ' . curl_error($ch));
}
curl_close($ch);

// 5. Handle Response
$result = json_decode($response, true);

if ($httpCode === 200 && isset($result['success']) && $result['success']) {
    $data = $result['data'];
    echo "✅ Extraction Successful!\n\n";
    echo "Name:          " . $data['given_names'] . " " . $data['surname'] . "\n";
    echo "Doc Number:    " . $data['document_number'] . "\n";
    echo "Region:        " . $data['region'] . " (" . $data['country_code'] . ")\n";
    echo "Vehicle Class: " . $data['vehicle_class'] . "\n";
    echo "Expiry:        " . $data['date_of_expiry'] . "\n";

    // Highlighting the advanced security feature extraction
    if (!empty($data['extra_details']['card_security_number'])) {
        echo "Security Code: " . $data['extra_details']['card_security_number'] . "\n";
    }
} else {
    echo "❌ API Error (Code $httpCode):\n";
    // Print error message from API if available
    if (isset($result['error'])) {
        echo "Error: " . $result['error'] . "\n";
    } else {
        echo $response;
    }
}

?>

Technical Specs

  • •Latency: < 4s (Average)
  • •Availability: Served from Cloudflare's global edge network
  • •Security: AES-256 Encryption & SOC2 Compliant
  • •Input: JPG, PNG, WebP (Base64 Encoded)
  • •Max File Size: 4.5MB
  • •Output: JSON (Structured Data)

Key Features

  • •True Global Compatibility: Out-of-the-box support for North American AAMVA standards, EU numbered fields, and complex LATAM layouts (e.g., Brazil CNH) without maintaining custom RegEx.
  • •Date Normalization: All dates automatically formatted to YYYY-MM-DD.
  • •Vehicle Class Parsing: Extracts allowed vehicle categories (e.g., A, B, C).

Sample JSON Output

StructOCR returns a normalized JSON object, regardless of the input image angle or quality.

{
  "success": true,
  "data": {
    "type": "drivers_license",
    "country_code": "USA",
    "region": "CA",
    "document_number": "E3802489",
    "personal_number": null,
    "surname": "IDNOOB",
    "given_names": "MING",
    "address": "750 GONZALEZ DR APT 6B, SAN FRANCISCO, CA 94512",
    "vehicle_class": "C",
    "sex": "F",
    "date_of_birth": "1988-06-06",
    "date_of_expiry": "2020-06-06",
    "date_of_issue": "2015-07-22",
    "extra_details": {
      "card_security_number": "06/09/2014599A5/DOFD/19",
      "fathers_name": null,
      "mothers_name": null,
      "rg_number": null
    }
  }
}

Frequently Asked Questions

How does StructOCR compare to AWS Textract or Google Vision?

General-purpose OCR services like Textract return an array of raw text lines and coordinates. You are still responsible for parsing this data and mapping it to meaningful fields. StructOCR is a specialized API that performs this final step, returning a structured JSON object with labeled fields like `surname` and `date_of_birth`, saving you significant development effort.

Do you store the uploaded images?

Only briefly. Uploaded images and results are kept for at most 24 hours for quality spot-checks, then deleted. We do not persist any customer PII on our servers.

How do you handle blurry or low-quality images?

Our API includes a mandatory, automated pre-processing pipeline that performs image enhancement, including de-noising, sharpening, and perspective correction, before the OCR models are executed. This maximizes accuracy on real-world mobile captures.

You May Also Like

Related tutorials, platform guides, and comparisons

Country Tutorial

UK Driver License OCR with Node.js SDK

Extract structured fields from a United Kingdom photocard driving licence with the StructOCR Node.js SDK and TypeScript + Express.

Node.js · Driver License
Country Tutorial

Australia Driver License OCR with Python SDK

Extract structured fields from a Australia state or territory driver licence with the StructOCR Python SDK and FastAPI.

Python · Driver License
Country Tutorial

US Driver License OCR with Python SDK

Extract structured fields from a United States state-issued driver license with the StructOCR Python SDK and FastAPI.

Python · Driver License
Country Tutorial

Australia Driver License OCR with Node.js SDK

Extract structured fields from a Australia state or territory driver licence with the StructOCR Node.js SDK and TypeScript + Express.

Node.js · Driver License
Country Tutorial

Philippines Driver License OCR with Node.js SDK

Extract structured fields from a Philippines LTO driver's license with the StructOCR Node.js SDK and TypeScript + Express.

Node.js · Driver License
Tutorial

Python Driver License OCR SDK & API

Stop struggling with manual data entry. Integrate our driver license OCR SDK (Python wrapper) to extract structured JSON in <3s. Upload an image for a free test! Secure, accurate, and developer-friendly.

Python · Driver License
Tutorial

Go Driver License OCR API

Upload an image for a free test! Struggling with manual driver's license data entry? Our Go API delivers structured JSON in seconds, with SOC 2 Type II and AES-256 encryption.

Go · Driver License
Country Tutorial

Canada Driver License OCR with Python SDK

Extract structured fields from a Canada provincial driver licence with the StructOCR Python SDK and FastAPI.

Python · Driver License
Country Tutorial

Canada Driver License OCR with Node.js SDK

Extract structured fields from a Canada provincial driver licence with the StructOCR Node.js SDK and TypeScript + Express.

Node.js · Driver License
Country Tutorial

Philippines Driver License OCR with Python SDK

Extract structured fields from a Philippines LTO driver's license with the StructOCR Python SDK and FastAPI.

Python · Driver License

Technical Comparisons & Integrations

Explore platform integrations and competitive analysis

From tutorial to production in 5 minutes.

You've seen the code. Now get your API key, grab your 200 free credits, and see it work with your own images. No credit card required.

Instant Access Cancel Anytime No Credit Card