1. Go to this page and download the library: Download bohemicastudio/ppl-myapi 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/ */
use BohemicaStudio\PplMyApi\Enums\ProductList;
use BohemicaStudio\PplMyApi\Models\Data\Shipment\ShipmentBatch;
use BohemicaStudio\PplMyApi\Models\Data\Shipment\Shipment;
use BohemicaStudio\PplMyApi\Models\Data\Shipment\Address;
use BohemicaStudio\PplMyApi\Models\Data\Shipment\CashOnDelivery;
use BohemicaStudio\PplMyApi\Models\Data\Shipment\ShipmentSet;
use BohemicaStudio\PplMyApi\Models\Data\Shipment\LabelSettings;
use BohemicaStudio\PplMyApi\Models\Data\Shipment\CompleteLabelSettings;
use BohemicaStudio\PplMyApi\Services\PplMyApiShipmentService;
use BohemicaStudio\PplMyApi\PplMyApi;
use GuzzleHttp\Exception\ClientException;
// First, create a shipment batch. This is a basic example; you can add more fields as 6-8eef258586a5',
sender: new Address(
country: 'CZ',
zipCode: '15500',
name: 'Your Company Name',
name2: 'Your Company Name',
street: 'Your Street 10/1',
city: 'Praha 13',
contact: 'Your Name',
phone: '+420223456789',
email: '[email protected]',
),
cashOnDelivery: new CashOnDelivery(
codCurrency: 'CZK',
codPrice: 1000,
codVarSym: '1234567890',
),
shipmentSet: new ShipmentSet(
numberOfShipments: 2, // Number of packages in the shipment
shipmentSetItems: [],
),
),
],
labelSettings: new LabelSettings(
format: 'Pdf',
completeLabelSettings: new CompleteLabelSettings(
isCompleteLabelRequested: true,
pageSize: 'A4',
),
),
shipmentsOrderBy: 'ShipmentNumber',
);
$pplApi = new PplMyApi();
$service = new PplMyApiShipmentService($pplApi);
try {
$shipmentBatchUrl = $service->createShipmentBatch($shipmentBatch);
} catch (ClientException $e) {
// You may want to output the validation error
dd($e->getResponse()->getBody()->getContents());
}
// Ideally, you should create a delayed queued job for this, that will try this after 1 minute and repeat for let's say 10 minutes (as PPL API does not provide any webhook or callback)
// But if you are forced to do it in the same request, you can do it like this:
$attempts = 0;
while ($attempts < 24) {
sleep($attempts === 0 ? 3 : 2);
// Then we can get the shipment batch detail - parsing of shipment batch detail will work only if there is only one shipment in the batch
// Otherwise you need to parse the response yourself
$detail = $service->getShipmentBatchDetail($shipmentBatchUrl);
if ($detail->isFullyCompleted()) {
$count = 0;
foreach ($detail->items as $item) {
// Make sure the directory exists
$name = 'pdf/' . $item->shipmentNumber . '_' . $count . '.pdf';
file_put_contents(public_path($name), $pplApi->request($item->labelUrl)->getBody()->getContents());
$count++;
}
break;
}
$attempts++;
}