Sirv REST API – PHP examples

On this page

Use the Sirv REST API to perform over 40 common tasks on your Sirv account. Use the example PHP scripts below for a quick start.

Either write your own PHP scripts or use the Sirv PHP API class below, to connect, get a token, then perform API tasks.

Connect to Sirv with PHP

  1. Create an API client, if you've not created one already. You'll be given a client ID and client Secret.

  2. Download the Sirv PHP API class (zipped PHP file).

  3. Require the Sirv API class:

    require_once('sirv.api.class.php');
  4. Initialize the class to get a token (CLIENT_ID):

    $sirv = new SirvAPIClient(
    'CLIENT_ID',
    'SECRET_ID',
    '',
    '',
    'Sirv PHP client'
    );
    
    // Check if client has successfully connected:
    echo 'Is connected: '.$sirv->isConnected();
    echo '<br/>';
    
    // Get token and its expiry time
    $token = $sirv->getToken();
    $tokenExpireTime = $sirv->getTokenExpireTime();
    
    echo 'Token: '.$token;
    echo '<br/>';
    echo 'Token expire time: '.$tokenExpireTime;
  5. If you already have a valid token from a previous connection, skip step 4 and use this instead:

    $token = 'ENTER_YOUR_TOKEN_HERE';
    $tokenExpireTime = 'ENTER_EXPIRY_TIME_HERE';
    
    $sirv = new SirvAPIClient(
      'CLIENT_ID',
      'SECRET_ID',
      $token,
      $tokenExpireTime,
      'Sirv PHP client'
    );

Now you can perform over 40 Sirv REST API methods.

Upload file

Upload a local file to Sirv your Sirv account:

$sirv->uploadFile('example/local-file.jpg', 'example-folder/file-on-sirv.jpg');

A successful upload will return the following response headers in JSON format. There is no body. A response code of 200 means success:

HTTP/2 200
date: Sat, 02 May 2020 16:57:19 GMT
content-length: 0
x-ratelimit-limit: 2000
x-ratelimit-remaining: 1992
x-ratelimit-reset: 1588441712
x-ratelimit-type: rest:post:files:upload
access-control-allow-origin: *
access-control-expose-headers: *
cache-control: no-cache
server: Sirv.API
strict-transport-security: max-age=31536000

Convert video to spin

$client = new httpClient;
$request = new httpClientRequest;

$body = new httpMessageBody;
$body->append('{
 "filename": "/path/to/video/Example.mp4"
}');

$request->setRequestUrl('https://api.sirv.com/v2/files/video2spin');
$request->setRequestMethod('POST');
$request->setBody($body);

$request->setHeaders(array(
 'authorization' => 'Bearer RECEIVED_TOKEN_HERE',
 'content-type' => 'application/json'
));

$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();

Fetch image from URL

Fetch remote images via URL and save them to your Sirv account:

$images = array(
  array(
    'url' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c0/Castle_Mountain.jpg/800px-Castle_Mountain.jpg',
    'filename' => '/Folder-on-Sirv/file-1.jpg',
    'wait' => true
  ),
  array(
    'url' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c0/Castle_Mountain.jpg/800px-Castle_Mountain.jpg',
    'filename' => '/Folder-on-Sirv/file-2.jpg',
    'wait' => true
  ),
);
$sirv->fetchImage($images);

Get folder contents

Fetch a list of folder contents:

$contents = $sirv->getFolderContents('ExampleFolder');
print_r($contents);

Delete file / folder

To delete a folder, it must be empty.

$sirv->deleteFile('ExampleFolder/file.jpg');

Get file info

$info = $sirv->getFileStat('ExampleFolder/file-on-sirv.jpg');

Folder options

Get folder options:

$f_options = $sirv->getFolderOptions('ExampleFolder');

Set folder options:

$f_options = array(
  'scanSpins' => false,
  'nameSpinsAfterFolder' => true,
  'allowListing' => false
);
$sirv->setFolderOptions('folderName', $f_options);

Get account info

$info = $sirv->getAccountInfo();

Get usage info

$info = $sirv->getStorageInfo();

Configure CDN

$sirv->configCDN(
  true, // true - activate CDN, false - disable
  'account-name-here'
);

Was this article helpful?

Get help from a Sirv expert