PHP code example of very-code-com / furgonetka-api-php

1. Go to this page and download the library: Download very-code-com/furgonetka-api-php 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/ */

    

very-code-com / furgonetka-api-php example snippets


use VeryCodeCom\FurgonetkaApi\Auth\Store\FileTokenStore;
use VeryCodeCom\FurgonetkaApi\Dto\{Parcel, PriceScope, Pickup, Receiver, ShipmentRequest};
use VeryCodeCom\FurgonetkaApi\Enum\Carrier;
use VeryCodeCom\FurgonetkaApi\{FurgonetkaApiClient, FurgonetkaApiConfig};

$client = FurgonetkaApiClient::create(
    FurgonetkaApiConfig::fromEnv(),
    new FileTokenStore('/var/lib/shop/furgonetka-token.json'),
);

$service = $client->listServices()->first(Carrier::Dpd)           // your contract with the carrier
    ?? throw new RuntimeException('The account has no DPD contract.');

$request = new ShipmentRequest(
    serviceId: $service->id ?? throw new RuntimeException('The service has no id.'),
    pickup: new Pickup('Marszalkowska 100', '00-026', 'Warszawa', name: 'Jan Kowalski', countryCode: 'PL', email: '[email protected]', phone: '500600700'),
    receiver: new Receiver(name: 'Anna Nowak', street: 'Florianska 15', postcode: '31-021', city: 'Krakow', countryCode: 'PL', email: '[email protected]', phone: '600700800'),
    parcels: [Parcel::package(30, 40, 20, 5.0, 100.0, 'Books')],    // cm, kg, value in PLN
);

// Prices of several carriers at once; a carrier that cannot take the parcel is `available: false`.
$quotes = $client->calculatePrices($request, PriceScope::carriers(Carrier::Dpd, Carrier::InPost, Carrier::Gls));

// Create the shipment, then order it. A carrier's rejection is a result, not an exception.
$package = $client->createPackage($request);
$result  = $client->orderPackages([$package]);

if ($result->isSuccessful()) {
    file_put_contents('label.pdf', $client->fetchLabel($package)->bytes);
    echo implode(', ', $client->fetchPackage($package)->waybillNumbers());
} else {
    foreach ($result->errorsFor((string) $package->id) as $error) {
        echo $error->path, ': ', $error->message, PHP_EOL;
    }
}