<span aria-hidden="true" id="php-sdk-"></span>

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

<span aria-hidden="true" id="user-content-php-sdk-install"></span>

<span aria-hidden="true" id="install"></span>

## Install

```
composer require transloadit/php-sdk

```

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

<span aria-hidden="true" id="user-content-php-sdk-usage"></span>

<span aria-hidden="true" id="usage"></span>

## Usage

<span aria-hidden="true" id="user-content-php-sdk-1-upload-and-resize-an-image-from-your-server"></span>

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

```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>';

```

<span aria-hidden="true" id="user-content-php-sdk-2-create-a-simple-end-user-upload-form"></span>

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

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

```

<span aria-hidden="true" id="user-content-php-sdk-3-integrate-the-jquery-plugin-into-the-previous-example"></span>

### 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:

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

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

```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';

```

<span aria-hidden="true" id="user-content-php-sdk-4-fetch-the-assembly-status-json"></span>

### 4. Fetch the Assembly Status JSON

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

```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>';

```

<span aria-hidden="true" id="user-content-php-sdk-5-create-an-assembly-with-a-template"></span>

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

```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>';

```

<span aria-hidden="true" id="user-content-php-sdk-signature-auth"></span>

<span aria-hidden="true" id="signature-auth"></span>

### Signature Auth

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

<span aria-hidden="true" id="user-content-php-sdk-example"></span>

<span aria-hidden="true" id="example"></span>

## Example

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

<span aria-hidden="true" id="user-content-php-sdk-documentation"></span>

<span aria-hidden="true" id="documentation"></span>

## Documentation

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