PrintBase
  • Product
  • Pricing
  • Docs
  • Resources
PrintBase/Guides/Print from your language or framework

Print ESC/POS receipts from PHP

From PHP, curl an ESC/POS payload to the print API. Shared hosting is enough because the call is outbound HTTPS.

For PHP applications printing receipts

This page in another language: 中文

Overview

PHP's curl can POST JSON. Build the receipt as a binary string, base64_encode it, and set content_type to raw. You do not need the php-printer extension or a COM object on the web server.

The web server never opens the printer port. The shop computer runs the agent. That split is what makes shared hosting work.

This is the English counterpart of the Chinese PHP receipt page. The JSON fields are identical.

Create the job with POST /v1/print-jobs, an Authorization: Bearer header, printer_code, content_type pdf or raw, and exactly one of content or content_url. The free plan is 100 jobs a month on one computer.

Integration steps

  1. 01

    Create an API key on the server

    Use an API key (pb_live_…) in the process environment. Authorization is Bearer. Do not ship the key to browsers or mobile apps.

  2. 02

    Install the agent beside the printer

    Windows, macOS, or Linux. GET /v1/printers and keep the numeric code.

  3. 03

    POST /v1/print-jobs

    content_type is pdf or raw. Send exactly one of content (standard base64) or content_url.

  4. 04

    Reuse the idempotency key on retry

    A new key means a second physical print. HTTP 409 means the same key was reused with a different body.

  5. 05

    Read the job before you tell the user

    GET /v1/print-jobs/{id}. Paid plans can also receive print_job.completed and print_job.failed webhooks.

PHP

<?php
// PHP ESC/POS receipt
$payload = [
    'printer_code' => (int) getenv('PRINTER_CODE'),
    'content_type' => 'raw',
    'content' => base64_encode($receipt),,
    'copies' => 1,
];
$ch = curl_init('https://api.printbase.cloud/v1/print-jobs');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('PRINTBASE_API_KEY'),
        'Content-Type: application/json',
        'Idempotency-Key: ' . $orderId,
    ],
    CURLOPT_POSTFIELDS => json_encode($payload),
]);
$body = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 300) {
    throw new RuntimeException($body);
}
$job = json_decode($body, true); // id, status=queued

Details for this setup

Binary strings

ESC bytes are not UTF-8 text. Build them as a PHP string of bytes, then base64. json_encode will escape them if you forget to base64 first.

Timeouts

Set CURLOPT_TIMEOUT to a small number of seconds. The queued response should be fast.

Questions

ext-printer or Mike42?

Those talk to a local device. Use them only on the printer machine. A remote shop uses this HTTP call. Mike42 can still build the bytes you then base64.

Where is the key?

getenv or an env file outside the web root. Not in a repository.

Laravel? in this setup

See the Laravel pages. The curl body is the same. The call is still POST /v1/print-jobs with a Bearer API key, and HTTP 201 means queued rather than printed.

Chinese? in this setup

Encode GBK before base64 if that is the printer's code page. PHP's json_encode is only for the envelope.

Related guides

  • Print Laravel receipts from shared hosting

    Laravel on shared hosting can still print. The host only makes HTTPS. The receipt printer stays in the shop behind the agent.

  • Print ESC/POS receipts from Python

    Build ESC/POS bytes in Python and send them as raw. The agent does not transcode. Encode text before base64.

  • How to auto-print WooCommerce order receipts

    Print a WooCommerce order receipt from the order-status hook on your server. There is no PrintBase WooCommerce plugin; use the REST API.

Free plan: 100 jobs a month on one computer, no card.

Create a free accountRead the API docs
PrintBase

PrintBase connects your applications to physical printers anywhere - securely, reliably, and in real time.

Product
Receipt PrintingLabel PrintingDashboardDevicesWebhooks
Resources
DocsBlogStatusAPI ReferenceDownloadPrintBase vs PrintNodePrintNode PricingFAQ
Guides
All guidesIntegrationsPrintersCode samplesComparisonsUse casesTroubleshooting
Company
ContactSecurityPrivacy

© 2026 Aveniq Technologies OÜ. All rights reserved. PrintBase is a service of Aveniq Technologies OÜ.

PrivacyTermsRefunds