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
- 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.
- 02
Install the agent beside the printer
Windows, macOS, or Linux. GET /v1/printers and keep the numeric code.
- 03
POST /v1/print-jobs
content_type is pdf or raw. Send exactly one of content (standard base64) or content_url.
- 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.
- 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.
Free plan: 100 jobs a month on one computer, no card.