Skip to content

Quickstart

Website screenshots in PHP

Use cURL to create a screenshot with one HTTPS request and no browser infrastructure to manage.

Ironfang Render uses a standard HTTPS API, so PHP can call it with cURL and does not require an SDK. A successful request returns the image bytes directly, ready to write to a file.

Get a key

Create an API key in the portal. The secret is displayed once and stored as a hash. Keep it in an environment variable named IRONFANG_API_KEY rather than in the source file; the example below reads it from there.

export IRONFANG_API_KEY="if_live_..."

Take a screenshot

<?php
$key = getenv('IRONFANG_API_KEY');
if ($key === false || $key === '') {
    fwrite(STDERR, "Set IRONFANG_API_KEY to your Ironfang API key.\n");
    exit(1);
}

$ch = curl_init('https://api.ironfang.com/render/v1/screenshot');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 60,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $key,
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'url' => 'https://example.com',
        'width' => 1280,
    ]),
]);

$body = curl_exec($ch);
if ($body === false) {
    throw new RuntimeException('Request failed: ' . curl_error($ch));
}
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($status !== 200) {
    $err = json_decode($body, true)['error'] ?? ['code' => 'unknown', 'message' => $body];
    throw new RuntimeException("{$status} {$err['code']}: {$err['message']}");
}

file_put_contents('shot.png', $body);

Run it, and the screenshot is saved as shot.png in the current directory:

php shot.php

Handling errors

Successful render responses are binary. Errors use JSON in the form {"error": {"code", "message"}}. A missing or unrecognised key returns 401 invalid_api_key. A key without the render:render scope returns 403 insufficient_scope.

// CURLOPT_RETURNTRANSFER keeps the binary response in $body
// instead of writing it directly to stdout. curl_exec returns
// false only when the request never completed.

PDFs and templates

The same call shape works for PDFs at /render/v1/pdf, and for templated images at /render/v1/image/{id}. Only the path and the body change.

Next