commercetools-sdk-php-v2  6.0.2
The platform, import-api and ml-api PHP sdks generated from our api reference.

Client and Request Builder for making API requests against Commercetools.

Installation

composer require commercetools/commercetools-sdk

Usage

namespace Commercetools;
require_once __DIR__ . '/vendor/autoload.php';
$authConfig = new ClientCredentialsConfig(new ClientCredentials($clientId, $clientSecret));
$client = ClientFactory::of()->createGuzzleClient(
new Config(),
$authConfig
);

RequestBuilder

Detailed information of all available methods for the product API can be found here. For the Import API here and for the ML API here

Examples to retrieve project information

use GuzzleHttp\ClientInterface;
$builder = new ApiRequestBuilder($client);
$request = $builder->withProjectKey('your-project-key')->get();

To avoid specifying the project key for every request built it's possible to use the ones in the Commercetools\Client namespace instead

use GuzzleHttp\ClientInterface;
$builder = new ApiRequestBuilder('your-project-key', $client);
$request = $builder->categories()->get();
$importBuilder = new ImportRequestBuilder('your-project-key', $client);
$request = $importBuilder->importSinks()->get();
$mlBuilder = new MLRequestBuilder('your-project-key', $client);
$request = $mlBuilder->recommendations()->generalCategories()->get();

Executing requests

use GuzzleHttp\ClientInterface;
$builder = new ApiRequestBuilder('your-project-key', $client);
$request = $builder->with()->get();
// executing the request and mapping the response directly to a domain model
$project = $request->execute();
// send the request to get the response object
$response = $request->send();
// map the response to a domain model
$project = $request->mapFromResponse($response);
// send the request asynchronously
$promise = $request->sendAsync();
// map the response to a domain model
$project = $request->mapFromResponse($promise->wait());
// send the request using a client instance
$response = $client->send($request);
$project = $request->mapFromResponse($response);

Migration Guidelines

To migrate from the 1.x to the 2.x, there is a guideline below:

  • Migration guidelines from v1 to v2

Documentation

License

MIT

Commercetools\Client\MLRequestBuilder
Definition: MLRequestBuilder.php:13
Commercetools\Api\Client\Config
Definition: Config.php:14
Commercetools
Commercetools\Api\Client\ApiRequestBuilder
Definition: ApiRequestBuilder.php:16
Commercetools\Client\ApiRequestBuilder
Definition: ApiRequestBuilder.php:11
Commercetools\Api\Client\ClientCredentialsConfig
Definition: ClientCredentialsConfig.php:15
Commercetools\Client\ClientCredentials
Definition: ClientCredentials.php:15
Commercetools\Client\ClientFactory
Definition: ClientFactory.php:18
Commercetools\Client\ImportRequestBuilder
Definition: ImportRequestBuilder.php:10