PHP
Integrate the Flimpa API into your PHP applications.
Installation
This example uses native PHP cURL to interact with the API. No external dependencies are required.
Initialization
Set up your API key and base URL.
php
$api_key = 'your_api_key';
$base_url = 'https://api.flimpa.com/api/v1';Authentication
Authenticate your requests using the X-API-Key header for task creation, and the X-Task-Access-Token header for status and download requests.
Usage Example (cURL)
This example demonstrates how to create a task, poll for its status, and download the result. Operations supported are compress-pdf and image-to-webp.
php
<?php
$api_key = 'your_api_key';
$base_url = 'https://api.flimpa.com/api/v1';
$file_path = '/path/to/input.pdf';
$operation = 'compress-pdf';
// 1. Create Task (Upload)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$base_url/tasks");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-API-Key: $api_key"]);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'operation' => $operation,
'file' => new CURLFile($file_path)
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code >= 400) {
$error_data = json_decode($response, true);
die("Failed to create task: " . $error_data['error'] . " (" . $error_data['code'] . ")");
}
$task_data = json_decode($response, true);
$task_id = $task_data['taskId'];
$access_token = $task_data['accessToken'];
// 2. Poll Status
$delay = 1;
while (true) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$base_url/tasks/$task_id");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Task-Access-Token: $access_token"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$status_data = json_decode($response, true);
if ($status_data['status'] === 'COMPLETED') {
break;
}
if ($status_data['status'] === 'FAILED') {
die("Task failed: " . $status_data['error'] . " (" . $status_data['code'] . ")");
}
sleep($delay);
$delay = min($delay * 2, 10);
}
// 3. Download Result
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$base_url/tasks/$task_id/download");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Task-Access-Token: $access_token"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code >= 400) {
die("Failed to download result");
}
file_put_contents('output.pdf', $result);
echo "Success!";
?>Error Handling
Always check the response status and handle potential errors. The API returns JSON with error and code fields when a request fails.
Best Practices
- Implement exponential backoff for polling (as shown in the example).
- Files are limited to 20MB.
- Tasks have a TTL of 1 hour. Download your results promptly.
- Do not expose your API key in client-side code.
Webhooks
Webhooks for task completion are Coming Soon. For now, please use polling.