PHP code example of sonnenglas / shiplogic-php-sdk

1. Go to this page and download the library: Download sonnenglas/shiplogic-php-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/ */

    

sonnenglas / shiplogic-php-sdk example snippets


use Sonnenglas\Shiplogic\Shiplogic;
use Sonnenglas\Shiplogic\ValueObjects\Address;
use Sonnenglas\Shiplogic\ValueObjects\Contact;
use Sonnenglas\Shiplogic\ValueObjects\Parcel;
use Sonnenglas\Shiplogic\ValueObjects\ShipmentRequest;
use Sonnenglas\Shiplogic\Enums\ServiceLevel;
use Sonnenglas\Shiplogic\Enums\AddressType;

// Shiplogic uses a single API host (api.shiplogic.com); sandbox vs production
// is decided by the API token, not the URL. `productionMode` is kept for
// backwards compatibility but no longer changes the base URI.
$shiplogic = new Shiplogic(apiToken: '...');
$shipmentService = $shiplogic->getShipmentService();

$sender = new Address(
    streetAddress: '16 Viljoen Street',
    localArea: 'Lorentzville',
    city: 'Johannesburg',
    zone: 'GP',
    country: 'ZA',
    code: '2094',
    company: 'Sonnenglas',
    type: AddressType::Business,
);

$recipient = new Address(
    streetAddress: '10 Midas Avenue',
    localArea: 'Olympus AH',
    city: 'Pretoria',
    zone: 'GP',
    country: 'ZA',
    code: '0081',
    type: AddressType::Residential,
);

$parcel = new Parcel(lengthCm: 30.0, widthCm: 20.0, heightCm: 10.0, weightKg: 2.0);

$request = new ShipmentRequest(
    collectionAddress: $sender,
    collectionContact: new Contact(name: 'Warehouse Op', mobileNumber: '+27110000000', email: '[email protected]'),
    deliveryAddress: $recipient,
    deliveryContact: new Contact(name: 'Jane Doe', mobileNumber: '+27821234567', email: '[email protected]'),
    parcels: [$parcel],
    serviceLevelCode: ServiceLevel::Economy,
    customerReference: 'ORDER-123',
);

$response = $shipmentService->createShipment($request);

echo $response->shortTrackingReference;        // e.g. "3BM3"
echo $response->customTrackingReference;       // e.g. "SLX3BM3"

$label = $shipmentService->getLabel($response->id);
echo $label->url;                              // signed S3 URL, valid 24h

use Sonnenglas\Shiplogic\Webhooks\TrackingEventPayload;

$payload = TrackingEventPayload::fromArray(json_decode($request->getBody(), true));
bash
composer