PHP code example of c24-toys / php-gls-shipping-sdk

1. Go to this page and download the library: Download c24-toys/php-gls-shipping-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/ */

    

c24-toys / php-gls-shipping-sdk example snippets


$logger = new \Psr\Log\NullLogger();
$serviceFactory = new \C24Toys\GLS\Sdk\ParcelProcessing\Service\ServiceFactory();
$service = $serviceFactory->createShipmentService('basicAuthUser', 'basicAuthPass', $logger, $sandbox = true);

// REGULAR SHIPMENT
$requestBuilder = new \C24Toys\GLS\Sdk\ParcelProcessing\RequestBuilder\ShipmentRequestBuilder();
$requestBuilder->setShipperAccount($shipperId = '98765 43210');
$requestBuilder->setRecipientAddress(
    $country = 'DE',
    $postalCode = '36286',
    $city = 'Neuenstein',
    $street = 'GLS-Germany-Straße 1 - 7',
    $name = 'Jane Doe'
);
$requestBuilder->addParcel($parcelWeightA = 0.95);
$requestBuilder->addParcel($parcelWeightB = 1.2);
$request = $requestBuilder->create();

$shipment = $service->createShipment($request);

// work with the web service response, e.g. persist label
foreach ($shipment->getLabels() as $i => $label) {
    file_put_contents("/tmp/{$shipment->getConsignmentId()}-{$i}.pdf", $label);
}

// RETURN SHIPMENT
$requestBuilder = new \C24Toys\GLS\Sdk\ParcelProcessing\RequestBuilder\ReturnShipmentRequestBuilder();
$requestBuilder->setShipperAccount($shipperId = '98765 43210');
$requestBuilder->setShipperAddress(
    $country = 'DE',
    $postalCode = '36286',
    $city = 'Neuenstein',
    $street = 'GLS-Germany-Straße 1 - 7',
    $name = 'Jane Doe'
);
$requestBuilder->setRecipientAddress(
    $country = 'DE',
    $postalCode = '36286',
    $city = 'Neuenstein',
    $street = 'GLS Germany-Straße 1 - 7',
    $company = 'GLS Germany'
);
$requestBuilder->addParcel($weight = 0.95, $qrCode = true);
$request = $requestBuilder->create();

$shipment = $service->createShipment($request);

$logger = new \Psr\Log\NullLogger();
$serviceFactory = new \C24Toys\GLS\Sdk\ParcelProcessing\Service\ServiceFactory();
$service = $serviceFactory->createCancellationService('basicAuthUser', 'basicAuthPass', $logger, $sandbox = true);

$cancelledIds = $service->cancelParcels([$parcelIdA = '12345', $parcelIdB = '54321']);
bash
$ composer 
bash
$ composer remove c24-toys/php-gls-shipping-sdk