PrintBase
  • Product
  • Pricing
  • Docs
  • Resources
PrintBase/专题/平台打印接入

自建 PHP 商城如何自动打印订单小票

在 PHP 订单支付逻辑里调用打印接口,把门店小票机打出来。不需要商城插件市场里的打印组件。调用 POST /v1/print-jobs,API Key 只放在服务端。

面向 维护自建 PHP 商城的小团队

说明

自建商城的支付成功函数是最合适的出票点。顾客浏览器只看到下单结果。小票在门店电脑上打出。不需要、本产品也没有针对某种商城程序的插件。打印机由门店电脑上的 Agent 主动连出,不需要公网 IP,也不需要把 9100 端口映射到互联网。打印机由门店电脑上的 Agent 主动连出,不需要公网 IP,也不需要把 9100 端口映射到互联网。

在支付回调里确认金额和订单状态后,用 PHP 的 curl 把 ESC/POS 发到 POST /v1/print-jobs。虚拟主机只要能访问外网 HTTPS 即可,不必把打印机端口映射到公网。

Idempotency-Key 用内部订单号。支付平台重复通知时保持 body 不变。API Key 放在服务器环境变量,不要写进仓库或前端模板。

创建任务使用 POST /v1/print-jobs,请求头 Authorization: Bearer,正文包含 printer_code、content_type(pdf 或 raw)以及 content 或 content_url 二者之一。免费套餐是一台电脑每月 100 个任务。

创建任务使用 POST /v1/print-jobs,请求头 Authorization: Bearer,正文包含 printer_code、content_type(pdf 或 raw)以及 content 或 content_url 二者之一。免费套餐是一台电脑每月 100 个任务。

接入步骤

  1. 01

    门店安装 Agent

    小票机在系统里测试页能打,再抄 printer code。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  2. 02

    只在服务端回调里打印

    支付宝或微信的异步通知到达后再打,避免未支付订单出纸。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  3. 03

    拼好 ESC/POS 再 base64

    中文用 GBK。切纸前走纸。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  4. 04

    curl 调用接口

    Authorization: Bearer,Content-Type: application/json,Idempotency-Key。

  5. 05

    把返回的 id 记到订单表

    客服查单时能看到是没发出去还是打印机离线。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

PHP

<?php
// 支付回调:订单小票
$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

这一套的细节

虚拟主机也够用

主机不连接打印机。它只向 api.printbase.cloud 或 cn.api.printbase.cloud 发 HTTPS。打印机由门店 Agent 负责。

怎样确认已经打出来

创建接口返回 id 和 status queued。之后是 queued、dispatched、printing、completed。失败原因包括 AGENT_OFFLINE、PRINTER_OFFLINE、DOWNLOAD_FAILED、PRINT_ERROR、INVALID_CONTENT。用 GET /v1/print-jobs/{id} 查询。Webhook 只在付费套餐提供,且只有 print_job.completed 和 print_job.failed。

常见问题

ThinkPHP 或 Laravel 都可以吗?

可以。差别只是 HTTP 客户端写在哪一层。接口形状相同。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

要开路由器端口吗?(详见本页)

不要。Agent 主动连出。打印机不必有公网 IP。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

订单改价后要重打?(详见本页)

使用新的 Idempotency-Key。旧 key 会返回第一次的任务。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

58 和 80 毫米怎么选?

按实际纸宽排版。接口不知道纸宽,也不会自动换行。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

相关页面

  • 用 PHP 远程打印小票

    PHP 用 curl 发送 ESC/POS。网站服务器不连接打印机。与英文 PHP 小票页是同一接口。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • 微信小程序如何自动打印小票

    小程序不能直接驱动店里的 USB 小票机。订单落到你的云函数或服务器后,再调用打印接口。没有官方小程序插件。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • 门店收银不买云打印机也能打小票

    收银电脑上已经有的小票机,装上 Agent 就能接收服务器发来的小票。不必换成带 SN 的云打印机。调用 POST /v1/print-jobs,API Key 只放在服务端。

免费套餐:一台电脑每月 100 个任务,不需要信用卡。

免费注册查看 API 文档
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