PHP SDK
Learn how the FastPix PHP SDK enables video uploads, live streaming, and playback management in PHP applications.
Add secure, scalable video to your PHP project
The FastPix PHP SDK gives you everything you need to manage video from your backend. Upload files from a public URL, attach metadata, and retrieve playback links, all without building encoding pipelines or managing storage. With this SDK, you can:
- Upload and organize video assets
- Define access control and metadata
- Generate HLS playback URLs
- Streamline video delivery from your PHP server
Prerequisites
Before you start using the SDK, make sure you have the following:
- PHP 8.2 or later: This SDK is compatible with PHP 8.2 or higher
- Composer: Required for dependency management
- cURL extension: Required for HTTP requests
- JSON extension: Required for JSON processing
- FastPix API credentials: You’ll need an Access Token and a Secret Key. You can generate these credentials by following the steps in the authentication guide
- Basic understanding of PHP and REST APIs: Familiarity with PHP development and API integration concepts
Installation
Install the SDK using Composer:
Add the SDK to your project:
{
"require": {
"fastpix/sdk": "*"
}
}Then run:
composer updateIf you host the package in a private repository, add the repository to your composer.json:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/your-org/fastpix-sdk-php.git"
}
],
"require": {
"fastpix/sdk": "*"
}
}Import the SDK
<?php
require 'vendor/autoload.php';
use FastPix\Sdk;
use FastPix\Sdk\Models\Components;
use FastPix\Sdk\Models\Operations;Example
Let's create a file named index.php
This example shows how to upload a video from a public URL with metadata and public access:
NOTE:
In the following example, replaceyour-access-tokenandyour-secret-keywith the Access Token ID and Secret Key values from the.envfile you downloaded.
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use FastPix\Sdk;
use FastPix\Sdk\Models\Components;
try {
$sdk = Sdk\Fastpixsdk::builder()
->setSecurity(
new Components\Security(
username: 'your-access-token',
password: 'your-secret-key',
)
)
->build();
$request = new Components\CreateMediaRequest(
inputs: [
new Components\PullVideoInput(),
],
metadata: [
'key1' => 'value1',
],
);
$response = $sdk->inputVideo->createMedia(
request: $request
);
if ($response->statusCode >= 200 && $response->statusCode < 300) {
$rawBody = (string) $response->rawResponse->getBody();
$decoded = json_decode($rawBody, true);
echo ($decoded !== null ? json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) : $rawBody) . "\n";
} else {
$errorPayload = $response->defaultError ?? $response->error ?? null;
if ($errorPayload !== null) {
$errorResponse = json_decode(json_encode($errorPayload), true);
echo json_encode($errorResponse, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n";
} else {
echo json_encode(['message' => 'No response data'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n";
}
}
} catch (\Exception $e) {
// Extract API error response
$errorBody = null;
if (property_exists($e, 'body') && property_exists($e, 'statusCode')) {
$body = $e->body;
$errorBody = json_decode($body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$errorBody = $body;
}
} elseif (method_exists($e, 'getResponse')) {
$response = $e->getResponse();
if ($response !== null) {
$body = (string)$response->getBody();
$errorBody = json_decode($body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$errorBody = $body;
}
}
}
// Output API error response
if ($errorBody !== null) {
echo json_encode($errorBody, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n";
} else {
echo json_encode(['error' => $e->getMessage()], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n";
}
exit(1);
}After the video is processed, you can use the media ID to fetch playback info, monitor status, or transform content through the API.
Run the example
To execute the PHP script, use the following command. Make sure to replace index.php with your actual file name if it's different:
php index.phpNOTE
Some methods might throw errors when you are on a trial plan.
Full documentation
See the FastPix PHP SDK for additional examples, API methods, and advanced usage.
Updated 6 days ago