PHP code example of labelzoom / sdk

1. Go to this page and download the library: Download labelzoom/sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

labelzoom / sdk example snippets


use LabelZoom\Sdk\LabelZoomClient;

$client = new LabelZoomClient();            // anonymous; this works

$result = $client->convert()
    ->fromZpl('^XA^FO20,20^A0N,28^FDHello^FS^XZ')
    ->toPng()
    ->withDpi(300)
    ->withLabelSize(widthInches: 4, heightInches: 6)
    ->execute();

$result->save('label.png');

$client = new LabelZoomClient('lz_live_...');

$client->convert()->fromUrl($url)->toZpl();     // fine
$client->convert()->fromZpl($zpl)->to(TargetFormat::Url);
//                                  ^^^^^^^^^^^^^^^^^^^ Access to undefined constant

$client->convert()->fromFile(SourceFormat::Pdf, 'invoice.pdf')->toZpl()->execute();
$client->convert()->fromStream(SourceFormat::Png, $handle)->toZpl()->execute();
$client->convert()->fromBase64Text(SourceFormat::Pdf, $base64)->toZpl()->execute();

$result = $client->convert()
    ->fromZpl($template)
    ->toPdf()
    ->withData([
        ['sku' => 'A-1', 'qty' => 12],
        ['sku' => 'B-2', 'qty' => 4],
    ])
    ->execute();                       // a 2-page PDF

$result->getBytes();        // the document, exactly as sent
$result->getText();         // decoded using the response charset, for zpl/xml/json
$result->getContentType();
$result->getRequestId();    // X-LZ-Request-Id — quote it to support
$result->save('out.pdf');

use LabelZoom\Sdk\Exception\ForbiddenException;
use LabelZoom\Sdk\Exception\LabelZoomException;

try {
    $client->convert()->fromZpl($zpl)->toJson()->execute();
} catch (ForbiddenException $e) {
    if ($e->isPaidFeature()) {
        // "JSON export is a paid feature" — the most common free-tier failure.
        echo $e->getMessage() . ' (request ' . $e->getRequestId() . ')';
    }
} catch (LabelZoomException $e) {
    echo $e->getStatus() . ': ' . $e->getMessage();
}

$client = new LabelZoomClient(maxRetries: 0, timeout: 30.0);

$client = new LabelZoomClient(
    httpClient: new \GuzzleHttp\Client(['connect_timeout' => 2]),
);

$psr17 = new \GuzzleHttp\Psr7\HttpFactory();
$client = new LabelZoomClient(
    httpClient: $guzzle,
    requestFactory: $psr17,
    streamFactory: $psr17,
);

use LabelZoom\Sdk\LabelZoomClient;
use LabelZoom\Sdk\Sleeper;

$sleeper = new class implements Sleeper {
    /** @var list<float> */
    public array $slept = [];

    public function sleep(float $seconds): void
    {
        $this->slept[] = $seconds;
    }
};

$client = new LabelZoomClient(
    apiKey: null,
    httpClient: $yourPsr18Stub,
    sleeper: $sleeper,
    useJitter: false,          // deterministic backoff, so assertions can be exact
);

// ... provoke a retry, then:
assert($sleeper->slept === [1.0, 2.0]);