Guides · PHP · cURL extension

Take a screenshot of a URL in PHP with cURL extension

The simplest request the API takes: a URL in, PNG bytes out. No full_page, no extra options — you get exactly what a visitor with a 1280×800 browser window would see above the fold. That is the right default more often than people expect: link previews, monitoring thumbnails and visual smoke tests all want the fold, not a 40,000-pixel scroll of the footer.

Below is a complete, runnable PHP program using cURL extension, no third-party dependency needed. It reads your API key from the SNAPDOK_KEY environment variable — free keys take about thirty seconds and need no card.

<?php
$body = json_encode([
    'url' => "https://your-app.com/report/42",
    'format' => "png",
    'width' => 1280,
    'height' => 800,
]);

$ch = curl_init("https://snapdok.io/v1/render");
$headers = [];
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 90,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('SNAPDOK_KEY'),
        'Content-Type: application/json',
    ],
    CURLOPT_HEADERFUNCTION => function ($ch, $line) use (&$headers) {
        $parts = explode(':', $line, 2);
        if (count($parts) === 2) $headers[strtolower(trim($parts[0]))] = trim($parts[1]);
        return strlen($line);
    },
]);

$bytes = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);

if ($status !== 200) {
    throw new RuntimeException("snapdok error $status: $bytes");
}
file_put_contents('screenshot.png', $bytes);

The two dimensions are the viewport, not the output size: the page lays itself out as if the browser window were 1280×800, and the image comes back at exactly those pixels (multiply by device_scale if you set it). Responsive sites react to the width — ask for 390 and you get the mobile layout, no user-agent spoofing involved.

Two habits worth starting on day one. Check the HTTP status before writing bytes to disk — errors arrive as JSON, and saving {"error":"UNAUTHORIZED"} as screenshot.png is the classic first-week bug. And log the X-Cache header: identical requests within 24 hours return HIT, cost nothing, and come back in milliseconds — failed renders are never metered either way.

Same endpoint, one more trick: if the page you are rendering has a form on it, adding "pdf_forms": true to a PDF render brings it back with real, fillable AcroForm fields — a PDF people can type into, not a picture of one. How that works.

Notes for cURL extension

Plain ext-curl, no Composer needed. The details that bite: CURLOPT_RETURNTRANSFER must be set or the bytes are echoed to stdout instead of returned; the HTTP status has to be read explicitly with curl_getinfo() because cURL happily "succeeds" on a 401; and response headers need a CURLOPT_HEADERFUNCTION callback if you want to check things like X-Form-Fields. file_put_contents is binary-safe in PHP — strings are byte arrays — so no "b" flag dance is needed.

Response headers worth reading

HeaderMeaning
X-CacheHIT = served from the 24h cache, free.
X-Quota-RemainingMetered renders left this month.
X-RateLimit-RemainingRequests left in the current second.

Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.

Related guides

Same task, other stacks

More with PHP + cURL extension