SDKs
PHP SDK
PSR-compatible PHP SDK
PHP SDK
The official PHP SDK for the AllScreenshots API with PSR-compatible HTTP client support.
Source code: GitHub | Package: allscreenshots/sdk
Installation
composer require allscreenshots/sdkQuick start
<?php
use Allscreenshots\Sdk\Client\AllscreenshotsClient;
use Allscreenshots\Sdk\Models\ScreenshotRequest;
$client = AllscreenshotsClient::builder()->build();
$request = new ScreenshotRequest('https://github.com');
$imageData = $client->screenshot($request);
file_put_contents('screenshot.png', $imageData);Configuration
Builder pattern
$client = AllscreenshotsClient::builder()
->apiKey('your-api-key')
->baseUrl('https://api.allscreenshots.com')
->timeout(60)
->maxRetries(3)
->build();Environment variables
Set ALLSCREENSHOTS_API_KEY to automatically configure authentication:
// Client will read API key from environment
$client = AllscreenshotsClient::builder()->build();Capture screenshots
Basic capture
$request = new ScreenshotRequest('https://example.com');
$imageData = $client->screenshot($request);
file_put_contents('screenshot.png', $imageData);With options
$request = new ScreenshotRequest('https://example.com');
$request->setDevice('Desktop HD');
$request->setFormat('png');
$request->setFullPage(true);
$request->setDarkMode(true);
$request->setBlockAds(true);
$request->setBlockCookieBanners(true);
$request->setDelay(1000);
$imageData = $client->screenshot($request);Device presets
// Desktop
$request = new ScreenshotRequest('https://example.com');
$request->setDevice('Desktop HD');
$desktop = $client->screenshot($request);
// Mobile
$request = new ScreenshotRequest('https://example.com');
$request->setDevice('iPhone 15');
$mobile = $client->screenshot($request);
// Tablet
$request = new ScreenshotRequest('https://example.com');
$request->setDevice('iPad Pro 11');
$tablet = $client->screenshot($request);Async jobs
For long-running captures:
// Create async job
$request = new ScreenshotRequest('https://example.com');
$request->setFullPage(true);
$job = $client->screenshotAsync($request);
echo "Job ID: " . $job->jobId . "\n";
// Poll for completion
$status = $client->jobs()->get($job->jobId);
if ($status->status === 'completed') {
$imageData = $client->jobs()->getResult($job->jobId);
file_put_contents('screenshot.png', $imageData);
}Bulk capture
Process multiple URLs efficiently:
$bulkJob = $client->bulk()->create([
'urls' => [
['url' => 'https://example1.com'],
['url' => 'https://example2.com'],
['url' => 'https://example3.com'],
],
'defaults' => [
'device' => 'Desktop HD',
'format' => 'png',
],
]);
// Check status
$status = $client->bulk()->get($bulkJob->bulkJobId);Composition
Combine multiple screenshots into a single image:
$composed = $client->compose([
'url' => 'https://example.com',
'variants' => [
['device' => 'Desktop HD', 'label' => 'Desktop'],
['device' => 'iPhone 15', 'label' => 'Mobile'],
],
'output' => [
'layout' => 'grid',
'spacing' => 20,
],
]);Schedules
Create recurring screenshot captures:
// Create a schedule
$schedule = $client->schedules()->create([
'name' => 'Daily Homepage',
'url' => 'https://example.com',
'schedule' => '0 9 * * *',
'timezone' => 'America/New_York',
]);
// List schedules
$schedules = $client->schedules()->list();
// Get captures
$captures = $client->schedules()->getCaptures($schedule->scheduleId);Usage tracking
Monitor your API usage:
$usage = $client->usage()->get();
echo "Screenshots: {$usage->screenshotCount}/{$usage->quota}\n";Laravel integration
// config/services.php
'allscreenshots' => [
'api_key' => env('ALLSCREENSHOTS_API_KEY'),
],
// app/Providers/AppServiceProvider.php
use Allscreenshots\Sdk\Client\AllscreenshotsClient;
public function register()
{
$this->app->singleton(AllscreenshotsClient::class, function () {
return AllscreenshotsClient::builder()
->apiKey(config('services.allscreenshots.api_key'))
->build();
});
}
// In your controller
public function capture(Request $request, AllscreenshotsClient $client)
{
$screenshotRequest = new ScreenshotRequest($request->url);
$imageData = $client->screenshot($screenshotRequest);
return response($imageData)->header('Content-Type', 'image/png');
}Error handling
use Allscreenshots\Sdk\Exceptions\AuthenticationException;
use Allscreenshots\Sdk\Exceptions\ValidationException;
use Allscreenshots\Sdk\Exceptions\RateLimitException;
use Allscreenshots\Sdk\Exceptions\NotFoundException;
use Allscreenshots\Sdk\Exceptions\ServerException;
use Allscreenshots\Sdk\Exceptions\NetworkException;
try {
$imageData = $client->screenshot($request);
} catch (RateLimitException $e) {
echo "Rate limited. Retry after {$e->getRetryAfter()}s\n";
} catch (AuthenticationException $e) {
echo "Invalid API key\n";
} catch (ValidationException $e) {
echo "Validation error: {$e->getMessage()}\n";
} catch (ServerException $e) {
echo "Server error: {$e->getMessage()}\n";
}License
Apache License 2.0