Robust PHP API for Receipt OCR & Expense Data Extraction

Build scalable bookkeeping portals and expense management dashboards in PHP. Convert messy thermal receipts into structured arrays instantly.

PHP Receipt OCR data extraction process via cURL REST API for Laravel and Symfony backends
StructOCR empowers your PHP application to seamlessly convert messy mobile receipt uploads into structured, database-ready arrays.

The Memory Nightmare of Native PHP OCR

Processing mobile receipt uploads natively in PHP is a massive bottleneck. Relying on local OCR tools like Tesseract via PHP `shell_exec()` or bloated extensions quickly leads to 'Allowed memory size exhausted' errors, grinding web servers to a halt. Furthermore, extracting reliable financial data from a raw text dump is an unwinnable battle. Every retailer uses completely different layouts, thermal ink fades rapidly, and mobile photos are often wrinkled or shadowed, making regular expression (Regex) matching for taxes, tips, and line items completely futile.

The Cloud-Powered StructOCR Solution

StructOCR offers a streamlined, cloud-native alternative for PHP developers. Our receipt OCR API shifts the heavy machine learning workloads entirely off your web server. By simply capturing the `$_FILES` array upload, encoding the image to Base64, and making a quick HTTP request, our engine handles the complex perspective correction and semantic extraction. This allows you to effortlessly power expense management automation within your Laravel or Symfony applications, receiving clean JSON that decodes directly into native PHP associative arrays.

Perfect for PHP Web Applications

  • Laravel Accounting Portals: Enable small business clients to upload shoeboxes full of receipts, automatically categorizing line items into ledger entries without manual typing.
  • Employee Reimbursement Forms: Enhance internal HR portals by auto-filling expense claim inputs the moment a staff member uploads a picture of their dining or travel receipt.
  • Customer Loyalty Campaigns: Validate proof-of-purchase in marketing campaigns by programmatically scanning uploaded retail receipts for specific promotional SKUs.

Live Demo: Receipt scanner

No registration required. Upload a file to test the extraction.

1
Upload
2
Results

Drop files here or click to browse

JPG · PNG · WebP  ·  up to 500 files · max 4.5 MB each

No files selected
Need more testing? Create a free account to get 200 free credits (equals 200 Receipt scans).

Implementation: Native PHP cURL Request

A robust PHP script to extract deeply parsed expense data from a receipt using standard cURL. The output is easily mapped to your Eloquent models or database queries.

Prerequisite: PHP 7.4+ or PHP 8+ with the libcurl extension enabled.

CODE EXAMPLE
// 💰 Save hours of regex debugging. Get 200 free credits instantly:
// 👉 https://structocr.com/register

<?php

$imagePath = '/path/to/dinner_receipt.jpg'; // Path to your uploaded image
$apiKey = 'YOUR_API_KEY'; // Replace with your StructOCR API key

// 1. Safely read the image file and encode it to Base64
$imageData = file_get_contents($imagePath);
if ($imageData === false) {
    die('❌ Failed to read image file. Check file permissions.');
}
$base64Image = base64_encode($imageData);

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

// 3. Initialize the cURL session targeting the Receipt endpoint
$ch = curl_init('https://api.structocr.com/v1/receipt');

// 4. Configure cURL options for a POST request
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $payload,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'x-api-key: ' . $apiKey,
        'Content-Type: application/json'
    ],
]);

// 5. Execute the API request
$response = curl_exec($ch);

// 6. Handle cURL network errors
if (curl_errno($ch)) {
    die('❌ cURL error: ' . curl_error($ch));
}
curl_close($ch);

// 7. Decode the structured JSON response into a PHP associative array
$result = json_decode($response, true);

// 8. Safely parse the extracted expense data
if (isset($result['success']) && $result['success'] && $result['data']['is_valid']) {
    $data = $result['data'];
    
    echo "✅ Receipt Parsed Successfully!\n";
    echo "Merchant: " . $data['merchant_name'] . " (Confidence: " . $data['confidence'] . ")\n";
    echo "Date: " . $data['date'] . " " . $data['time'] . "\n";
    echo "Total: " . $data['total_amount'] . " " . $data['currency'] . "\n";
    echo "Tax: " . $data['tax_amount'] . "\n\n";
    
    // Loop through individual purchased items
    echo "--- Line Items ---\n";
    foreach ($data['items'] as $item) {
        echo "- " . $item['name'] . " (Qty: " . $item['quantity'] . ") = " . $item['price'] . "\n";
    }

} else {
    $errorMsg = $result['data']['validation_error'] ?? 'Unknown extraction error or invalid receipt.';
    echo "❌ Extraction Failed: " . $errorMsg . "\n";
}

?>

Technical Specs

  • Latency: < 3s (Optimized for synchronous web requests)
  • Uptime: 99.9% SLA
  • Security: Zero Data Retention (Strict GDPR & SOC2 Compliance)
  • Input: JPG, PNG, WebP (Max 4.5MB)
  • Output: Flat PHP Associative Array (via JSON)

Key Features

  • Framework Agnostic: Works beautifully with raw PHP `curl`, Laravel's `Http` facade (Guzzle), or Symfony's `HttpClient`.
  • Line-Item Precision: Goes beyond simple total extraction. Natively parses continuous-roll thermal receipts to capture individual product names and prices.
  • Low Memory Footprint: By offloading computer vision processing to our cloud, your PHP-FPM workers avoid memory spikes, keeping your server pool highly stable.

Sample JSON Response

The API handles all the complex spatial mapping, returning a predictable flat JSON structure that decodes perfectly into a PHP associative array.

{
  "success": true,
  "data": {
    "is_valid": true,
    "confidence": "high",
    "merchant_name": "Blue Bottle Coffee",
    "date": "2026-04-22",
    "time": "08:45 AM",
    "currency": "USD",
    "total_amount": 14.5,
    "tax_amount": 1.25,
    "items": [
      {
        "name": "Caffe Latte - Large",
        "quantity": 2,
        "price": "11.00"
      },
      {
        "name": "Butter Croissant",
        "quantity": 1,
        "price": "3.50"
      }
    ],
    "validation_error": null
  }
}

Frequently Asked Questions

Can I use GuzzleHttp or Laravel's HTTP Client instead of raw cURL?

Yes! StructOCR is a standard REST API. We highly recommend using Laravel's `Http::withHeaders()->post(...)` or Guzzle for a much cleaner, more modern implementation than native cURL.

How should I handle large 10MB iPhone photo uploads in `$_FILES`?

To stay under the API's 4.5MB limit and avoid PHP `memory_limit` exhaustion, we recommend using the GD library or the Intervention Image package to resize the temporary upload (e.g., to a max width of 1920px) and apply JPEG compression before encoding it to Base64.

Does the API successfully read handwritten tips on restaurant receipts?

Yes. Our spatial AI models are explicitly trained on hospitality data to distinguish between the printed subtotal, localized taxes, and handwritten gratuity lines, outputting the correct final total.

You May Also Like

Related tutorials, platform guides, and comparisons

Tutorial

Node.js Receipt OCR API SDK

Tutorial: Integrate the StructOCR Node.js SDK to extract structured data from retail receipts. Upload an image for a free test! Parse merchants, totals, and line items in your Javascript backends.

Node.js · Receipt
Tutorial

Python Receipt OCR API SDK

Tutorial: Extract merchants, line items, and totals from receipts using the StructOCR Python SDK. Upload an image for a free test! Ideal for fintech ETL pipelines, AI accounting, and Pandas dataframes.

Python · Receipt
Tutorial

Java Receipt OCR API

Tutorial: Integrate StructOCR's Receipt API into your Java enterprise applications. Upload an image for a free test! Extract line items, merchants, and taxes from POS receipts into structured JSON.

Java · Receipt
Tutorial

Go Receipt OCR API

Upload an image for a free test! Tutorial: Build scalable expense reporting apps in Go. Learn how to extract merchants, line items, and totals from POS receipts using our Golang OCR integration.

Go · Receipt
Tutorial

C# Receipt OCR API

Upload an image for a free test! Integrate a robust Receipt OCR API into your C# .NET app. Accurately extract merchants, line items, taxes, and tips from crumpled thermal receipts.

C# · Receipt
Tutorial

PHP License Plate OCR API

PHP License Plate OCR tutorial. Upload an image for a free test! Accurately extract global license plates, native scripts, colors, and vehicle types.

PHP · License Plate
Tutorial

PHP HIN (Hull Identification Number) OCR API

Tutorial: How to use the StructOCR PHP API to extract HIN from images. Upload an image for a free test! Includes native cURL code samples and marine OCR solutions.

PHP · HIN
Tutorial

PHP Passport & Visa OCR API

Enterprise-grade PHP Passport & Visa OCR API. Test our instant JSON extraction for free. Accurate ICAO 9303 & identity document parsing with zero RegEx required.

PHP · Passport
Tutorial

PHP VIN OCR API

Tutorial: How to use the StructOCR PHP Client to extract data from VINs. Upload an image for a free test! Includes code samples and JSON schema.

PHP · VIN
Tutorial

PHP Vehicle Registration OCR API

PHP Vehicle Registration OCR API tutorial. Upload an image for a free test! Accurately extract standardized fields like VIN, make, and model, along with dynamically localized data for global documents.

PHP · Vehicle Reg.

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 99.9% Uptime