Code Examples

Take Screenshots with PHP

Ready-to-copy PHP code for capturing website screenshots, generating PDFs, and processing pages at scale. Uses cURL (built-in) or Guzzle.

Quick Start

Screenshot in PHP — minimal example

The simplest way to capture a screenshot. Just a URL and your API key.

PHPquickstart
$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);
Full Example

Production-ready PHP script

Complete working script with options for viewport, format, full page capture, and error handling.

PHPfull example
<?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";
Advanced

More PHP examples

PDF generation, batch processing, webhooks, CSS/JS injection, and S3 upload.

PDF Generation

PHPpdf
$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);

Batch Screenshots

PHPbatch
$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";
}

Async with Webhooks

PHPwebhooks
$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";

Custom CSS/JS Injection

PHPinject
$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);

S3 Upload

PHPs3
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",
]);

Try it live

Test the API in the interactive playground — pre-filled with PHP snippets.

Setup

Installation

Install the HTTP library used in the examples above.

Terminalinstall
composer require guzzlehttp/guzzle

Uses cURL (built-in) or Guzzle. Any HTTP client that supports GET requests will work.

More Examples

Code examples in other languages

Start capturing screenshots
with PHP today.

100 free API calls per month. No credit card. First screenshot in under 30 seconds.

Take Screenshots with PHP — ScreenshotDEV