PHP code example of justcommunication-ru / outofbox-ru-api-php-sdk

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

    

justcommunication-ru / outofbox-ru-api-php-sdk example snippets


$client = new OutofboxAPIClient('https://domain.ru', 'email', 'token');

$client = new OutofboxAPIClient('https://domain.ru', 'username', ''); // обязательно передать пустой token

$token = $client->getAuthToken($password);

var_dump($token);

$request = new ProductsListRequest();

$request
    ->setImagesSizes([
        'thumbnail' => [
            'fs' => 'ofb-320-240'
        ]
    ])
;

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

foreach ($response->getProducts() as $product) {
    echo $product->getTitle() . ' ' . $product->getFieldValue('Марка') . ' ' . $product->getFieldValue('Модель') . "\n";
    if ($product->withImages()) {
        echo $product-getImages()[0]->getUrl('thumbnail') . "\n";
    }
}

$request = new ProductsListRequest();
$request
    ->setInStock(true) // только в наличии на любом складе
    ->setInStock(false) // только НЕ в наличии на любом складе
    
    ->setStock(5643) // только в наличии в филиале с номером 5643
    ->setStocks([ 5643, 5644 ]) // только если позиция есть в наличии в филиалах 5643 или 5644
;

$request = ProductViewRequest::withProductID($id);
$request
    ->addImageSize('medium', [
        'fs' => 'ofb-640'
    ])
;

$response = $client->sendProductViewRequest($request);
$product = $response->getProduct();

echo $product->getTitle() . ' ' . $product->getFieldValue('Марка') . ' ' . $product->getFieldValue('Модель') . "\n";
if ($product->withImages()) {
    echo $product-getImages()[0]->getUrl('medium') . "\n";
}

$request = new CategoriesListRequest();
$response = $client->sendCategoriesListRequest($request);

foreach ($response->getCategories() as $category) {
    echo $category->getFullTitle(' > ') . "\n";
}

$request = new CategoriesListRequest(123); // список категорий для подкатегории с идентификатором 123

$request = new CategoriesListRequest();
$request->setParentId(123);

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

foreach ($response->getCategories() as $category) {
    echo $category->getFullTitle(' > ') . "\n";
}

$request = new StoresListRequest();
$response = $client->sendStoresListRequest($request);

foreach ($response->getStores() as $store) {
    echo $store->getName() . ' (№' . $store->getId() . ')' . "\n";
}

$request = new CreateShopOrderRequest();

$request
    ->setPhoneNumber('89688888888') // номер телефона
    ->setStoreId(123) // идентификатор филиала/склада
    // состав заказа
    ->setProducts([
        ProductShopOrderItem::create(1271231, 1), // Позиция с id = 1271231, 1 шт.
        ProductShopOrderItem::create(1271232, 2)  // Позиция с id = 1271232, 2 шт.
    ])
;

// Можно добавить еще один товар в список
$request->addProduct(ProductShopOrderItem::create(1271233, 3));

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

echo 'Заказ создан, его номер: ' . $response->getShopOrder()->getNumber();

use Outofbox\OutofboxSDK\API\ShopOrders\GetShopOrderRequest;
use Outofbox\OutofboxSDK\OutofboxAPIClient;

$client = new OutofboxAPIClient($domain, $username, $api_token);

// через объект запроса
$request = new GetShopOrderRequest();
$request->setOrderNumber('8189-071122');

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

$shopOrder = $response->getShopOrder();

// либо короче
$shopOrder = $client->getShopOrder('8189-071122');

$shopOrder->number; // номер заказа
$shopOrder->delivery_price; // стоимость доставки

if ($shopOrder->status) {
    $shopOrder->status->id; // идентификатор статуса заказа
    $shopOrder->status->value; // наименование статуса заказа
} else {
    // статус "Новый"
}

if ($shopOrder->deliveryMethod) {
    $shopOrder->deliveryMethod->id; // идентификатор способа доставки
    $shopOrder->deliveryMethod->value; // наименование способа доставки
}

if ($shopOrder->paymentMethod) {
    $shopOrder->paymentMethod->id; // идентификатор способа оплаты
    $shopOrder->paymentMethod->value; // наименование способа оплаты
}

// позиции заказа
foreach ($shopOrder->items as $shopOrderItem) {
    $shopOrderItem->id; // идентификатор позиции
    $shopOrderItem->title; // наименование позиции
    $shopOrderItem->price; // стоимость единицы позиции
    $shopOrderItem->quantity; // количество
    $shopOrderItem->amount; // общая стоимость
    $shopOrderItem->item_weight; // вес одной позиции
}

try {
    $client->sendProductViewRequest(ProductViewRequest::withProductID($id));
} catch (OutofboxAPIException $e) {
    $logger->error('Outofbox ERROR: ' . $e->getMessage());
}

$client = new OutofboxAPIClient('https://domain.ru', 'email', 'token', [
    'proxy' => 'tcp://localhost:8125',
    'timeout' => 6,
    'connect_timeout' => 4
]);

// Http клиент с логгированием всех запросов

$stack = HandlerStack::create();
$stack->push(Middleware::log($logger, new MessageFormatter(MessageFormatter::DEBUG)));

$httpClient = new \GuzzleHttp\Client([
    'handler' => $stack,
    'timeout' => 6
]);

$client = new OutofboxAPIClient('https://domain.ru', 'email', 'token', $httpClient);

$client = new OutofboxAPIClient('https://domain.ru', 'email', 'token');
$client->setHttpClient($httpClient);

$client->setLogger($someLogger);