commercetools PHP SDK
A great share of the ecommerce world is written in PHP. So it was a natural decision to provide a PHP SDK for the commercetools platform.
Starting in January 2015 we had a rough idea what we want to achieve:
- The SDK should be written in plain PHP to be independent from frameworks and support a wide range of applications.
- We needed a reusable client.
- The request and update actions should be abstracted to ease the communication with the API
- It should be possible to map the API result objects.
One of the first decisions made, was to use the Guzzle HTTP client to talk to the API. This client library is written in vanilla PHP, it supports futures and batch requests for concurrent request handling and it can use different HTTP connection methods like streams and curl. So the client supports a lot of scenarios out in the wild.
The second decision was to encapsulate the HTTP client and OAuth management and build our own request models. So the end user don’t has to hassle with http requests, reauthentication issues etc.
At third we started to build a document mapper for the response data. As mapping the response json can be very expensive, we decided to go for a lazy loading approach, which only maps result data when first used. The mapping also has the advantage to build convenience methods or automatically build indices on arrays e.g. access attributes by name.
A goal not yet mentioned which had been achieved is the use of some of the PSR standards. We are supporting following standards: We are using PSR-2 as coding style guideline, which is also enforced in our CI environment. PSR-4 is used as class autoloading standard. For logging purposes we are supporting all PSR-3 loggers (e.g. Monolog) and internally we are using PSR-7 for the HTTP requests and responses, so any HTTP client supporting PSR-7 can be used in the API client.
So let’s take a look in to the future of the SDK and some projects around them.
- As we try to use standardized interfaces, we also would like to add support for PSR-6 compatible caches.
- We are working on a Symfony 3.0 bundle to speed up integration of CTP in our customers projects.
- Our quickstart application Sunrise implementing some of the common tasks for an ecommerce shop is already using our Symfony Bundle.
Get started with our PHP SDK:
You can find the complete source of the commercetools PHP SDK on Github. It is using the MIT license and comes with a detailled installation description.
(Image source: https://www.flickr.com/photos/kea42/, CC BY-SA 2.0)