Ready-to-copy PHP code for capturing website screenshots, generating PDFs, and processing pages at scale. Uses cURL (built-in) or Guzzle.
The simplest way to capture a screenshot. Just a URL and your API key.
$response = file_get_contents(
"https://api.screenshotdev.com/v1/screenshot?" . http_build_query([
"url" => "https://example.com",
"access_key" => "YOUR_ACCESS_KEY",
])
);
file_put_contents("screenshot.png", $response);Complete working script with options for viewport, format, full page capture, and error handling.
<?php
$apiKey = "YOUR_ACCESS_KEY";
$baseUrl = "https://api.screenshotdev.com/v1/screenshot";
function takeScreenshot(
string $url,
string $format = "webp",
int $width = 1280,
bool $fullPage = false,
bool $darkMode = false
): string {
global $apiKey, $baseUrl;
$params = http_build_query([
"url" => $url,
"access_key" => $apiKey,
"format" => $format,
"width" => $width,
"full_page" => $fullPage ? "true" : "false",
"dark_mode" => $darkMode ? "true" : "false",
]);
$ch = curl_init("$baseUrl?$params");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status !== 200) {
throw new RuntimeException("Screenshot failed ($status): $body");
}
return $body;
}
$img = takeScreenshot("https://example.com", format: "webp", width: 1440, fullPage: true);
file_put_contents("screenshot.webp", $img);
echo "Saved screenshot.webp (" . strlen($img) . " bytes)\n";PDF generation, batch processing, webhooks, CSS/JS injection, and S3 upload.
$params = http_build_query([
"url" => "https://example.com",
"access_key" => "YOUR_ACCESS_KEY",
"format" => "a4",
"landscape" => "false",
"margin" => "20px",
]);
$pdf = file_get_contents("https://api.screenshotdev.com/v1/pdf?$params");
file_put_contents("page.pdf", $pdf);$apiKey = "YOUR_ACCESS_KEY";
$urls = [
"https://example.com",
"https://github.com",
"https://news.ycombinator.com",
];
foreach ($urls as $url) {
$params = http_build_query([
"url" => $url,
"access_key" => $apiKey,
"format" => "webp",
]);
$img = file_get_contents(
"https://api.screenshotdev.com/v1/screenshot?$params"
);
$host = parse_url($url, PHP_URL_HOST);
file_put_contents("$host.webp", $img);
echo " $host.webp (" . strlen($img) . " bytes)\n";
}$ch = curl_init("https://api.screenshotdev.com/v1/screenshot");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_POSTFIELDS => json_encode([
"url" => "https://example.com",
"access_key" => "YOUR_ACCESS_KEY",
"webhook_url" => "https://your-app.com/api/webhooks/screenshot",
"format" => "webp",
"full_page" => true,
]),
]);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
echo "Job queued: {$result['id']}\n";$params = http_build_query([
"url" => "https://example.com",
"access_key" => "YOUR_ACCESS_KEY",
"inject_css" => "body { font-family: 'Inter', sans-serif; } .ad { display: none; }",
"inject_js" => "document.querySelectorAll('[data-cookie-banner]').forEach(el => el.remove())",
"block_ads" => "true",
]);
$img = file_get_contents("https://api.screenshotdev.com/v1/screenshot?$params");
file_put_contents("clean.webp", $img);use Aws\S3\S3Client;
// 1. Capture the screenshot
$params = http_build_query([
"url" => "https://example.com",
"access_key" => "YOUR_ACCESS_KEY",
"format" => "webp",
]);
$img = file_get_contents("https://api.screenshotdev.com/v1/screenshot?$params");
// 2. Upload to S3
$s3 = new S3Client(["region" => "us-east-1"]);
$s3->putObject([
"Bucket" => "my-screenshots",
"Key" => "captures/example-com.webp",
"Body" => $img,
"ContentType" => "image/webp",
]);Test the API in the interactive playground — pre-filled with PHP snippets.
Install the HTTP library used in the examples above.
composer require guzzlehttp/guzzleUses cURL (built-in) or Guzzle. Any HTTP client that supports GET requests will work.
100 free API calls per month. No credit card. First screenshot in under 30 seconds.