PHP code example of sanmai / shipandco-sdk

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

    

sanmai / shipandco-sdk example snippets


$builder = new \ShipAndCoSDK\ClientBuilder();
$builder->setToken($token);

$client = $builder->build();

$request = new \ShipAndCoSDK\Requests\RatesRequest();

// This requests  > 0) {
    // Will not be printed because count() is zero here.
    echo 'Rates received: ', \count($response), "\n";
}

if ($response->hasErrors()) {
    // Check for exact errors
    foreach ($response->getMessages() as $message) {
        if ($message->getErrorCode() !== '') {
            // That's the error:
            echo "{$message->getErrorCode()}: {$message->getMessage()}\n";
        }
    }

    /** @var \ShipAndCoSDK\Responses\Bad\ErrorResponse $response */
    
    // To get more specific error details use response-specific fields. E.g.:
    foreach ($response->details as $detail) {
        echo "Error code: {$detail->code}\n\tMessage: {$detail->message}\n\tField: {$detail->field}\n";
    }
}

$request = new \ShipAndCoSDK\Requests\CarriersRequest();

$response = $client->sendCarriersRequest($request);

\var_dump(\count($response)); // Should print the number of configured carriers

foreach ($response as $value) {
    echo "{$value->id}\t{$value->type}\t{$value->state}\t{$value->created_at->format('Y-m-d')}\n";
    
    foreach ($value->credentials as $key => $value) {
        echo "\t$key =\t$value\n";
    }
}

$request = new \ShipAndCoSDK\Requests\AddressesRequest();

$response = $client->sendAddressesRequest($request);

\var_dump(\count($response)); // Should print the number of addresses returned

foreach ($response as $value) {
    echo "{$value->id}\t{$value->created_at->format('Y-m-d')}\n";
    
    foreach ($value->address as $key => $value) {
        echo "\t$key =\t$value\n";
    }
}

$request = new \ShipAndCoSDK\Requests\WarehousesRequest();

$response = $client->sendWarehousesRequest($request);

\var_dump(\count($response)); // Should print the number of warehouses returned

foreach ($response as $value) {
    echo "{$value->id}\t{$value->created_at->format('Y-m-d')}\t{$value->company}\n";
    
    foreach ($value->address as $key => $value) {
        echo "\t$key =\t$value\n";
    }
}

$request = new \ShipAndCoSDK\Requests\RatesRequest();

$request->from_address->country = 'JP';
$request->from_address->full_name = 'Yamada Taro';
$request->from_address->company = 'World Company';
$request->from_address->email = '[email protected]';
$request->from_address->phone = '08012341234';
$request->from_address->country = 'JP';
$request->from_address->address1 = 'OSAKAFU';
$request->from_address->address2 = 'OTECHO';
$request->from_address->province = 'OSAKA';
$request->from_address->zip = '5670883';
$request->from_address->city = 'IBARAKI SHI';

$request->to_address->full_name = 'John Doe';
$request->to_address->company = 'ACME';
$request->to_address->email = '[email protected]';
$request->to_address->phone = '0901231234';
$request->to_address->country = 'PT';
$request->to_address->address1 = 'Rua Maria Matos, 32';
$request->to_address->address2 = '';
$request->to_address->province = 'SETUBAL';
$request->to_address->zip = '2820-344';
$request->to_address->city = 'CHARNECA DA CAPARICA';

$product = $request->addProduct();
$product->quantity = 1;
$product->name = 'Example';
$product->price = 1000;

$parcel = $request->addParcel();
$parcel->weight = 200;
$parcel->width = 10;
$parcel->height = 10;
$parcel->depth = 10;

$request->customs->duty_paid = false;
$request->customs->content_type = 'MERCHANDISE';

$request->setup->date = new DateTime('+1 week');

$response = $client->sendRatesRequest($request);

\var_dump(\count($response));

foreach ($response as $rate) {
    echo "{$rate->carrier}\t{$rate->service}\t{$rate->price} {$rate->currency}\n";

    foreach ($rate->surcharges as $surcharge) {
        echo "\t{$surcharge->type}\t{$surcharge->price}\n";
    }
}

$request = new \ShipAndCoSDK\Requests\CreateShipmentRequest();

$request->to_address->country = 'JP';
$request->to_address->full_name = 'TEST TARO';
$request->to_address->phone = '1111111111';
$request->to_address->country = 'JP';
$request->to_address->address1 = '京都市中京区八百屋町117';
$request->to_address->zip = '604-8072';
$request->to_address->city = '京都府';

$request->from_address->full_name = 'テスト';
$request->from_address->phone = '08012341234';
$request->from_address->country = 'JP';
$request->from_address->address1 = 'OSAKAFU';
$request->from_address->province = 'OSAKA';
$request->from_address->zip = '1234567';
$request->from_address->city = 'IBARAKI SHI';

$product = $request->addProduct();
$product->name = 'Blue Basketball';
$product->quantity = 2;
$product->price = 4850;
$product->origin_country = 'JP';

$product = $request->addProduct();
$product->name = 'Orange Basketball';
$product->quantity = 1;
$product->price = 7850;
$product->origin_country = 'JP';

$parcel = $request->addParcel();
$parcel->amount = 1; // That's the default.
$parcel->weight = 2000;
$parcel->width = 10;
$parcel->height = 10;
$parcel->depth = 10;
// $parcel->package = 'fedex_envelope';

$request->customs->duty_paid = false;
$request->customs->content_type = 'MERCHANDISE';

$request->setup->carrier = 'sagawa';
$request->setup->service = 'sagawa_regular';
$request->setup->currency = 'JPY';
$request->setup->shipment_date = new DateTime('+1 day');
$request->setup->date = new DateTime('+2 day');
$request->setup->time = '16-18';
$request->setup->insurance = 0;
$request->setup->ref_number = '';
$request->setup->delivery_note = '';
$request->setup->signature = false;
$request->setup->care->fragile = false;
$request->setup->care->side_up = false;
$request->setup->care->valuable_goods = false;
$request->setup->pack_size = '0';
$request->setup->pack_amount = 3;
$request->setup->cash_on_delivery->amount = 1000;
$request->setup->cash_on_delivery->tax = 100;
$request->setup->return_label = false;
$request->setup->print_start_location = 1;
$request->setup->test = true;

$response = $client->sendCreateShipmentRequest($request);

if ($response->hasErrors()) {
    // Check for exact errors
    foreach ($response->getMessages() as $message) {
        if ($message->getErrorCode() !== '') {
            // That's the error
            echo "{$message->getErrorCode()}: {$message->getMessage()}\n";
        }
    }

    return; // This is a failure.
}

/** @var $response \ShipAndCoSDK\Responses\ShipmentResponse */
echo "{$response->id}\t{$response->state}\n";

echo "Shipping Label: {$response->delivery->label}\n";

foreach ($response->delivery->tracking_numbers as $trackingNumber) {
    echo "Tracking Number: {$trackingNumber}\n";
}

$request->setup->test = false;
bash
export SHIPANDCO_ACCESS_TOKEN=... (your token)
php examples/030_CarriersRequest.php