PHP code example of converter-studio / converter-studio-php

1. Go to this page and download the library: Download converter-studio/converter-studio-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

converter-studio / converter-studio-php example snippets


use ConverterStudio\Client;

$client = new Client(); // the API key comes from CONVERTER_STUDIO_API_KEY

$conversion = $client->convert('IMG_1234.HEIC', 'jpg');
$client->download($conversion, 'photos/'); // photos/IMG_1234.jpg

use ConverterStudio\Client;

$client = new Client(); // or new Client('cvs_live_...')

$conversion = $client->convert('talk.mov', 'mp4', options: ['height' => '720']);
echo $conversion->status, ' ', $conversion->credits, ' ', $conversion->outputName; // done 1 talk.mp4
$path = $client->download($conversion, 'out/'); // a folder (ending in / or existing), or a file path

header('Content-Type: image/jpeg');
$client->downloadTo($conversion, fopen('php://output', 'wb'));

foreach ($client->formats() as $format) { // no key needed
    echo $format->id, ' -> ', implode(', ', $format->targets), "\n";
}

$heic = $client->formats('heic')[0]; // what HEIC converts to
$account = $client->account();
echo $account->plan, ' ', $account->credits, ' ', $account->renewsAt?->format('Y-m-d');

use ConverterStudio\Exception\ConfirmationRequiredException;

try {
    $conversion = $client->convert('lecture.mkv', 'mp4', maxCredits: 5);
} catch (ConfirmationRequiredException $quoted) {
    echo "It costs {$quoted->credits} credits\n";
    $conversion = $client->wait($client->confirm($quoted->conversion));
}

$upload = $client->upload('scan.pdf'); // large files go in parts
$created = $client->createConversion('docx', uploadId: $upload->id, options: ['ocr' => true]);
$created = $client->createConversion('jpg', url: 'https://example.com/photo.heic');
$current = $client->getConversion($created->id); // status now
$current = $client->getConversion($created->id, wait: 30); // the API holds the answer up to 30 s
$done = $client->wait($created, timeout: 900);

$created = $client->createConversion(
    'mp4',
    url: 'https://example.com/raw/clip.mov',
    callbackUrl: 'https://example.com/hooks/converter-studio?token=SECRET',
    idempotencyKey: 'clip-42',
);

use ConverterStudio\Exception\ApiException;
use ConverterStudio\Exception\ConversionFailedException;

try {
    $client->convert('broken.heic', 'jpg');
} catch (ConversionFailedException $failed) {
    echo $failed->errorCode, ': ', $failed->getMessage();
} catch (ApiException $error) {
    echo $error->statusCode, ' ', $error->errorCode, ': ', $error->getMessage();
}

use ConverterStudio\Client;
use ConverterStudio\Http\CurlTransport;

$client = new Client(
    apiKey: 'cvs_live_...', // default: CONVERTER_STUDIO_API_KEY
    baseUrl: 'https://converter.studio/api', // default: CONVERTER_STUDIO_BASE_URL or this
    timeout: 60.0, // seconds to connect, and seconds a request may go without progress
    maxRetries: 2, // network errors, 408, 429, 5xx
    transport: new CurlTransport([CURLOPT_PROXY => 'http://proxy:3128']), // curl options of your own
);