PHP

Learn how to set up Sentry in your PHP application.

Image for: Learn how to set up Sentry in your PHP application.

Image for: Prerequisites
  • You need a Sentry account and project
  • Your application needs to run on PHP 7.2 or later
  • Read one of these other guides if you use Laravel or Symfony

Image for: Features

Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.

Image for: Install

Install the Sentry SDK using Composer:

Copied
composer require sentry/sentry

To use Profiling, you'll also need to install the Excimer extension via PECL:

Copied
pecl install excimer

The Excimer PHP extension supports PHP 7.2 and up. Excimer requires Linux or macOS and doesn't support Windows. For additional ways to install Excimer, see docs.

Image for: Configure

To capture all errors, even the one during the startup of your application, you should initialize the Sentry PHP SDK as soon as possible.

Copied
\Sentry\init([
  'dsn' => 'https://examplePublicKey@o0.ingest.sentry.io/0',
  // Add request headers, cookies and IP address,
  // see https://docs.sentry.io/platforms/php/data-management/data-collected/ for more info
  'send_default_pii' => true,
  //  performance
  // Specify a fixed sample rate
  'traces_sample_rate' => 1.0,
  //  performance
  //  profiling
  // Set a sampling rate for profiling - this is relative to traces_sample_rate
  'profiles_sample_rate' => 1.0,
  //  profiling
]);

Image for: Verify

In PHP you can either capture a caught exception or capture the last error with captureLastError.

Copied
try {
  $this->functionFailsForSure();
} catch (\Throwable $exception) {
  \Sentry\captureException($exception);
}

Image for: Set up Sentry through Forge

If you're using Laravel's Forge platform to provision and deploy your PHP application, you can create a Sentry organization through Forge.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").