# PHP SDK

[transloadit/php-sdk](https://github.com/transloadit/php-sdk)

Our PHP SDK allows you to automate the uploading of files through the TransloaditREST API using PHP.

It can be installed via Composer.

## Install

![](/_next/static/media/copy.04p1cju9qekk_.svg?dpl=dpl_CtwzFbHWtqiCy9uvWb9fE7WvfP9N)

```
composer require transloadit/php-sdk

```

Keep your Transloadit account's Auth Key & Secret nearby. You can check the[API credentials](/workspaces/credentials/) page for these values.

## Usage

### 1. Upload and resize an image from your server

This example demonstrates how you can use the SDK to create an Assembly on your server.

It takes a sample image file, uploads it to Transloadit, and starts a resizing job on it.

![](/_next/static/media/copy.04p1cju9qekk_.svg?dpl=dpl_CtwzFbHWtqiCy9uvWb9fE7WvfP9N)

```php
<?php
require 'vendor/autoload.php';

use transloadit\Transloadit;

$transloadit = new Transloadit([
  'key' => 'YOUR_TRANSLOADIT_KEY',
  'secret' => 'YOUR_TRANSLOADIT_SECRET',
]);

$response = $transloadit->createAssembly([
  'files' => ['/PATH/TO/FILE.jpg'],
  'params' => [
    'steps' => [
      'resize' => [
        'robot' => '/image/resize',
        'width' => 200,
        'height' => 100,
      ],
    ],
  ],
]);

// Show the results of the assembly we spawned
echo '<pre>';
print_r($response);
echo '</pre>';

```

### 2. Create a simple end-user upload form

This example shows you how to create a simple Transloadit upload form that redirects back to your site after the upload is done.

Once the script receives the redirect request, the current status for this Assembly is shown using `Transloadit::response()`.

###### Note

There is no guarantee that the Assembly has already finished executing by the time the `$response` is fetched. You should use the `notify_url` parameter for this.

![](/_next/static/media/copy.04p1cju9qekk_.svg?dpl=dpl_CtwzFbHWtqiCy9uvWb9fE7WvfP9N)

```php
<?php
require 'vendor/autoload.php';

use transloadit\Transloadit;

$transloadit = new Transloadit([
  'key' => 'YOUR_TRANSLOADIT_KEY',
  'secret' => 'YOUR_TRANSLOADIT_SECRET',
]);

// Check if this request is a Transloadit redirect_url notification.
// If so fetch the response and output the current assembly status:
$response = Transloadit::response();
if ($response) {
  echo '<h1>Assembly Status:</h1>';
  echo '<pre>';
  print_r($response);
  echo '</pre>';
  exit();
}

// This should work on most environments, but you might have to modify
// this for your particular setup.
$redirectUrl = sprintf('http://%s%s', $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI']);

// Setup a simple file upload form that resizes an image to 200x100px
echo $transloadit->createAssemblyForm([
  'params' => [
    'steps' => [
      'resize' => [
        'robot' => '/image/resize',
        'width' => 200,
        'height' => 100,
      ],
    ],
    'redirect_url' => $redirectUrl,
  ],
]);
?>
<h1>Pick an image to resize</h1>
<input name="example_upload" type="file">
<input type="submit" value="Upload">
</form>

```

### 3. Use Uppy for file uploads

We recommend using [Uppy](/docs/sdks/uppy.md), our next-gen file uploader for the web.

1. Include Uppy in your page:

![](/_next/static/media/copy.04p1cju9qekk_.svg?dpl=dpl_CtwzFbHWtqiCy9uvWb9fE7WvfP9N)

```html
<link href="https://releases.transloadit.com/uppy/v3.3.1/uppy.min.css" rel="stylesheet" />
<script src="https://releases.transloadit.com/uppy/v3.3.1/uppy.min.js"></script>

```

1. Initialize Uppy with the Transloadit plugin:

![](/_next/static/media/copy.04p1cju9qekk_.svg?dpl=dpl_CtwzFbHWtqiCy9uvWb9fE7WvfP9N)

```html
<div id="uppy"></div>

<script>
  const uppy = new Uppy.Core()
    .use(Uppy.Dashboard, {
      inline: true,
      target: '#uppy',
    })
    .use(Uppy.Transloadit, {
      params: {
        auth: { key: 'YOUR_TRANSLOADIT_KEY' },
        template_id: 'YOUR_TEMPLATE_ID',
        notify_url: 'https://your-site.com/transloadit_notify.php',
      },
    })
</script>

```

1. Handle notifications on your PHP backend:

![](/_next/static/media/copy.04p1cju9qekk_.svg?dpl=dpl_CtwzFbHWtqiCy9uvWb9fE7WvfP9N)

```php
<?php
require 'vendor/autoload.php';

use transloadit\Transloadit;

$transloadit = new Transloadit([
  'key' => 'YOUR_TRANSLOADIT_KEY',
  'secret' => 'YOUR_TRANSLOADIT_SECRET',
]);

$response = Transloadit::response();
if ($response) {
  http_response_code(200);
  echo 'OK';
  return;
}

http_response_code(400);
echo 'Bad Request';

```

### 4. Fetch the Assembly Status JSON

You can use the `getAssembly` method to get the Assembly Status.

![](/_next/static/media/copy.04p1cju9qekk_.svg?dpl=dpl_CtwzFbHWtqiCy9uvWb9fE7WvfP9N)

```php
<?php
require 'vendor/autoload.php';
$assemblyId = 'YOUR_ASSEMBLY_ID';

$transloadit = new Transloadit([
  'key' => 'YOUR_TRANSLOADIT_KEY',
  'secret' => 'YOUR_TRANSLOADIT_SECRET',
]);

$response = $transloadit->getAssembly($assemblyId);

echo '<pre>';
print_r($response);
echo '</pre>';

```

### 5. Create an Assembly with a Template.

This example demonstrates how you can use the SDK to create an Assembly withTemplates.

You are expected to create a Template on your Transloadit account dashboard and add theTemplate ID here.

![](/_next/static/media/copy.04p1cju9qekk_.svg?dpl=dpl_CtwzFbHWtqiCy9uvWb9fE7WvfP9N)

```php
<?php
require 'vendor/autoload.php';

use transloadit\Transloadit;

$transloadit = new Transloadit([
  'key' => 'YOUR_TRANSLOADIT_KEY',
  'secret' => 'YOUR_TRANSLOADIT_SECRET',
]);

$response = $transloadit->createAssembly([
  'files' => ['/PATH/TO/FILE.jpg'],
  'params' => [
    'template_id' => 'YOUR_TEMPLATE_ID',
  ],
]);

// Show the results of the assembly we spawned
echo '<pre>';
print_r($response);
echo '</pre>';

```

### Signature Auth

Signature Authentication is done by the PHP SDK by default internally so you do not need to worry about this :)

## Example

For fully working examples take a look at[examples/](https://github.com/transloadit/php-sdk/tree/HEAD/examples).

## Documentation

See [GitHub](https://github.com/transloadit/php-sdk) for the full documentation.
