Get started with the PHP SDK
Learn how to set up and use the PHP SDK.
This step-by-step guide leads you through setting up and making API calls using the PHP SDK.
To follow this guide you should have the following:
For more information on setting up a commercetools Composable Commerce Project or API Client, follow our Getting started with commercetools Composable Commerce guides.
Objectives of the get started guide
After following this guide you will have:
Example code in this guide uses placeholders that should be replaced with the following values.
If you do not have an API Client, follow our Get your API Client guide.
|project_key||your API Client|
|client_id||your API Client|
|secret||your API Client|
|scope||your API Client|
Install the PHP SDK
Use the following command to install the PHP SDK:
Create the Client class
Create a file called
Client.php and insert the following code:
Test the Client
In your PHP program, add the following code:
$apiRoot can now be used to build requests to the Composable Commerce API.
This code includes an example API call that gets your Project to
$myProject and outputs the Project's name using
Using the PHP SDK
Without importing resource-specific classes you will be unable to use/access specific objects and methods.
For example, to create a Shopping List you must import:
If not imported, a "Class not found" fatal error is returned with the name of the required class.
The PHP SDK follows a builder pattern when constructing drafts, update actions, and other objects/types that contain multiple fields.
Consult the HTTP API reference to ensure that all required fields are included.
Once the fields/values are added,
build() finishes building the object.
How to structure your API call
Add an endpoint
An endpoint should be added to
$apiRoot. The following targets the Shopping Lists endpoint:
If your IDE supports auto-complete, the full list of endpoints can be viewed.
If no endpoint is specified, the Project is referenced.
Get a single resource
When targeting a specific resource, you should include its ID or key followed by
If you query a resource with an id or key that does not exist, the API returns a 404 Not Found error.
In this example,
$shoppingListInfo now contains the data of the specified Shopping List. Individual information can be accessed from the fields within that object:
Get multiple resources
If no ID or key is included, then the endpoint returns a
PagedQueryResponse, which is identical to the PagedQueryResults in the HTTP API.
These are identical to the parameters you can add to standard HTTP API calls. If your IDE supports autocomplete you can view a full list of methods available:
A list of resources within a
PagedQueryResponse can be accessed using
Writing a resource
Creating a new resource
$newShoppingListDetails should be included within
post() and followed by
Updating an existing resource
Updating an existing resource requires posting an update payload. This payload (in the case of Shopping Lists, a
ShoppingListUpdate) contains a collection of update actions and the last seen version of the resource.
Update actions and payloads are created using builders.
This payload must then be posted to a single resource (using
Deleting a resource
Deleting a resource requires using the
.delete() method with the last seen version of the resource. The resource to delete must be identified using
Retrieving the raw API response
The above examples use
execute() to return the resource as an instance of an object.
To retrieve the raw API response, use
Try our example code
Continue learning about the PHP SDK by checking our SDK code examples. You will find example code for creating, querying, and updating Customers and Products.