PHP code example of metaship-ru / metaship-php-sdk

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

    

metaship-ru / metaship-php-sdk example snippets




\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');

$client = new MetaShipAPIClient('URL','api-key', 'api-secret');

use MetaShipRU\MetaShipPHPSDK\Request\Offer\OfferRequest;
use GuzzleHttp\Exception\BadResponseException;
use MetaShipRU\MetaShipPHPSDK\Component\ResponseFormatter\ErrorResponseFormatter;
use MetaShipRU\MetaShipPHPSDK\Component\ResponseFormatter\OfferResponseFormatter;
use MetaShipRU\MetaShipPHPSDK\MetaShipAPIClient;

$offersRequest = new OfferRequest();
$offersRequest->cityTo = 'Москва';
$offersRequest->zipTo = 115612;
$offersRequest->paymentSum = 1000;
$offersRequest->weight = 1.2;
$offersRequest->length = 10;
$offersRequest->height = 10;
$offersRequest->width = 10;
$offersRequest->deliveryType = 'courier';

try {
    $response = $client->getOffers($offersRequest);
    $offerResponse = OfferResponseFormatter::format($response);
} catch (BadResponseException $exception) {
    $errorResponse = ErrorResponseFormatter::format($exception->getResponse());
}


use GuzzleHttp\Exception\BadResponseException;
use MetaShipRU\MetaShipPHPSDK\Component\ResponseFormatter\ErrorResponseFormatter;
use MetaShipRU\MetaShipPHPSDK\Component\ResponseFormatter\OrderResponseFormatter;
use MetaShipRU\MetaShipPHPSDK\MetaShipAPIClient;
use MetaShipRU\MetaShipPHPSDK\Request\Address\CreateAddressRequest;
use MetaShipRU\MetaShipPHPSDK\Request\Item\CreateItemRequest;
use MetaShipRU\MetaShipPHPSDK\Request\Order\CreateOrderRequest;
use MetaShipRU\MetaShipPHPSDK\Request\Recipient\CreateRecipientRequest;

$createOrderRequest = new CreateOrderRequest();
$createOrderRequest->deliveryType = 'courier';
$createOrderRequest->width = 10;
$createOrderRequest->height = 10;
$createOrderRequest->weight = 1.2;
$createOrderRequest->length = 10;
$createOrderRequest->paymentSum = 1000;
$createOrderRequest->assessedValue = 1000;
$createOrderRequest->deliveryName = 'Boxberry';
$createOrderRequest->shopNumber = 'Test-order-3';
$createOrderRequest->deliveryCost = 200;
$createOrderRequest->shopServiceCode = 3;
$createOrderRequest->prepaidValue = 0;

$createRecipientRequest = new CreateRecipientRequest();
$createRecipientRequest->email = '[email protected]';
$createRecipientRequest->firstName = 'Иван';
$createRecipientRequest->middleName = 'Иванович';
$createRecipientRequest->lastName = 'Иванов';
$createRecipientRequest->phone = '7(926)777-77-77';

$createAddressRequest = new CreateAddressRequest();
$createAddressRequest->zip = 115612;
$createAddressRequest->city = 'Москва';
$createAddressRequest->street = 'Тверская';
$createAddressRequest->house = '43';
$createAddressRequest->housing = '3';
$createAddressRequest->apartment = 'кв 14';

$createRecipientRequest->address = $createAddressRequest;

$createOrderRequest->recipient = $createRecipientRequest;

$createItemRequest = new CreateItemRequest();
$createItemRequest->name = 'iPhone 6s';
$createItemRequest->weight = 1;
$createItemRequest->cost = 100;
$createItemRequest->quantity = 1;
$createItemRequest->vatCode = '20';
$createItemRequest->vendorCode = 'W234HH-12';

$createOrderRequest->items = [$createItemRequest];

try {
    $response = $client->createOrder($createOrderRequest);
    $orderResponse = OrderResponseFormatter::format($response);
} catch (BadResponseException $exception) {
    $errorResponse = ErrorResponseFormatter::format($exception->getResponse());
}