PHP SDK for Sirv REST API
composer require sirv/sirv-rest-api-php2. Copy your Client ID and Client secret from Account › Settings › API.3. Connect$client = new SirvClient('your-client-id', 'your-client-secret'); No account yet? Create one freeShort answer
Sirv's official PHP SDK wraps the Sirv REST API, so you can upload, download, search and manage files, metadata, users and account statistics from your code. Install it with composer require sirv/sirv-rest-api-php, then connect with the client ID and secret from your Sirv account's API settings. The SDK gets and refreshes the access token for you.
After reading this you will know
- How to install the SDK and connect to your Sirv account
- Which file, account, statistics and spin methods it offers
- How to handle errors and limits
The official PHP SDK provides a simple, elegant interface for interacting with the Sirv REST API. It handles authentication automatically and provides typed methods for all API operations.
Resources:
Installation
Section titled “Installation”Install the SDK via Composer:
composer require sirv/sirv-rest-api-phpRequirements
Section titled “Requirements”- PHP 7.4 or higher
- ext-json
- ext-curl
- Guzzle HTTP client 7.0+
Quick Start
Section titled “Quick Start”<?php
require_once 'vendor/autoload.php';
use Sirv\SirvClient;
// Initialize the client$client = new SirvClient( 'your-client-id', 'your-client-secret');
// Get account information$account = $client->account()->get();echo "Account: " . $account['alias'] . "\n";
// List files in a directory$files = $client->files()->list('/');foreach ($files['contents'] as $file) { echo $file['filename'] . "\n";}Authentication
Section titled “Authentication”The SDK handles authentication automatically. When you make your first API call, it obtains an access token using your client credentials.
The token is automatically refreshed when it expires.
$client = new SirvClient( 'your-client-id', 'your-client-secret');
// Optional: Manually authenticate to verify credentialstry { $token = $client->authenticate(); echo "Authenticated successfully\n";} catch (\Sirv\Exception\AuthenticationException $e) { echo "Authentication failed: " . $e->getMessage() . "\n";}
// Optional: Request a token with custom expiry time (5-604800 seconds)$token = $client->authenticate(3600); // Token valid for 1 hourFiles API
Section titled “Files API”Complete file management including upload, download, copy, rename, delete, and metadata operations.
Available Methods
Section titled “Available Methods”| Category | Methods |
|---|---|
| File Operations | list(), getInfo(), upload(), download(), copy(), rename(), delete(), mkdir(), fetch() |
| Metadata | getMeta(), setMeta(), getApproval(), setApproval(), getTitle(), setTitle(), getDescription(), setDescription(), getProductMeta(), setProductMeta(), getTags(), setTags(), deleteTags() |
| Batch Operations | zip(), getZipResult(), deleteBatch(), getDeleteBatchResult() |
| Media & Export | spinToVideo(), videoToSpin(), exportToAmazon(), exportToWalmart(), exportToHomeDepot(), exportToLowes(), exportToGrainger() |
Basic Operations
Section titled “Basic Operations”// List folder contents$files = $client->files()->list('/images');
// Upload a file$result = $client->files()->upload('/local/path/photo.jpg', '/images/photo.jpg');
// Download a file$content = $client->files()->download('/images/photo.jpg');file_put_contents('downloaded.jpg', $content);
// Copy a file$client->files()->copy('/images/photo.jpg', '/backup/photo.jpg');
// Delete a file$client->files()->delete('/images/photo.jpg');Search Files
Section titled “Search Files”// Search files$results = $client->files()->search([ 'query' => 'filename:*.jpg', 'from' => '/images', 'size' => 100]);
// Paginate through resultsif (isset($results['_scroll_id'])) { $nextPage = $client->files()->searchScroll($results['_scroll_id']);}Metadata Operations
Section titled “Metadata Operations”// Set file metadata$client->files()->setMeta('/images/photo.jpg', [ 'title' => 'My Photo', 'description' => 'A beautiful sunset']);
// Set tags$client->files()->setTags('/images/photo.jpg', ['nature', 'sunset', 'beach']);
// Set product metadata$client->files()->setProductMeta('/images/product.jpg', [ 'id' => 'SKU123', 'name' => 'Product Name', 'brand' => 'Brand Name']);Batch Operations
Section titled “Batch Operations”// Create ZIP file$job = $client->files()->zip([ '/images/photo1.jpg', '/images/photo2.jpg'], 'my-archive.zip');
// Check ZIP status$result = $client->files()->getZipResult($job['token']);if ($result['status'] === 'completed') { echo "ZIP URL: " . $result['url'] . "\n";}Account API
Section titled “Account API”Manage your Sirv account settings and retrieve account information.
Available Methods
Section titled “Available Methods”| Method | Description |
|---|---|
get() | Get account information including CDN URL and account name |
update(array $settings) | Update account settings (minify, fetching) |
getLimits() | Get API request limits and usage |
getStorage() | Get storage usage information |
getUsers() | List all account users |
getBillingPlan() | Get billing plan details |
searchEvents(array $params) | Search account events |
markEventsSeen(array $eventIds) | Mark events as seen |
Examples
Section titled “Examples”// Get account information$account = $client->account()->get();
// Get API limits$limits = $client->account()->getLimits();echo "API calls used: " . $limits['used'] . "/" . $limits['limit'] . "\n";
// Get storage information$storage = $client->account()->getStorage();echo "Storage used: " . $storage['used'] . " bytes\n";
// Search account events$events = $client->account()->searchEvents([ 'module' => 'files', 'level' => 'error']);Statistics API
Section titled “Statistics API”Retrieve usage statistics for your account.
| Method | Description |
|---|---|
getHttp($from, $to) | Get HTTP transfer statistics |
getSpinViews($from, $to, $alias) | Get spin viewer statistics (max 5 days) |
getStorage($from, $to) | Get storage statistics |
// HTTP transfer statistics$httpStats = $client->stats()->getHttp('2024-01-01', '2024-01-31');
// Using DateTime objects$from = new DateTime('2024-01-01');$to = new DateTime('2024-01-31');$httpStats = $client->stats()->getHttp($from, $to);
// Storage statistics$storageStats = $client->stats()->getStorage('2024-01-01', '2024-01-31');User API
Section titled “User API”Retrieve user information.
// Get current user info$user = $client->user()->get();echo "Email: " . $user['email'] . "\n";
// Get specific user info$user = $client->user()->get('user-id-here');Error Handling
Section titled “Error Handling”The SDK provides specific exception types for different error scenarios.
| Exception | Description |
|---|---|
AuthenticationException | Invalid credentials or authentication failed |
ApiException | General API errors |
RateLimitException | Rate limit exceeded |
ValidationException | Invalid request parameters |
use Sirv\SirvClient;use Sirv\Exception\AuthenticationException;use Sirv\Exception\ApiException;use Sirv\Exception\RateLimitException;
try { $client = new SirvClient('client-id', 'client-secret'); $files = $client->files()->list('/');} catch (AuthenticationException $e) { echo "Auth error: " . $e->getMessage() . "\n";} catch (RateLimitException $e) { echo "Rate limited. Retry after: " . $e->getRetryAfter() . " seconds\n";} catch (ApiException $e) { echo "API error: " . $e->getMessage() . "\n"; echo "HTTP Status: " . $e->getHttpStatusCode() . "\n";}RateLimitException Methods
Section titled “RateLimitException Methods”getRetryAfter()- Seconds to wait before retryinggetRateLimit()- Rate limit for the endpointgetRateLimitRemaining()- Remaining requests in window
ApiException Methods
Section titled “ApiException Methods”getHttpStatusCode()- HTTP status codegetRequestId()- Request ID for debugginggetErrorDetails()- Additional error details
Configuration
Section titled “Configuration”Timeout
Section titled “Timeout”// Set custom timeout (in seconds)$client = new SirvClient('client-id', 'client-secret', 60);Custom Guzzle Options
Section titled “Custom Guzzle Options”// Pass additional Guzzle HTTP client options$client = new SirvClient('client-id', 'client-secret', 30, [ 'proxy' => 'http://proxy.example.com:8080', 'verify' => false]);Only turn off SSL verification for testing, and keep it on in production.
