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.

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.
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.
// 💰 Save hours of regex debugging. Get 20 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.
More OCR Tutorials
PHP Shipping Container OCR API
Tutorial: Learn how to use the StructOCR PHP Client via cURL for shipping container OCR. Extract ISO 6346 container numbers with 99% accuracy. Includes code samples and JSON schemas.
PHP Driver License OCR SDK
Struggling with manual driver's license data entry? Our PHP OCR SDK provides AES-256 encrypted, SOC2 compliant data extraction in <5s, ensuring 98.5% uptime.
PHP HIN (Hull Identification Number) OCR API
Tutorial: How to use the StructOCR PHP API to extract Hull Identification Numbers (HIN) from images. Includes native cURL code samples and marine OCR solutions.
PHP Invoice Line Item OCR API
Struggling with manual invoice data entry? Our PHP invoice line item OCR API processes documents in <5s, outputting structured JSON. SOC2 compliant & AES-256 encrypted.
Precise Data Extraction and Seamless
Integration with AI-powered OCR API.
Empower your solutions with automated data extraction by
integrating best-in class StructOCR via API seamlessly.
No credit card required • Full API access included