PHP code example of leapocr / leapocr-php

1. Go to this page and download the library: Download leapocr/leapocr-php 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/ */

    

leapocr / leapocr-php example snippets




eapOCR\Enums\Format;
use LeapOCR\Enums\Model;
use LeapOCR\LeapOCR;
use LeapOCR\Models\ProcessOptions;

$client = new LeapOCR((string) getenv('LEAPOCR_API_KEY'));

$job = $client->ocr()->processUrl(
    'https://example.com/document.pdf',
    new ProcessOptions(
        format: Format::STRUCTURED,
        model: Model::STANDARD_V2,
        schema: [
            'type' => 'object',
            'properties' => [
                'title' => ['type' => 'string'],
            ],
            '

use LeapOCR\Enums\Format;
use LeapOCR\Enums\Model;
use LeapOCR\Models\ProcessOptions;

$job = $client->ocr()->processFile(
    __DIR__ . '/sample/test.pdf',
    new ProcessOptions(
        format: Format::STRUCTURED,
        model: Model::STANDARD_V2,
        instructions: 'Extract invoice number and total amount',
        schema: [
            'type' => 'object',
            'properties' => [
                'invoice_number' => ['type' => 'string'],
                'total_amount' => ['type' => 'number'],
            ],
            '

use LeapOCR\Models\PollOptions;

$result = $client->ocr()->waitUntilDone(
    $job->jobId,
    new PollOptions(
        pollIntervalSeconds: 2.0,
        maxWaitSeconds: 180.0,
    ),
);

use LeapOCR\Models\ProcessOptions;

$job = $client->ocr()->processFile(
    __DIR__ . '/sample/test.pdf',
    new ProcessOptions(templateSlug: 'invoice-template'),
);

$status = $client->ocr()->getJobStatus($job->jobId);

if ($status->status === \LeapOCR\Enums\JobStatusType::COMPLETED) {
    $result = $client->ocr()->getJobResult($job->jobId);
}

use LeapOCR\Config\ClientConfig;
use LeapOCR\LeapOCR;

$client = new LeapOCR(
    (string) getenv('LEAPOCR_API_KEY'),
    new ClientConfig(
        baseUrl: 'https://api.leapocr.com/api/v1',
        timeoutSeconds: 30.0,
        maxRetries: 3,
        retryDelayMilliseconds: 1000,
        retryMultiplier: 2.0,
    ),
);

use LeapOCR\Exceptions\AuthenticationException;
use LeapOCR\Exceptions\JobFailedException;
use LeapOCR\Exceptions\ValidationException;

try {
    $result = $client->ocr()->waitUntilDone($job->jobId);
} catch (AuthenticationException $exception) {
    // Invalid or missing API key
} catch (ValidationException $exception) {
    // Invalid request options
} catch (JobFailedException $exception) {
    // The job reached a failed terminal state
}



use LeapOCR\LeapOCR;

$rawBody = file_get_contents('php://input') ?: '';
$signature = $_SERVER['HTTP_X_WEBHOOK_SIGNATURE'] ?? '';
$timestamp = $_SERVER['HTTP_X_WEBHOOK_TIMESTAMP'] ?? '';
$secret = (string) getenv('LEAPOCR_WEBHOOK_SECRET');

if (!LeapOCR::verifyWebhookSignature($rawBody, $signature, $timestamp, $secret)) {
    http_response_code(401);
    echo 'Invalid signature';
    exit;
}

$payload = json_decode($rawBody, true, flags: JSON_THROW_ON_ERROR);
bash
composer 
bash
composer