PHP code example of arzcode / laravel-correos

1. Go to this page and download the library: Download arzcode/laravel-correos 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/ */

    

arzcode / laravel-correos example snippets


use Arzcode\LaravelCorreos\Correos;

$correos = app(Correos::class);
$correos->preregister()->createShipments($request);

use Arzcode\LaravelCorreos\Facades\Correos;

Correos::preregister()->createShipments($request);

use Arzcode\LaravelCorreos\Data\Preregister\DeliveryRequestData;

$request = DeliveryRequestData::from([
    'shipments' => [
        [
            'product' => 'PAFXB',
            'deliveryMethod' => 'DOUAOF',
            'contractNumber' => '12345678',
            'clientNumber' => '1234567890',
            'labellerCode' => '0001',
            'packagesNumber' => '1',
            'sender' => [
                'name' => 'My Company',
                'address' => 'Calle Sender 1',
                'locality' => 'Madrid',
                'province' => '28',
                'cp' => '28001',
                'country' => 'ESP',
            ],
            'addressee' => [
                'name' => 'John Doe',
                'address' => 'Calle Receiver 2',
                'locality' => 'Barcelona',
                'province' => '08',
                'cp' => '08001',
                'country' => 'ESP',
            ],
            'packages' => [
                ['packageWeightGrams' => '500'],
            ],
        ],
    ],
]);

$correos->preregister()->validateShipments($request);   // dry run, no shipment created
$response = $correos->preregister()->createShipments($request);

$response->fileIdentifier;                            // "FILE001"
$response->shipments[0]->shipmentCode;                // "PQXYZ1234567890"
$response->shipments[0]->packages[0]->packageCode;    // "PQ1DR4A0000012345678"

use Arzcode\LaravelCorreos\Data\Labels\PrintLabelsRequestData;

$labels = $correos->labels()->printLabels(PrintLabelsRequestData::from([
    'documentationType' => 1, // 0=All, 1=Label, 2=CN22/CN23
    'print' => [
        'shipments' => ['PQXYZ1234567890'],
        'labelFormat' => 2,    // 1=XML, 2=PDF, 3=ZPL
        'labelPrintMode' => 1, // 1=A4, 2=Labeler
    ],
]));

$labels->pdf;            // Base64-encoded PDF content
$labels->decodedPdf();   // The same PDF as raw bytes, or null if there is none

use Arzcode\LaravelCorreos\Enums\LabelOrderType;

$labels = $correos->labels()->printLabels(PrintLabelsRequestData::from([
    'documentationType' => 1,
    'print' => [
        'shipments' => ['PQ1DR4A0000012345678'],
        'labelFormat' => 2,
        'labelPrintMode' => 1,
        'preregisterInd' => 1, // the codes are preregistered shipments
        'labelOrderType' => LabelOrderType::PackageId->value,
    ],
]));

use setasign\Fpdi\Fpdi;
use setasign\Fpdi\PdfParser\StreamReader;

$pdf = new Fpdi;
$pdf->AddPage();

$pages = $pdf->setSourceFile(
    StreamReader::createByString($labels->decodedPdf())
);

// 2 columns x 4 rows of 105mm x 74.25mm cells on A4.
foreach (range(1, $pages) as $cell => $page) {
    $pdf->useTemplate(
        $pdf->importPage($page),
        x: ($cell % 2) * 105,
        y: intdiv($cell, 2) * 74.25,
        width: 105,
    );
}

use Arzcode\LaravelCorreos\Data\Labels\PrintDocumentsRequestData;

$document = $correos->labels()->printDocuments(PrintDocumentsRequestData::from([
    'documentationType' => 5, // 5=DCAF, 6=DDP
    'documentData' => [
        'destinationName' => 'France',
        'contractNumber' => '12345678',
        'clientNumber' => '1234567890',
    ],
]));

$document->pdf;  // Base64-encoded PDF

$tracking = $correos->tracking()->searchShipment('PQ1DR4A0000012345678');

$tracking->code;          // "PQ1DR4A0000012345678"
$tracking->codProduct;    // "PQDOM"
$tracking->remitName;     // Sender name
$tracking->destiName;     // Addressee name

foreach ($tracking->events as $event) {
    $event->eventDate;     // "06/02/2026"
    $event->eventCode;     // "P010000V"
    $event->summaryText;   // "Shipment preregistered"
    $event->location;      // "CTA MADRID"
}

use Arzcode\LaravelCorreos\Enums\ProductCode;    // PaqPremium, PaqEstandar, PaqToday, ...
use Arzcode\LaravelCorreos\Enums\LabelPrintMode; // A4, Labeler

ProductCode::PaqPremium->label();  // "Paq Premium"
LabelPrintMode::options();         // [1 => 'A4 sheet', 2 => 'Labeler']

use Arzcode\LaravelCorreos\Exceptions\CorreosApiException;

try {
    $response = $correos->preregister()->createShipments($request);
} catch (CorreosApiException $e) {
    $e->getMessage();        // Error message from the API
    $e->getCode();           // HTTP status code
    $e->errorCode;           // Correos error code
    $e->moreInformation;     // Additional error details
    $e->getResponse();       // The raw Saloon response, for logging
}

$labels = $correos->labels()->printLabels($labelRequest);

// Never reached when Correos answered `{"pdf": null, "error": "El envío no existe"}`.
$pdf = $labels->decodedPdf();

$correos->labels()->lastResponse()?->body();

$packages = $correos->preregister()->getPackagesByReference('ORDER-10231');

if ($packages->packageCodes) {
    // Already registered: store the codes instead of creating the shipment again.
}

$clean = fn (array $values) => collect($values)
    ->map(fn ($value) => is_array($value) ? $clean($value) : $value)
    ->reject(fn ($value) => $value === null || $value === '' || $value === [])
    ->all();

$request = DeliveryRequestData::from($clean($this->form->getState()));

Action::make('label')
    ->action(fn (Shipment $record) => response()->streamDownload(
        fn () => print $correos->labels()->printLabels($record->labelRequest())->decodedPdf(),
        "etiqueta-{$record->shipment_code}.pdf",
    ));

} catch (CorreosApiException $e) {
    Notification::make()
        ->danger()
        ->title(__('The label could not be printed'))
        ->body($e->moreInformation ?? $e->errorCode)
        ->send();
}

// config/data.php
'livewire' => [
    'enable_synths' => true,
],