How to screenshot a website with PHP
Capture web pages as images or PDFs from PHP with file_get_contents or cURL. No wkhtmltoimage, no headless Chrome on your server. Laravel-ready.
On shared hosting you usually cannot install Chrome, and old tools like wkhtmltoimage render modern pages badly. An API call works on any PHP host.
The shortest version
<?php
$png = file_get_contents("https://getscreenapi.com/api/v1/screenshot?" . http_build_query([
"url" => "https://example.com",
"full_page" => "true",
"access_key" => getenv("SCREENAPI_KEY"),
]));
file_put_contents("example.png", $png);Keep this on the server: anyone who sees a URL containing access_key can use your quota. To show a capture in a page, use a signed URL instead.
With cURL and error handling
<?php
$ch = curl_init("https://getscreenapi.com/api/v1/screenshot");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ["x-api-key: " . getenv("SCREENAPI_KEY"), "Content-Type: application/json"],
CURLOPT_POSTFIELDS => json_encode(["url" => "https://example.com", "format" => "pdf"]),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 90,
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status !== 200) {
$err = json_decode($body, true)["error"] ?? [];
throw new RuntimeException(($err["code"] ?? "ERROR") . ": " . ($err["message"] ?? ""));
}
file_put_contents("page.pdf", $body);In Laravel
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
$response = Http::withHeaders(["x-api-key" => config("services.screenapi.key")])
->timeout(90)
->post("https://getscreenapi.com/api/v1/screenshot", [
"url" => "https://example.com",
"device" => "iphone-15-pro",
"format" => "webp",
])
->throw();
Storage::put("shots/example.webp", $response->body());100 free captures every month, no credit card.
Get a free key