Blog
ArchiveTwitterFeed

Introducing: Sentry's Unified PHP SDK

If you’ve done web development in the past decade, you’ve probably heard about the Personal Home Page (PHP) programming language. Some love it — some don’t. The fact is that PHP is one of the most used programming languages for web development.

While you probably don’t script together your Personal Home Page these days, many PHP frameworks, like Laravel and Symfony, are still gaining popularity and others are well established. WordPress, for instance, started as a little blogging CMS and now powers a third of the whole internet (or so they claim).

This growth solidifies our need to provide the best PHP support we can. After all, everyone knows that more programmers writing code means more errors and bugs to find and fix.

Let’s dive deeper into how to use Sentry with PHP.

Sentry’s PHP SDK

Installing the PHP SDK is as simple as:

$ composer require sentry/sdk

This line adds the SDK and all required dependencies to your project. There is a lot flexibility when it comes to customizing which HTTP client to use, and more details can be found in our PHP documentation.

Include the following to set up and use the SDK in your code:

Sentry\init(['dsn' => 'YOUR DSN' ]);

// Setting some context into into the event
Sentry\configureScope(function (Sentry\State\Scope $scope): void {
    $scope->setUser(['email' => 'hello@sentry.io']);
});

// Adding a Breadcrumb
Sentry\addBreadcrumb(new Sentry\Breadcrumb(
    Sentry\Breadcrumb::LEVEL_INFO,
    Sentry\Breadcrumb::TYPE_USER,
    'Title',
    'Message'
));

// throw new \Exception('bar foo');
// Sentry\captureException(\Exception('bar foo'));

Now all unhandled errors and exceptions in your application will show up in Sentry.

In general, writing PHP scripts using the SDK directly is rare. It’s much more likely that you are using one of our framework-specific SDKs, like Laravel.

Sentry’s Laravel SDK

Installing our Laravel SDK also requires only one line of code.

$ composer require sentry/sentry-laravel

You’ll need to add the following to App/Exceptions/Handler.php so that your Laravel application sends all exceptions to Sentry:

public function report(Exception $exception)
{
    if (app()->bound('sentry') && $this->shouldReport($exception)){
        app('sentry')->captureException($exception);
    }

    parent::report($exception);
}

We also have some Laravel-specific options you’ll need to publish our default config, like:

$ php artisan vendor:publish --provider="Sentry\Laravel\ServiceProvider"

Now paste your DSN into your .env file (without this, the SDK won’t send anything).

SENTRY_LARAVEL_DSN=YOUR DSN

Want to make sure it’s working? Call:

$ php artisan sentry:test

The output should look something like this:

[sentry] Client DSN discovered!
[sentry] Generating test event
[sentry] Sending test event
[sentry] Event sent: e6442bd7806444fc8b2710abce3599ac

Now that the SDK is installed and working, errors will appear in Sentry with the full stack trace, tags, and Breadcrumbs.

Sentry Issue Details


If you’re also using one of our other unified SDKs like JavaScript, Rust, .Net, or Python, the API usage should feel very familiar. So, use it. Break things. Repair them. Break them some more. Repair them again. Break them one more time. Repair — you get the idea.

We’d like to give a shout out to our PHP community for helping us get this new major release out the door, especially Alessandro, Alex, and Stefano.

Any feedback you have about the PHP SDK is welcome. If you’re still using the old version of the PHP SDK, make sure to upgrade now.

Your code is broken. Let's Fix it.
Start using Sentry