Skip to content
shotsmith
Features Docs Guides Pricing Changelog Sign inStart free

How to take website screenshots in PHP and Laravel

Capture website screenshots and PDFs from PHP and Laravel: plain cURL, the HTTP client, queued jobs and storing files.

3 min read

You can screenshot a web page from PHP without installing Chrome, Browsershot or a Node toolchain on your server. shotsmith renders the page in real Chrome and returns the image or PDF in the response, so all your code does is make an HTTP request. This guide covers plain PHP first, then Laravel.

1. Get an API key

Create a free account (100 renders a month, no card) and copy your access key from the API keys page. Store it in your environment, not in code.

2. Plain PHP with cURL

The curl extension ships with almost every PHP install. This saves a full-page PNG:

<?php

$query = http_build_query([
    'access_key' => 'YOUR_ACCESS_KEY',
    'url' => 'https://shotsmith.dev/sample',
    'full_page' => 'true',
    'block_cookie_banners' => 'true',
    'format' => 'png',
], '', '&', PHP_QUERY_RFC3986);

$ch = curl_init('https://shotsmith.dev/v1/take?' . $query);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 120,
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);

if ($status !== 200) {
    throw new RuntimeException(json_decode($body, true)['error_message'] ?? "HTTP {$status}");
}

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

3. In Laravel, with the HTTP client

Laravel’s HTTP client makes this a few lines. Query parameters are passed as an array:

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

$response = Http::timeout(30)->get('https://shotsmith.dev/v1/take', [
    'access_key' => config('services.shotsmith.key'),
    'url' => 'https://shotsmith.dev/sample',
    'format' => 'webp',
    'block_cookie_banners' => 'true',
]);

$response->throw();

Storage::put('screenshots/example.webp', $response->body());

Add the key to config/services.php as 'shotsmith' => ['key' => env('SHOTSMITH_ACCESS_KEY')].

4. Capture in a queued job

Screenshots take a second or two, so don’t make your users wait for them. Capture them in a job instead. This one stores a thumbnail for a Site model and records when it was taken:

<?php

namespace App\Jobs;

use App\Models\Site;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

class CaptureSiteThumbnail implements ShouldQueue
{
    use Queueable;

    public int $tries = 3;

    public function __construct(public Site $site) {}

    public function handle(): void
    {
        $response = Http::timeout(30)
            ->retry(2, 2000, throw: false)
            ->get('https://shotsmith.dev/v1/take', [
                'access_key' => config('services.shotsmith.key'),
                'url' => $this->site->url,
                'format' => 'webp',
                'viewport_width' => 1280,
                'image_width' => 640,
                'block_cookie_banners' => 'true',
                'block_ads' => 'true',
            ]);

        if ($response->failed()) {
            // error_code is stable, so you can branch on it.
            $this->fail(new \RuntimeException($response->json('error_code').': '.$response->json('error_message')));

            return;
        }

        $path = "thumbnails/{$this->site->id}.webp";
        Storage::put($path, $response->body());
        $this->site->update(['thumbnail_path' => $path, 'thumbnail_taken_at' => now()]);
    }
}

Dispatch it whenever a site is added or changes: CaptureSiteThumbnail::dispatch($site).

5. Turn your own pages into PDFs

For invoices and reports, send the HTML you already render with Blade as html and ask for a PDF. Use a POST request, because HTML is too long for a URL:

$pdf = Http::timeout(30)
    ->withHeaders(['X-Access-Key' => config('services.shotsmith.key')])
    ->post('https://shotsmith.dev/v1/take', [
        'html' => view('invoices.show', ['invoice' => $invoice])->render(),
        'format' => 'pdf',
        'pdf_paper_format' => 'a4',
        'pdf_print_background' => true,
    ])
    ->throw()
    ->body();

Storage::put("invoices/{$invoice->number}.pdf", $pdf);

The invoice PDF guide covers page breaks, margins and print styles.

6. Errors

Failed requests return JSON with a stable error_code and a message saying what to change, so $response->json('error_code') is safe to branch on. Failed renders are never billed. The error reference lists every code.

Prefer a typed client?

The official PHP and Laravel SDK gives you a typed option builder (TakeOptions::url($url)->fullPage()->format('pdf')), a Shotsmith facade, signed URLs for Blade and webhook verification.