🐘 PHP SDK — Quick Start

Install the SnapAPI PHP SDK via Composer. Compatible with Laravel, Symfony, and vanilla PHP. Requires PHP 7.4+.

On This Page

Installation

Install via Composer:

composer require snapapi/snapapi-php

Requirements: PHP 7.4+, ext-curl, ext-json.

Authentication

<?php
require 'vendor/autoload.php';

use SnapAPI\Client;

// Initialize with your API key
$client = new Client('YOUR_API_KEY');

// Or use environment variable (recommended)
$client = new Client(getenv('SNAPAPI_KEY'));

Take Screenshots

Basic Screenshot

<?php
require 'vendor/autoload.php';

use SnapAPI\Client;

$client = new Client(getenv('SNAPAPI_KEY'));

$screenshot = $client->screenshot([
    'url' => 'https://example.com',
    'format' => 'png',
    'width' => 1280,
    'height' => 800
]);

file_put_contents('screenshot.png', $screenshot);
echo "Screenshot saved!\n";

Full Page Screenshot

$screenshot = $client->screenshot([
    'url' => 'https://example.com',
    'format' => 'png',
    'fullPage' => true,
    'width' => 1280
]);

Screenshot with All Options

$screenshot = $client->screenshot([
    'url' => 'https://example.com',
    'format' => 'avif',          // png, jpeg, webp, avif
    'width' => 1440,
    'height' => 900,
    'fullPage' => false,
    'blockAds' => true,           // Block advertisements
    'blockCookies' => true,       // Block cookie banners
    'delay' => 2000,              // Wait 2s before capture
    'devicePreset' => 'iPhone15', // Mobile emulation
    'css' => 'body { background: white; }',
    'responseType' => 'url'       // Get hosted URL
]);

Get Hosted URL

$result = $client->screenshot([
    'url' => 'https://example.com',
    'format' => 'png',
    'responseType' => 'url'
]);

echo $result['url'];
// → https://cdn.snapapi.pics/screenshots/abc123.png

Generate PDFs

$pdf = $client->pdf([
    'url' => 'https://example.com',
    'format' => 'A4',
    'printBackground' => true,
    'margin' => [
        'top' => '20mm',
        'bottom' => '20mm',
        'left' => '15mm',
        'right' => '15mm'
    ]
]);

file_put_contents('page.pdf', $pdf);

PDF from HTML String

$pdf = $client->pdf([
    'html' => '<h1>Invoice #1234</h1><p>Total: $99.00</p>',
    'format' => 'A4',
    'printBackground' => true
]);

Extract Content

$content = $client->extract([
    'url' => 'https://example.com/blog-post',
    'format' => 'markdown'
]);

echo $content['markdown'];
// → # Blog Post Title\n\nArticle content here...

Extract Structured Data

$data = $client->extract([
    'url' => 'https://example.com/product',
    'format' => 'structured',
    'schema' => [
        'title' => 'string',
        'price' => 'number',
        'description' => 'string'
    ]
]);

echo "Product: {$data['title']} - \${$data['price']}";

Error Handling

<?php
use SnapAPI\Client;
use SnapAPI\Exceptions\AuthenticationException;
use SnapAPI\Exceptions\RateLimitException;
use SnapAPI\Exceptions\BadRequestException;
use SnapAPI\Exceptions\SnapAPIException;

$client = new Client(getenv('SNAPAPI_KEY'));

try {
    $screenshot = $client->screenshot([
        'url' => 'https://example.com'
    ]);
    file_put_contents('screenshot.png', $screenshot);

} catch (AuthenticationException $e) {
    echo "Invalid API key\n";

} catch (RateLimitException $e) {
    echo "Rate limit exceeded. Retry after: {$e->getRetryAfter()}s\n";

} catch (BadRequestException $e) {
    echo "Bad request: {$e->getMessage()}\n";

} catch (SnapAPIException $e) {
    echo "API error ({$e->getStatusCode()}): {$e->getMessage()}\n";
}

Laravel Integration

Service Provider Setup

// config/services.php
return [
    // ...
    'snapapi' => [
        'key' => env('SNAPAPI_KEY'),
    ],
];

// app/Providers/AppServiceProvider.php
use SnapAPI\Client;

public function register()
{
    $this->app->singleton(Client::class, function () {
        return new Client(config('services.snapapi.key'));
    });
}

Laravel Controller

<?php
namespace App\Http\Controllers;

use SnapAPI\Client;
use Illuminate\Http\Request;

class ScreenshotController extends Controller
{
    public function capture(Request $request, Client $snapapi)
    {
        $request->validate(['url' => 'required|url']);

        $screenshot = $snapapi->screenshot([
            'url' => $request->input('url'),
            'format' => 'png',
            'width' => 1280
        ]);

        return response($screenshot)
            ->header('Content-Type', 'image/png')
            ->header('Content-Disposition', 'attachment; filename="screenshot.png"');
    }
}

Laravel Route

// routes/web.php
Route::get('/screenshot', [ScreenshotController::class, 'capture']);
💡 Tip: Add SNAPAPI_KEY=your_key_here to your Laravel .env file.

Next Steps