Swell-php is an API-wrapper written in PHP with composer support aimed at backend development. It provides some convenience functions for performing CRUD operations on your Swell store data using a server-side PHP application. It also allows you to connect multiple store instances within the same call.

In this guide, we’ll review the installation and initialization steps, supported frameworks, some commonly used Swell-php methods, and this library’s caching behavior.

See our backend API docs for code examples using PHP.

To install Swell-php, check out this repo.

Add Swell-php to your project using composer:

Install Swell-php
composer require swellstores/swell-php

Initialize the Swell backend client using PHP. The client uses your store ID and secret key for authorization.

Initialization
$swell = new Swell\Client('my-store', 'secret-key');

To connect multiple store instances within the same request:

Connect multiple instances
$store1 = new Swell\Client('my-store-1', 'secret-key-1')
$store1 = new Swell\Client('my-store-2', 'secret-key-2')

Swell-php can be used with a standalone server-side application written in any PHP framework. It can also be used with any PHP framework such as:

  • Laravel
  • Symfony
  • CakePHP
  • CodeIgniter
  • Phalcon

Swell-php is frequently used for querying and retrieving backend data like account, product, and cart details. Some of the most commonly used methods in the library include:

  • get/put/post/delete
  • close

In this section, we’ll go over what each method is used for, the parameters for each one, and see an example of how it’s used.

The get/put/post/delete methods are CRUD operations used to manage and edit your Swell store data and content models. You can also use these CRUD operations to implement custom logic to your store’s data. The parameters for this method are url and data.

All methods
// Retrieve all active products.
$swell->get('/products', [
  'where' => [ 'active' => true ],
  'limit' => 25,
  'page' => 1,
]);

The close method is used to close the server connection manually. Parameters for this method are not necessary.

Example of the close method
swell->close()

Swell-php provides some in-memory caching enabled by default. For records that don't change frequently, such as products, it will always return from cache when possible.

To disable caching behavior, use the option ‘cache' => false.

$swell = new Swell\Client('my-store', 'secret-key', [
  'cache' => false
]);

Now that we've reviewed the Swell-php, we’re ready to explore Swell’s backend API. Continue on to learn about errors and querying.