Skip to content

PHP quickstart

This page shows how to convert labels from PHP with the LabelZoom PHP SDK.

  • Install PHP 8.1 or later and Composer.
  • Optional: Get an API key. Without one, output has a watermark. See Authentication.

Install the labelzoom/sdk package from Packagist:

Terminal window
composer require labelzoom/sdk

Save the following program as convert.php:

<?php
require __DIR__ . '/vendor/autoload.php';
use LabelZoom\Sdk\LabelZoomClient;
$client = new LabelZoomClient(); // reads LABELZOOM_API_KEY
$result = $client->convert()
->fromZpl('^XA^FO50,50^ADN,36,20^FDLabelZoom^FS^XZ')
->withLabelSize(widthInches: 4, heightInches: 6)
->toPdf()
->execute();
$result->save('label.pdf');

The require line loads Composer’s autoloader, which makes the SDK classes available. The SDK reads your API key from the LABELZOOM_API_KEY environment variable. To pass a key explicitly, use new LabelZoomClient('API_KEY').

Run the program with php convert.php. It writes label.pdf.

$result = $client->convert()
->fromPdf(file_get_contents('shipping-label.pdf'))
->withPage(0) // 0-based; omit to convert every page
->toZpl()
->withDpi(203) // the printer's resolution
->execute();
echo $result->getText();

Write PDF, image, and printer-language (EPL, IPL, TSPL, DPL, SBPL) output as bytes ($result->getBytes()), not text; see Binary output.

$template = '^XA^FO50,50^A0N,30,30^FD{name}^FS^FO50,100^A0N,30,30^FD{sku}^FS^XZ';
$result = $client->convert()
->fromZpl($template)
->withData([ // one label per record
['name' => 'ACME Corp', 'sku' => '12345'],
['name' => 'Globex', 'sku' => '67890'],
])
->toZpl()
->execute();
echo $result->getText();

Each placeholder, such as {name}, takes its value from the matching key in each record; see Merge fields for the placeholder syntax.

use LabelZoom\Sdk\Exception\LabelZoomException;
try {
$client->convert()->fromZpl('^XA^FO50,50^ADN,36,20^FDLabelZoom^FS^XZ')->toPdf()->execute();
} catch (LabelZoomException $e) {
$code = $e->getErrorCode() ?? 'none'; // the error's `code`, when the response has one
error_log("HTTP {$e->getStatus()} ({$code}), request ID {$e->getRequestId()}: {$e->getMessage()}");
}

Every API error extends LabelZoomException. The SDK retries 429, 5xx, and network failures automatically before it throws. An invalid argument, such as a rotation that isn’t a multiple of 90, throws ValidationException before any request is sent. For status codes and code values, see Errors.

To call the API with PHP’s cURL extension instead of the SDK, send the label as the request body:

$zpl = '^XA^FO50,50^ADN,36,20^FDLabelZoom^FS^XZ';
// The API key is optional. Omit the Authorization header if it isn't set.
$headers = ['Content-Type: text/plain', 'Accept: */*'];
$apiKey = getenv('LABELZOOM_API_KEY');
if ($apiKey !== false && $apiKey !== '') {
$headers[] = 'Authorization: Bearer ' . $apiKey;
}
$query = http_build_query(['label.width' => 4, 'label.height' => 6]);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.labelzoom.com/api/v2/convert/zpl/to/pdf?' . $query,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $zpl,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($status !== 200) {
throw new RuntimeException("LabelZoom returned {$status}: {$body}");
}
file_put_contents('label.pdf', $body);

To convert a PDF to ZPL, post the file’s bytes to /convert/pdf/to/zpl with Content-Type: application/pdf, and read the response body as text.

To set options, send one params query parameter that holds JSON, or use dot notation such as label.width=4. Accept can be */*, the target’s media type, or a wildcard such as image/*. See Conversion parameters.