# PHP

Source: https://developers.swell.is/backend-api/backend-libraries/php

Swell for 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 methods, and this library’s caching behavior.

> **Note:** See our [backend API docs](https://developers.swell.is/backend-api/products/create-a-product) for code examples using PHP.

> **Tip:** Find the `swell-php` library on Github.

### Installation

Add the library to your project using composer:

**Install using Composer**

**PHP**

```php
composer require swellstores/swell-php
```

### Initialization

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

**Initialization**

**PHP**

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

To connect multiple store instances within the same request:

**Connect multiple instances**

**PHP**

```php
$store1 = new Swell\Client('my-store-1', 'secret-key-1');
$store2 = new Swell\Client('my-store-2', 'secret-key-2');
```

### Using different frameworks

Swell PHP can be used with a standalone server-side PHP application, or with any PHP framework such as:

- Laravel
- Symfony
- CakePHP
- CodeIgniter
- Phalcon

### Methods

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`

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.

#### get/put/post/delete

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**

**PHP** `GET`

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

**PHP** `POST`

```php
// Create a new product.
$swell->post('/products', [
  'name' => 'Longsword',
  'price' => 50.00,
  'active' => true,
  'options' => [
    [
      'name' => 'Material',
      'values' => [
        [
          'name' => 'Iron',
        ],
        [
          'name' => 'Steel',
        ],
				[
          'name' => 'Silver',
        ],
      ],
    ],
  ],
]);
```

**PHP** `PUT`

```php
// Update an existing product.
$swell->put('/products/{id}', [
  'id' => '5c8fb5e1ed2faf8c79da492a',
  'price' => 19.99,
  // use $set to override values
  '$set' => [
    'options' => [
      [
        'name' => 'Material',
        'values' => [
          [
            'name' => 'Ebony',
          ],
          [
            'name' => 'Glass',
          ],
        ],
      ],
    ],
  ],
]);
```

**PHP** `DELETE`

```php
// Delete an existing product.
$swell->delete('/products/{id}', [
  'id' => '5c8fb5e1ed2faf8c79da492a',
]);
```

### Caching

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`.

**PHP**

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