PHP code example of altwebdesign / sameday-php-sdk

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

    

altwebdesign / sameday-php-sdk example snippets




// Initialization. Change user and password as needed for your account. For testing purposes (also implies different user/password) set a third parameter to 'https://sameday-api.demo.zitec.com'.
$samedayClient = new \Sameday\SamedayClient('user', 'password');
$sameday = new \Sameday\Sameday($samedayClient);

// Get list of available pickup points for client.
$pickupPoints = $sameday->getPickupPoints(new \Sameday\Requests\SamedayGetPickupPointsRequest());
// Use first found pickup point id. These ids are different for DEMO and PROD environments. This id can be cached on your application.
$pickupPointId = $pickupPoints->getPickupPoints()[0]->getId();

// Get list of available services for client.
$services = $sameday->getServices(new \Sameday\Requests\SamedayGetServicesRequest());
// Use first service id. These ids are different for DEMO and PROD environments. This id can be cached on your application.
// This is just for example purpose. Choose the right service for your app.
// For instance if requesting with 2H service (delivery in 2 hours) and cities are different (pickup point city and recipient city) then the validation will fail.
$serviceId = $services->getServices()[0]->getId();

try {
    $awb = $sameday->postAwb(new \Sameday\Requests\SamedayPostAwbRequest(
        $pickupPointId,
        null, // Contact person id can be left to NULL and default will be used.
        new \Sameday\Objects\Types\PackageType(\Sameday\Objects\Types\PackageType::PARCEL),
        [
            // This will generate an AWB expedition with 2 parcels (packages). Only the $weight is mandatory.
            new \Sameday\Objects\ParcelDimensionsObject(0.5),
            new \Sameday\Objects\ParcelDimensionsObject(3, 15, 28, 67)
        ],
        $serviceId,
        new \Sameday\Objects\Types\AwbPaymentType(\Sameday\Objects\Types\AwbPaymentType::CLIENT), // Who pays for the AWB. CLIENT is the only allowed value.
        new \Sameday\Objects\PostAwb\Request\AwbRecipientEntityObject('Huedin', 'Cluj', 'str. Otesani', 'Nume Destinatar', '0700111111', '[email protected]', new \Sameday\Objects\PostAwb\Request\CompanyEntityObject('nume companie SRL')), // AWB recipient. Please note that CompanyEntityObject is optional if the recipient is not company.
        0, // Insured value.
        100 // Cash on delivery value. Can be 0 if the payment was made online.
        // Other parameters may follow, see https://github.com/sameday-courier/php-sdk/blob/master/docs/reference/SamedayPostAwbRequest.md
    ));
} catch (\Sameday\Exceptions\SamedayBadRequestException $e) {
    // When request fails validation. Show the list of validation errors.
    var_dump($e->getErrors());
    exit;
} // Other exceptions may be thrown, see https://github.com/sameday-courier/php-sdk/blob/master/docs/reference.md#core-exceptions

$pdf = $sameday->getAwbPdf(new \Sameday\Requests\SamedayGetAwbPdfRequest($awb->getAwbNumber(), new \Sameday\Objects\Types\AwbPdfType(\Sameday\Objects\Types\AwbPdfType::A6)));
echo $pdf->getPdf();



$samedayClient = new \Sameday\SamedayClient('user', 'password');
$sameday = new \Sameday\Sameday($samedayClient);

// Get Romanian counties (default behaviour — countryCode omitted)
$roCounties = $sameday->getCounties(new \Sameday\Requests\SamedayGetCountiesRequest(''));

// Get Hungarian counties
$huCounties = $sameday->getCounties(new \Sameday\Requests\SamedayGetCountiesRequest('', 'HU'));

foreach ($huCounties->getCounties() as $county) {
    echo $county->getId() . ' — ' . $county->getName() . PHP_EOL;
}

// Get cities for a Romanian county (default)
$roCities = $sameday->getCities(new \Sameday\Requests\SamedayGetCitiesRequest($countyId));

// Get cities for a Hungarian county
// Constructor: ($countyId, $name, $postalCode, $countryCode)
$huCities = $sameday->getCities(new \Sameday\Requests\SamedayGetCitiesRequest($countyId, '', null, 'HU'));

foreach ($huCities->getCities() as $city) {
    echo $city->getId() . ' — ' . $city->getName() . PHP_EOL;
}

$request = new \Sameday\Requests\SamedayGetCountiesRequest('');
$request->setCountryCode('HU');

$counties = $sameday->getCounties($request);

use Sameday\Sameday;
use Sameday\SamedayClient;
use Sameday\Requests\SamedayGetCountiesRequest;
use Sameday\Requests\SamedayGetCitiesRequest;

$sameday = new Sameday(
    new SamedayClient(
        env('SAMEDAY_USER'),
        env('SAMEDAY_PASSWORD')
    )
);

public function getCounties()
{
    $data = \Cache::rememberForever('sameday_counties_hu', function () use ($sameday) {
        $counties = $sameday->getCounties(new SamedayGetCountiesRequest('', 'HU'));
        return json_decode($counties->getRawResponse()->getBody())->data;
    });

    return response()->json([
        'success' => 1,
        'counties' => $data,
    ]);
}

public function getCities($countyId)
{
    $data = \Cache::rememberForever('sameday_hu_cities_' . $countyId, function () use ($sameday, $countyId) {
        $cities = $sameday->getCities(new SamedayGetCitiesRequest($countyId, '', null, 'HU'));
        return json_decode($cities->getRawResponse()->getBody())->data;
    });

    return response()->json([
        'success' => 1,
        'cities' => $data,
    ]);
}
bash
composer