Productsup
Container API

PHP SDK

The official PHP package for interacting with the Container API.

4 min read

The recommended way to interact with the Container API in PHP connectors is the official SDK. It wraps all HTTP endpoints into a typed ContainerApiInterface.

Installation

composer require productsupcom/container-api-client

The package is hosted on GitHub. Add the repository to your composer.json if you haven't already:

"repositories": [
    { "type": "vcs", "url": "https://github.com/productsupcom/container-api-client" }
]

Setup with Symfony

Wire the Container API client using factory classes and Symfony's DI container.

Factory classes — create these to instantiate the Container API client:

src/ContainerApi/ContainerApiClientFactory.php
<?php

namespace App\ContainerApi;

use GuzzleHttp\Client;

class ContainerApiClientFactory
{
    public static function create(): Client
    {
        return new Client(['base_uri' => 'http://cde-container-api']);
    }
}
src/ContainerApi/ContainerApiFactory.php
<?php

namespace App\ContainerApi;

use GuzzleHttp\Client;
use Productsup\CDE\ContainerApi\ContainerApi;

class ContainerApiFactory
{
    public static function create(Client $client): ContainerApi
    {
        return new ContainerApi($client);
    }
}

Service definitions — register the factories in a config file:

config/packages/container_api.yaml
services:
    container.api.client:
        class: GuzzleHttp\Client
        factory: ['App\ContainerApi\ContainerApiClientFactory', 'create']

    container.api:
        class: Productsup\CDE\ContainerApi\ContainerApi
        factory: ['App\ContainerApi\ContainerApiFactory', 'create']
        arguments:
            - '@container.api.client'

DI binding — bind the interface so it can be injected anywhere:

config/services.yaml
services:
    _defaults:
        autowire: true
        autoconfigure: true
        bind:
            Productsup\CDE\ContainerApi\ContainerApiInterface $containerApi: '@container.api'

Now you can inject ContainerApiInterface into any service:

public function __construct(
    private ContainerApiInterface $containerApi,
) {}

Key methods

Writing output (data source connectors)

MethodDescription
appendToOutputFile(array $item)Write a single product
appendManyToOutputFile(array $items)Write a batch of products (auto-chunks large batches)
streamToOutput(\Generator $generator)Stream products from a generator — memory-efficient for large datasets

Reading input (export connectors)

MethodDescription
yieldFromInputFile()Iterate all products one by one
yieldBatchFromInputFile(int $size)Iterate products in batches
countItemsFromInputFile()Get total item count

Reading delta input (export-delta connectors)

MethodDescription
yieldFromNewFile()Products added since last run
yieldFromModifiedFile()Products changed since last run
yieldFromUnchangedFile()Products with no changes
yieldFromDeletedFile()Products removed since last run

Batch and count variants are also available: yieldBatchFromNewFile(int $size), countItemsFromNewFile(), etc.

Category specific attributes

All yield methods accept an optional array $options as their last argument. Pass ['classification-id' => $id] to filter by a CSA classification:

$this->containerApi->yieldFromNewFile(['classification-id' => $classification->id()]);
$this->containerApi->yieldBatchFromNewFile(100, ['classification-id' => $classification->id()]);

Use listClassifications() to retrieve all classifications configured for the current channel:

MethodDescription
listClassifications()Return all Classification objects for the current channel. Each exposes id() and path().

Writing feedback (export connectors)

MethodDescription
appendToFeedbackFile(array $item)Write a single feedback entry
appendManyToFeedbackFile(array $items)Write a batch of feedback entries

Logging

MethodDescription
info(string $message)Log an info message
warning(string $message)Log a warning
error(string $message)Log an error
debug(string $message)Log a debug message

Metadata

MethodDescription
showHeaders(string $type)Get column names of an input file
countItemsFromInputFile()Get item count

Keeping up to date

Keep the SDK updated to get the latest methods and bug fixes:

composer update productsupcom/container-api-client

On this page

Still stuck?

Reach out to our support team and we’ll help you get unstuck.

Contact support