PHP code example of jouwweb / sendcloud

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

    

jouwweb / sendcloud example snippets


use JouwWeb\Sendcloud\Client;
use JouwWeb\Sendcloud\Model\Address;
use JouwWeb\Sendcloud\Model\Parcel;
use JouwWeb\Sendcloud\Model\ParcelItem;
use JouwWeb\Sendcloud\Model\WebhookEvent;
use JouwWeb\Sendcloud\Exception\SendcloudRequestException;

$client = new Client('your_public_key', 'your_secret_key');

// Print prices for all enabled shipping methods that ship to the Netherlands
foreach ($client->getShippingMethods() as $shippingMethod) {
    $price = $shippingMethod->getPriceForCountry('NL');
    if ($price) {
        echo $shippingMethod->getName() . ': €' . ($price / 100) . PHP_EOL;
    }
}

// Create a parcel and label
try {
    // Most of these arguments are optional and will fall back to defaults configured in Sendcloud
    $parcel = $client->createParcel(
        shippingAddress: new Address(
            name: 'John Doe',
            companyName: 'Big Box Co.',
            addressLine1: 'Office Street 2834A',
            city: 'Metropolis',
            postalCode: '9999ZZ',
            countryCode: 'NL',
            emailAddress: '[email protected]',
            phoneNumber: '+31612345678'
        ),
        servicePointId: null,
        orderNumber: '20190001',
        weight: 2500, // 2.5kg
        // Below options are only 

use JouwWeb\Sendcloud\ServicePointsClient;
use JouwWeb\Sendcloud\Exception\SendcloudRequestException;

$client = new ServicePointsClient('your_public_key', 'your_secret_key');

try {
    // Search for service points in the Netherlands.
    $servicePoints = $client->searchServicePoints('NL');

    var_dump($servicePoints[0]->isActive()); // bool(true)
    var_dump($servicePoints[0]->getName()); // string(7) "Primera"
    var_dump($servicePoints[0]->getCarrier()); // string(6) "postnl"
    var_dump($servicePoints[0]->getDistance()); // NULL ↓

    // If we want Sendcloud to calculate the distance between us and each service point, we need to supply latitude and
    // longitude.
    $servicePointsWithDistance = $client->searchServicePoints(
        country: 'NL',
        latitude: 51.4350511,
        longitude: 5.4746339
    );

    var_dump($servicePointsWithDistance[0]->getName()); // string(14) "Pakketautomaat"
    var_dump($servicePointsWithDistance[0]->getDistance()); // int(553)

    // Obtain a specific service point by ID.
    $servicePoint = $client->getServicePoint(1);
} catch (SendcloudRequestException $exception) {
    echo $exception->getMessage();
}