leapocr/leapocr-php
Composer 安装命令:
composer require leapocr/leapocr-php
包简介
Official PHP SDK for LeapOCR - Transform documents into structured data using AI-powered OCR.
README 文档
README
Official PHP SDK for LeapOCR - Transform documents into structured data using AI-powered OCR.
Overview
LeapOCR provides enterprise-grade document processing with AI-powered data extraction. This SDK offers a modern PHP interface for local file uploads, remote URL processing, job polling, and result retrieval.
Installation
composer require leapocr/leapocr-php
Quick Start
Prerequisites
- PHP 8.2 or higher
- LeapOCR API key (sign up here)
Basic Example
<?php require 'vendor/autoload.php'; use LeapOCR\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'], ], 'required' => ['title'], ], ), ); $result = $client->ocr()->waitUntilDone($job->jobId); var_dump($result->pages[0]->result);
Key Features
- Idiomatic PHP API - Enums, immutable value objects, and exception mapping for the public SDK surface
- Generated from the live API spec - Uses the real OpenAPI document from the running API
- SDK-only public surface - Generates and exposes only operations tagged for the SDK
- Direct file upload support - Handles presigned multipart upload flow for local files
- Polling helpers - Wait for completion with a single method call
- Structured and markdown output - Use templates or direct processing options
- Webhook verification helper - Verify incoming
X-R2-Signatureheaders with the raw request body
Processing Models
Use Model::STANDARD_V2 or Model::PRO_V2, or pass a custom model string through ProcessOptions.
Usage Examples
Processing a Local File
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'], ], 'required' => ['invoice_number', 'total_amount'], ], ), );
Waiting for Completion
use LeapOCR\Models\PollOptions; $result = $client->ocr()->waitUntilDone( $job->jobId, new PollOptions( pollIntervalSeconds: 2.0, maxWaitSeconds: 180.0, ), );
Using a Template
use LeapOCR\Models\ProcessOptions; $job = $client->ocr()->processFile( __DIR__ . '/sample/test.pdf', new ProcessOptions(templateSlug: 'invoice-template'), );
Getting Status and Results Separately
$status = $client->ocr()->getJobStatus($job->jobId); if ($status->status === \LeapOCR\Enums\JobStatusType::COMPLETED) { $result = $client->ocr()->getJobResult($job->jobId); }
For more runnable samples, see examples/.
Output Formats
| Format | Description | Use Case |
|---|---|---|
Format::STRUCTURED |
Single JSON object | Extract specific fields across the document |
Format::MARKDOWN |
Text per page | Convert a document into readable OCR text |
Configuration
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, ), );
Error Handling
The SDK throws typed exceptions for the public API:
AuthenticationExceptionValidationExceptionFileExceptionJobExceptionJobFailedExceptionJobTimeoutExceptionRateLimitExceptionNetworkExceptionApiException
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 }
Webhook Signature Verification
Use LeapOCR::verifyWebhookSignature() with the raw request body exactly as received. LeapOCR sends customer webhooks with X-Webhook-Signature and X-Webhook-Timestamp, and signs timestamp . "." . rawBody with your webhook secret.
<?php 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);
Do not verify against re-encoded JSON. Use the original body string and timestamp header from the HTTP request.
Development
Tooling
The SDK uses mise for local tooling:
mise install
mise exec php@8.3 ubi:composer/composer@2.8.12 -- composer install
Regenerating the SDK
The generated client is derived from the live API and then filtered down to SDK-tagged operations only:
make fetch-spec make filter-spec make generate
Running Tests
make test
LEAPOCR_API_KEY=your-api-key make test-integration
The integration suite reads LEAPOCR_BASE_URL and also accepts OCR_BASE_URL for parity with the other LeapOCR SDK test setups.
Publishing
Publish the PHP SDK to Packagist. Composer users install it with:
composer require leapocr/leapocr-php
Versions should come from Git tags such as v0.1.0, not from a hard-coded
version field in composer.json.
One-time setup
- Create the public repository at
https://github.com/leapocr/leapocr-php. - Submit that repository on Packagist as
leapocr/leapocr-php. - Optionally add GitHub Actions secrets for explicit Packagist refreshes:
PACKAGIST_USERNAMEPACKAGIST_TOKEN
Releasing
git tag -a v0.1.0 -m "Release v0.1.0"
git push origin v0.1.0
The release workflow will validate the package, run lint and unit tests, build a release archive, create a GitHub release, and notify Packagist when the Packagist secrets are configured.
For the full setup checklist, see .github/PACKAGIST_SETUP.md.
Generation Notes
- The OpenAPI spec is fetched from the running API at
http://localhost:8443/api/v1/docs/openapi.json scripts/filter_sdk_endpoints.pykeeps only SDK-tagged operations and rewrites them to a singleSDKtag- OpenAPI Generator produces the low-level client in
src/Generated - The public PHP API lives in
src
leapocr/leapocr-php 适用场景与选型建议
leapocr/leapocr-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「sdk」 「OCR」 「ai」 「document-extraction」 「document-processing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 leapocr/leapocr-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 leapocr/leapocr-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 leapocr/leapocr-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The Best Image Ocr SDK For BAT.
The Best Image Ocr SDK For BAT.
腾讯 OCR SDK
Integration of tesseract bridge via FFI and CLI
Alfabank REST API integration
Baidu OCR by smallerfan
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 45
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-19