Install the SnapAPI PHP SDK via Composer. Compatible with Laravel, Symfony, and vanilla PHP. Requires PHP 7.4+.
Install via Composer:
composer require snapapi/snapapi-php
Requirements: PHP 7.4+, ext-curl, ext-json.
<?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'));<?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";$screenshot = $client->screenshot([
'url' => 'https://example.com',
'format' => 'png',
'fullPage' => true,
'width' => 1280
]);$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
]);$result = $client->screenshot([
'url' => 'https://example.com',
'format' => 'png',
'responseType' => 'url'
]);
echo $result['url'];
// → https://cdn.snapapi.pics/screenshots/abc123.png$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 = $client->pdf([
'html' => '<h1>Invoice #1234</h1><p>Total: $99.00</p>',
'format' => 'A4',
'printBackground' => true
]);$content = $client->extract([
'url' => 'https://example.com/blog-post',
'format' => 'markdown'
]);
echo $content['markdown'];
// → # Blog Post Title\n\nArticle content here...$data = $client->extract([
'url' => 'https://example.com/product',
'format' => 'structured',
'schema' => [
'title' => 'string',
'price' => 'number',
'description' => 'string'
]
]);
echo "Product: {$data['title']} - \${$data['price']}";<?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";
}// 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'));
});
}<?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"');
}
}// routes/web.php
Route::get('/screenshot', [ScreenshotController::class, 'capture']);SNAPAPI_KEY=your_key_here to your Laravel .env file.