Docs · PHP

SDKs

PHP

Bugzor works with the open-source sentry/sentry PHP SDK (and Laravel and Symfony packages built on it). Tested with sentry/sentry 4.37 on PHP 8.5 (plain PHP; the Laravel and Symfony packages use the same client).

Plain PHP#

Terminal
composer require sentry/sentry

Initialize as early as possible, for example at the top of your front controller:

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

\Sentry\init([
    'dsn' => 'https://<public-key>@www.bugzor.com/<project-id>',
    'environment' => 'production',
    'release' => 'my-app@1.0.0',
    // Include the user's IP (needed for "users affected").
    'send_default_pii' => true,
]);

The SDK registers error, exception and fatal-error handlers, so uncaught errors are reported automatically.

Laravel#

Terminal
composer require sentry/sentry-laravel
php artisan sentry:publish --dsn=https://<public-key>@www.bugzor.com/<project-id>

sentry:publish writes config/sentry.php and adds SENTRY_LARAVEL_DSN to your .env. On Laravel 11 and later, hook it into exception handling in bootstrap/app.php:

bootstrap/app.php
use Sentry\Laravel\Integration;

->withExceptions(function (Exceptions $exceptions) {
    Integration::handles($exceptions);
})

Symfony#

Terminal
composer require sentry/sentry-symfony

Set SENTRY_DSN in .env to your Bugzor DSN. The bundle reports exceptions from controllers, the console and Messenger.

Capture errors and messages#

PHP
try {
    importOrders();
} catch (\Throwable $e) {
    \Sentry\captureException($e);
}

\Sentry\captureMessage('Payment provider responded slowly', \Sentry\Severity::warning());

Add context#

PHP
\Sentry\configureScope(function (\Sentry\State\Scope $scope): void {
    $scope->setUser(['id' => $user->id, 'email' => $user->email]);
    $scope->setTag('component', 'checkout');
    $scope->setContext('cart', ['items' => 3, 'total_cents' => 4599]);
});

Long-running workers#

PHP-FPM sends events when each request ends. In queue workers or CLI scripts, flush before exiting:

PHP
\Sentry\SentrySdk::getCurrentHub()->getClient()?->flush();

Test it#

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

\Sentry\init(['dsn' => getenv('SENTRY_DSN')]);
\Sentry\captureMessage('Hello from PHP');
Terminal
SENTRY_DSN="https://<public-key>@www.bugzor.com/<project-id>" php test.php

In Laravel, php artisan sentry:test sends a test event.

Something unclear or out of date? Tell us.