commercetools / commercetools-sdk
The official PHP SDK for the commercetools Composable Commerce APIs
Installs: 169 679
Dependents: 0
Suggesters: 0
Security: 0
Stars: 17
Watchers: 64
Forks: 8
Open Issues: 4
Requires
- php: >=7.2
- ext-json: *
- guzzlehttp/guzzle: ^6.0 || ^7.0
- guzzlehttp/psr7: ^1.7 || ^2.0.0
- psr/cache: ^1.0 || ^2.0 || 3.0
- psr/http-client: ^1.0
- psr/http-message: ^1.0 || ^2.0
- psr/log: ^1.0 || ^2.0 || 3.0
- psr/simple-cache: ^1.0 || ^2.0 || 3.0
- ramsey/uuid: ^4.0
- symfony/cache: ^3.0 || ^4.0 || ^5.0 || ^6.0
Requires (Dev)
- cache/array-adapter: ^1.0
- commercetools/php-sdk: ^2.16
- monolog/monolog: ^1.3 || ^2.0
- phpunit/phpunit: ^9.0 | 8.5.22
- symplify/easy-coding-standard: 12.0.7
- vimeo/psalm: ^4.26
- dev-master
- 10.3.0
- 10.2.0
- 10.1.0
- 10.0.0
- 9.6.1
- 9.6.0
- 9.5.0
- 9.4.0
- 9.3.0
- 9.2.0
- 9.1.0
- 9.0.0
- 8.1.0
- 8.0.0
- 7.1.1
- 7.1.0
- 7.0.0
- 6.0.2
- 6.0.1
- 6.0.0
- v5.0.x-dev
- 5.0.1
- 5.0.0
- 4.4.0
- 4.3.0
- 4.2.0
- 4.1.0
- 4.0.0
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.0
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.0
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0
- 0.7.1
- 0.7.0
- 0.6.0
- 0.5.1
- 0.5.0
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
- dev-gen-sdk-updates
- dev-test-for-doc
- dev-update-documentation
- dev-CUT-1091-mcg
- dev-new-product-search-dfd
- dev-test-order
- dev-raml-remove-java-extends
- dev-TQL-282-docs-check-product-type-exists
- dev-update-order-by-import
- dev-feat-product-selection-variants
- dev-raml-api-reviews
- dev-raml-api-product-types
- dev-raml-api-subscriptions
- dev-raml-api-shipping-methods
- dev-raml-api-stores
- dev-psalm-fix
- dev-raml-api-common-types
- dev-variant_accessor
- dev-test-product-projection-variantsku
- dev-camelCase-restructure-processing-states
- dev-user-agent
- dev-exception_details
- dev-renovate/configure
- dev-address_draft_support
This package is auto-updated.
Last update: 2024-07-26 07:49:38 UTC
README
Client and Request Builder for making API requests against Commercetools.
Installation
composer require commercetools/commercetools-sdk
Usage
namespace Commercetools; use Commercetools\Api\Client\ClientCredentialsConfig; use Commercetools\Api\Client\Config; use Commercetools\Client\ClientCredentials; use Commercetools\Client\ClientFactory; require_once __DIR__ . '/vendor/autoload.php'; /** @var string $clientId */ /** @var string $clientSecret */ $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. Information for the Import API can be found here.
Examples to retrieve project information
use Commercetools\Api\Client\ApiRequestBuilder; use GuzzleHttp\ClientInterface; /** @var ClientInterface $client */ $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 Commercetools\Client\ApiRequestBuilder; use Commercetools\Client\ImportRequestBuilder; use GuzzleHttp\ClientInterface; /** @var ClientInterface $client */ $builder = new ApiRequestBuilder('your-project-key', $client); $request = $builder->categories()->get(); $importBuilder = new ImportRequestBuilder('your-project-key', $client); $request = $importBuilder->importSinks()->get();
Executing requests
use Commercetools\Client\ApiRequestBuilder; use GuzzleHttp\ClientInterface; /** @var ClientInterface $client */ $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);
Custom endpoint for different regions
By default, the library uses api.europe-west1.gcp.commercetools.com
endpoint. If you use a different region, you can configure the client to use a custom endpoint. Here is an example for the us-central1
region:
$authConfig = new ClientCredentialsConfig( new ClientCredentials(CLIENT_ID, CLIENT_SECRET), [], 'https://auth.us-central1.gcp.commercetools.com/oauth/token' ); $config = new Config([], 'https://api.us-central1.gcp.commercetools.com'); $client = ClientFactory::of()->createGuzzleClient( $config, $authConfig, );
Note that the auth endpoint should contain the /oauth/token
suffix, but the API endpoint - don't.
Migration Guidelines
To migrate from the 1.x to the 2.x, there is a guideline below:
Observability
To monitor and observe the SDK, see the official documentation Observability, there is a Demo application which shows how to monitor the PHP SDK with New Relic.
Documentation
License
MIT