PHP code example of docxtract / php-sdk

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

    

docxtract / php-sdk example snippets


$dx->authorised();   // is the key active?
$dx->models();       // which document types may it use?

use DocXtract\DocXtract;

$dx = new DocXtract('sk_your_api_key');

$result = $dx->extract('invoice.pdf', ['model' => 'invoice']);

echo $result['vendor'];              // array access into the extracted data
echo $result->get('line_items.0.hsn');  // dot paths for nested values
echo $result->pages();               // 1
echo $result->extractionId();        // stored-result id, if store_db was on

$result = $dx->extract('500-page-statement.pdf', ['model' => 'bank_statement']);

$result = $dx->extract('big.pdf', ['model' => 'invoice'], function ($done, $total, $stage) {
    echo "chunk {$done}/{$total}\n";
});

$dx = new DocXtract(['api_key' => '...', 'chunk_pause_ms' => 500]);

$manifest = $dx->splitDocument('big.pdf', ['model' => 'invoice']);

foreach ($manifest->chunks as $chunk) {
    $dx->processChunk($chunk->jobId, ['model' => 'invoice']);  // safe to retry
}

$result = $dx->collectResult($manifest->jobId);

if (!$result->isComplete()) {
    print_r($result->failedPages());   // retry those chunks
    print_r($result->pendingPages());
}

foreach ($dx->models() as $model) { /* ... */ }

use DocXtract\Exception\{RateLimitException, QuotaException, AuthenticationException,
    ExtractionFailedException, JobException, RequestException, ServerException,
    TransportException, DocXtractException};

try {
    $result = $dx->extract('invoice.pdf', ['model' => 'invoice']);
} catch (RateLimitException $e) {
    sleep($e->getRetryAfter() ?? 30);        // from X-RateLimit-Reset when the server sends it
} catch (QuotaException $e) {
    // usage_limit_exceeded | insufficient_credits — top up, do not retry
} catch (AuthenticationException $e) {
    // invalid_api_key | expired_api_key
} catch (RequestException $e) {
    // your input: invalid_file_type, file_too_large, unknown_model, ...
} catch (ExtractionFailedException $e) {
    // see the billing note below
} catch (JobException $e) {
    // job_expired | job_not_found | chunk_source_lost | chunk_in_progress
} catch (ServerException | TransportException $e) {
    // retryable
}

catch (DocXtractException $e) {
    match ($e->getErrorCode()) {
        'insufficient_credits' => $this->notifyBilling(),
        'unknown_model'        => $this->reportBadModel($e->getDetails()),
        default                => $e->isRetryable() ? $this->requeue() : throw $e,
    };
}

$dx = new DocXtract([
    'api_key'        => 'sk_...',                      // h'      => '/v3.1',                       // default
    'timeout'        => 120,                           // seconds per request
    'max_retries'    => 3,                             // retryable errors only
    'chunk_pause_ms' => 0,                             // delay between chunk calls
    'user_agent'     => '',                            // override the default UA
]);

$dx = new DocXtract(['api_key' => '...', 'base_path' => '/v3']);
bash
composer