PHP SDK
Official PHP SDK for SnapSharp. Capture screenshots, OG images, and site audits with Composer.
Installation
Install the PHP SDK using your package manager:
composer require snapsharp/sdkThen import and initialise the client:
use SnapSharp\SnapSharp;
$snap = new SnapSharp('sk_live_...');Replace sk_live_... with your API key from the dashboard.
Quick Start
Three common use cases to get you up and running in minutes.
1. Take a Screenshot
Capture a screenshot and save the result to a file.
<?php
require_once 'vendor/autoload.php';
use SnapSharp\SnapSharp;
$snap = new SnapSharp('sk_live_...');
$image = $snap->screenshot('https://example.com', [
'width' => 1280,
'height' => 720,
'format' => 'png',
'fullPage' => false,
]);
file_put_contents('screenshot.png', $image);
echo "Screenshot saved!\n";2. Generate an OG Image
Create Open Graph images for social media sharing.
<?php
use SnapSharp\SnapSharp;
$snap = new SnapSharp('sk_live_...');
$og = $snap->ogImage('https://example.com', [
'template' => 'default',
'width' => 1200,
'height' => 630,
]);
file_put_contents('og-image.png', $og);3. Render HTML to Image
Convert an HTML string to a rendered image.
<?php
use SnapSharp\SnapSharp;
$snap = new SnapSharp('sk_live_...');
$image = $snap->htmlToImage('<h1>Hello World</h1><p>Rendered with SnapSharp</p>', [
'width' => 800,
'height' => 400,
'format' => 'png',
'css' => 'body { font-family: sans-serif; background: #1a1a2e; color: #fff; display: grid; place-items: center; }',
]);
file_put_contents('html-render.png', $image);API Reference
Complete list of methods available on the PHP client.
screenshot()
screenshot(string $url, array $options = []): string
Captures a screenshot of the given URL. Options include width, height, format, fullPage, device, darkMode, delay, and headers.
ogImage()
ogImage(string $url, array $options = []): string
Generates an Open Graph image for the URL using built-in or custom templates.
htmlToImage()
htmlToImage(string $html, array $options = []): string
Renders raw HTML/CSS markup into an image binary string. Supports inline styles and custom viewport.
siteAudit()
siteAudit(string $url, array $options = []): array
Runs a performance and SEO audit. Returns an associative array with scores and recommendations.
All methods that return images accept an optional format parameter ("png", "jpeg", "webp"). Default is "png". See the full API reference for every available option.
Error Handling
The SDK throws typed errors with HTTP status, message, and a unique request ID for debugging. Always wrap calls in a try/catch to handle rate limits and authentication errors gracefully.
<?php
use SnapSharp\SnapSharp;
use SnapSharp\Exceptions\SnapSharpException;
$snap = new SnapSharp('sk_live_...');
try {
$image = $snap->screenshot('https://example.com');
} catch (SnapSharpException $e) {
echo "Error {$e->getStatus()}: {$e->getMessage()}\n";
echo "Request ID: {$e->getRequestId()}\n";
if ($e->getStatus() === 401) {
echo "Invalid API key. Check your credentials.\n";
}
if ($e->getStatus() === 429) {
echo "Rate limited. Retry after: {$e->getRetryAfter()}s\n";
}
}| Status | Meaning | Action |
|---|---|---|
| 400 | Bad Request — invalid parameters | Check the request body and query params |
| 401 | Unauthorized — invalid or missing API key | Verify your API key in the dashboard |
| 429 | Too Many Requests — rate limit exceeded | Wait for retryAfter seconds |
| 500 | Internal Server Error | Retry with exponential back-off |
Configuration
Customise the client with optional settings passed at initialisation.
$snap = new SnapSharp('sk_live_...', [
'baseUrl' => 'https://api.snapsharp.dev', // Custom API base URL
'timeout' => 30, // Request timeout in seconds
'retries' => 2, // Auto-retry on 5xx errors
]);| Option | Default | Description |
|---|---|---|
| baseUrl | api.snapsharp.dev | Override the API base URL (e.g. for on-premise) |
| timeout | 30s | Maximum time to wait for a response |
| retries | 0 | Number of automatic retries on 5xx errors |
Ready to start building?
Get your free API key and start capturing screenshots in under a minute. 100 free screenshots every month — no credit card required.