Guides · PHP · cURL extension

Capture a JavaScript-heavy page after it settles in PHP with cURL extension

Render a client-side app too early and the capture shows the loading state, not the page: skeleton placeholders where cards should be, a spinner where the chart goes, grey boxes where lazy images land. The page "loaded" — the load event fired — but the app was still fetching its data. "wait_until": "networkidle" moves the goalpost: the render proceeds only after the page stops making network requests for a quiet period.

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",
    'full_page' => true,
    'wait_until' => "networkidle",
    'delay' => 500,
]);

$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('settled.png', $bytes);

The four stages, in order of patience: commit (bytes started arriving), domcontentloaded (HTML parsed), load (the default — static assets done), and networkidle (the network went quiet, which for an SPA usually means data fetched and rendered). delay then adds a fixed pause on top — the 500 ms here covers entrance animations and fade-ins that happen after the last fetch.

The cost of patience is time: networkidle on a page with analytics beacons or long-polling can take a while to go quiet, and a page that never goes quiet will run into timeout (a 504, never metered). If your page exposes a reliable "ready" element, wait_for_selector is the sharper tool — it waits for exactly the thing you care about, no more. There is a guide for it below.

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-Page-HeightMeasured document height for the full-page capture.
X-CacheIdentical requests within 24h replay for free.

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

Related guides

Same task, other stacks

More with PHP + cURL extension