PHP code example of soneritics / postnl
1. Go to this page and download the library: Download soneritics/postnl 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/ */
soneritics / postnl example snippets
$apiKey = '*YOUR API KEY*';
$endpoints = new Sandbox;
$customer = (new Customer)
->setCustomerCode('DEVC')
->setCustomerNumber('11223344')
->setAddress((new Address)
->setAddressType(AddressType::SENDER)
->setCompanyName('Soneritics')
->setStreet('De Rosmolen')
->setHouseNr('123')
->setZipcode('1234AB')
->setCity('Amsterdam')
->setCountrycode('NL')
);
$api = new API($apiKey, $customer, $endpoints);
$barcodeService = $api->getBarcodeService();
$barcode = $barcodeService->generateBarcode();
echo "generated barcode: {$barcode}\r\n";
$message = new Message(1, PrinterType::JPG);
$dimension = (new Dimension)->setWeight(1000);
$receivingAddress = (new Address)
->setAddressType(AddressType::RECEIVER)
->setCompanyName('Jordi Jolink')
->setStreet('Some street')
->setHouseNr('123')
->setZipcode('1234AB')
->setCity('Amsterdam')
->setCountrycode('NL');
$shipments = (new Shipments)->addShipment(
(new Shipment)
->setAddresses((new Addresses)->addAddress($receivingAddress))
->setBarcode($barcode)
->setDimension($dimension)
);
$result = $api->getLabellingService()->generateLabel($shipments, $message);
$labelContentsBase64 = $result['ResponseShipments'][0]['Labels'][0]['Content'];
$receivingAddress = (new Address)
->setAddressType(AddressType::RECEIVER)
->setCompanyName('Soneritics')
->setStreet('Some street')
->setHouseNr('123')
->setZipcode('1234AB')
->setCity('Amsterdam')
->setCountrycode('NL');
$deliveryOptions = [
DeliveryOptions::DAYTIME,
DeliveryOptions::EVENING,
DeliveryOptions::AFTERNOON,
DeliveryOptions::MORNING,
DeliveryOptions::NOON
];
$result = $api->getTimeframeService()->calculateTimeframes($receivingAddress, $deliveryOptions);
echo "Timeframes:\r\n";
print_r($result);
# Confirm time frame
$barcodeService = $api->getBarcodeService();
echo "Confirming timeframe..\r\n";
if (!empty($result['Timeframes']['Timeframe'])) {
$timeframe = array_pop($result['Timeframes']['Timeframe']);
if ($timeframe !== null) {
$deliveryTimestampStart = new DateTime($timeframe['Date'] . ' ' . $timeframe['Timeframes']['TimeframeTimeFrame']['From']);
$deliveryTimestampEnd = new DateTime($timeframe['Date'] . ' ' . $timeframe['Timeframes']['TimeframeTimeFrame']['To']);
$contact = (new Contact)->setEmail('[email protected] ');
$shipments = (new Shipments)->addShipment(
(new Shipment)
->setAddresses((new Addresses)->addAddress($receivingAddress))
->setBarcode($barcodeService->generateBarcode())
->setDimension($dimension)
->setDeliveryDate($deliveryTimestampStart)
->setDeliveryTimeStampStart($deliveryTimestampStart)
->setDeliveryTimeStampEnd($deliveryTimestampEnd)
->setContacts((new Contacts)->addContact($contact))
);
$result = $api->getLabellingService()->generateLabel($shipments, $message);
print_r($result);
}
}
echo "Timeframe confirmed.\r\n";
$receivingAddress = (new Address)
->setAddressType(AddressType::RECEIVER)
->setZipcode('1234AB')
->setCountrycode('NL');
$result = $api->getLocationsService()->getNearestLocations($receivingAddress);
print_r($result);
$result = $api->getLocationsService()->getNearestLocationsByGeocode(51.963807, 5.968984, 'NL');
if (!empty($result['GetLocationsResult']['ResponseLocation'])) {
$location = $result['GetLocationsResult']['ResponseLocation'][0];
print_r($location);
echo "Location info:\r\n";
$locationCode = $location['LocationCode'];
$retailNetworkID = $location['RetailNetworkID'];
print_r($api->getLocationsService()->getLocationInformation($locationCode, $retailNetworkID));
}
$result = $api->getLocationsService()->getNearestLocationsByGeocode(51.963807, 5.968984, 'NL');
if (!empty($result['GetLocationsResult']['ResponseLocation'])) {
$location = $result['GetLocationsResult']['ResponseLocation'][0];
$locationCode = $location['LocationCode'];
$retailNetworkID = $location['RetailNetworkID'];
$pickupAddress = (new \PostNL\Model\Address)
->setAddressType(\PostNL\Enum\AddressType::DELIVERY_ADDRESS_FOR_PICKUP)
->setCompanyName($location['Name'])
->setStreet($location['Address']['Street'])
->setHouseNr($location['Address']['HouseNr'])
->setZipcode($location['Address']['Zipcode'])
->setCity($location['Address']['City'])
->setCountrycode('NL');
$contact = (new Contact)->setEmail('[email protected] ');
$shipments = (new Shipments)->addShipment(
(new Shipment)
->setAddresses((new Addresses)->addAddress($receivingAddress)->addAddress($pickupAddress))
->setBarcode($barcodeService->generateBarcode())
->setDimension($dimension)
->setContacts((new Contacts)->addContact($contact))
->setProductCodeDelivery('3533')
->setDeliveryAddress(AddressType::DELIVERY_ADDRESS_FOR_PICKUP)
);
$result = $api->getLabellingService()->generateLabel($shipments, $message);
print_r($result);
}
$afzender = (new Afzender)
->setBedrijfsnaam('Bedrijfsnaam')
->setPostcode('1234AB')
->setHuisnummerPostbusnummer('1')
->setLandcode('NL');
$voormelding = (new Voormelding)
->setAfzender($afzender)
->setKlantCode($customerCode)
->setKlantNummer($customerNr)
->setVolgnummer($volgnummer)
->setAanleverLocatie($bls);
$pakket = (new Pakket)
->setGeadresseerdeBedrijfsnaam($company)
->setGeadresseerdeVoornaam($firstname)
->setGeadresseerdeAchternaam($lastname)
->setGeadresseerdePostcode($zipcode)
->setGeadresseerdeStraatnaam($streetname)
->setGeadresseerdeHuisnummerPostbusnummer($housenumber)
->setGeadresseerdeHuisnummerToevoeging($housenumberExtension)
->setGeadresseerdeWoonplaats($city)
->setGeadresseerdeLandcode($country)
->setEmailadres($email)
->setZendingcode($shipmentCode);
$voormelding->addPakket($pakket);
// Contents ophalen. Dit kan opgeslagen worden in een LST bestand.
$voormeldContents = $voormelding->genereerInhoud()