Hey there, fellow developer! Ready to dive into the world of Frame.io API integration? You're in for a treat. Frame.io's API is a powerhouse for video collaboration, and we're about to harness that power in PHP. Buckle up!
Before we jump in, make sure you've got:
Got those? Great! Let's roll.
First things first, let's get our project structure in order:
composer require guzzlehttp/guzzle
This'll bring in Guzzle for our HTTP requests. Trust me, it'll make our lives easier.
Create a basic structure like this:
frameio-integration/
├── src/
│ └── FrameioClient.php
├── tests/
└── composer.json
Alright, let's tackle authentication. Frame.io uses OAuth 2.0, so we'll need to get an access token:
<?php use GuzzleHttp\Client; class FrameioClient { private $client; private $accessToken; public function __construct($clientId, $clientSecret) { $this->client = new Client(['base_uri' => 'https://api.frame.io/v2/']); $this->authenticate($clientId, $clientSecret); } private function authenticate($clientId, $clientSecret) { $response = $this->client->post('oauth/token', [ 'json' => [ 'client_id' => $clientId, 'client_secret' => $clientSecret, 'grant_type' => 'client_credentials' ] ]); $data = json_decode($response->getBody(), true); $this->accessToken = $data['access_token']; } }
Now that we're authenticated, let's set up a method for making API requests:
private function request($method, $endpoint, $data = []) { $response = $this->client->request($method, $endpoint, [ 'headers' => [ 'Authorization' => 'Bearer ' . $this->accessToken, 'Content-Type' => 'application/json' ], 'json' => $data ]); return json_decode($response->getBody(), true); }
Let's add methods for uploading assets and retrieving asset info:
public function uploadAsset($projectId, $filePath) { // First, initiate the upload $initUpload = $this->request('POST', "projects/$projectId/uploads", [ 'name' => basename($filePath) ]); // Now, upload the file $this->client->put($initUpload['upload_url'], [ 'body' => fopen($filePath, 'r') ]); // Finally, create the asset return $this->request('POST', "assets", [ 'name' => basename($filePath), 'type' => 'file', 'parent_id' => $projectId, 'upload_id' => $initUpload['id'] ]); } public function getAsset($assetId) { return $this->request('GET', "assets/$assetId"); }
Adding and retrieving comments is a breeze:
public function addComment($assetId, $text) { return $this->request('POST', "assets/$assetId/comments", [ 'text' => $text ]); } public function getComments($assetId) { return $this->request('GET', "assets/$assetId/comments"); }
Creating review links is crucial for collaboration:
public function createReviewLink($assetId) { return $this->request('POST', "assets/$assetId/review_links"); }
Setting up webhooks is key for real-time updates:
public function createWebhook($url, $events) { return $this->request('POST', "webhooks", [ 'url' => $url, 'events' => $events ]); }
Don't forget to implement robust error handling:
private function request($method, $endpoint, $data = []) { try { $response = $this->client->request($method, $endpoint, [ 'headers' => [ 'Authorization' => 'Bearer ' . $this->accessToken, 'Content-Type' => 'application/json' ], 'json' => $data ]); return json_decode($response->getBody(), true); } catch (\Exception $e) { // Log the error error_log("Frame.io API Error: " . $e->getMessage()); throw $e; } }
Don't skimp on testing! Here's a quick example using PHPUnit:
use PHPUnit\Framework\TestCase; class FrameioClientTest extends TestCase { private $client; protected function setUp(): void { $this->client = new FrameioClient('your_client_id', 'your_client_secret'); } public function testGetAsset() { $asset = $this->client->getAsset('some_asset_id'); $this->assertArrayHasKey('id', $asset); } }
Remember to respect rate limits and implement caching where appropriate. Your future self will thank you!
And there you have it! You've just built a solid Frame.io API integration in PHP. You've got the tools to manage assets, handle comments, create review links, and even set up webhooks. The video collaboration world is your oyster now!
Remember, this is just the beginning. There's so much more you can do with the Frame.io API. Keep exploring, keep building, and most importantly, keep having fun with it!
Happy coding, and may your integrations always be smooth and your videos always be in sync!