Older PHP apps

Integrate Bugsnag with other PHP apps, running older versions of PHP (5.2+).

This integration supports older versions of PHP (5.2+). For newer applications, see the updated PHP integration guide.

Installation

Using Composer

If you are using PHP 5.3+, then add bugsnag/bugsnagto your composer.json as follows:

$ composer require "bugsnag/bugsnag:^2.9"

Using a phar package

If you are using PHP 5.2+, then instead of using composer, you can download bugsnag.phar from the latest release of the 2.9 series to your PHP project, and include it in your application:

require_once "/path/to/bugsnag.phar";

Manual installation

Download and extract the latest Bugsnag source code to your PHP project, and incude it in your application using our provided autoloader:

require_once "/path/to/Bugsnag/Autoload.php";

Basic configuration

Create a Bugsnag client object with your Integration API Key:

$bugsnag = new Bugsnag_Client('YOUR-API-KEY-HERE');

You can find this key immediately after creating a new project from your Bugsnag dashboard, or later on your project’s settings page.

If you’d like to configure Bugsnag further, check out the configuration options reference.

Reporting unhandled exceptions

You can ensure that all unhandled errors and exceptions are reported to Bugsnag by attaching our error_handler and exception_handler methods as follows:

set_error_handler(array($bugsnag, 'errorHandler'));
set_exception_handler(array($bugsnag, 'exceptionHandler'));

At this point, Bugsnag should be installed and configured, and any unhandled exceptions will be automatically detected and should appear in your Bugsnag dashboard.

Reporting handled exceptions

Reporting handled exceptions can be accomplished as follows:

try {
    // Some potentially crashy code
} catch (Exception $e) {
    $bugsnag->notifyException($e);
}

When reporting handled exceptions, it’s often helpful to send us custom diagnostic data or to adjust the severity of particular errors. For more information, see reporting handled errors.

Sending diagnostic data

Automatically captured diagnostics

Bugsnag will automatically capture the following data for every exception:

  • A full stacktrace
  • Request information, including ip, headers, URL, HTTP method, and HTTP params
  • Session and cookie data
  • Release stage (production, beta, staging, etc)
  • Hostname

Custom diagnostics

It is often helpful to send us additional application-specific diagnostic data. This can be accomplished as follows:

$bugsnag->setBeforeNotifyFunction(function ($error) {
    $error->setMetaData([
      'account' => [
          'name' => 'Acme Co.',
          'paying_customer' => true,
      ]
    ]);
});

For more information, see the customizing error reports reference.

Identifying users

In order to correlate errors with customer reports, or to see a list of users who experienced each error, it is helpful to capture and display user information on your Bugsnag dashboard.

You can set this information as follows:

$bugsnag->setUser([
    'id' => '123456',
    'name' => 'Leeroy Jenkins',
    'email' => 'leeeeroy@jenkins.com',
]);

You can also set this on the error object from the callback function from v2.9 and upwards:

$bugsnag->setBeforeNotifyFunction(function ($error) {
    $error->setUser([
        'id' => '123456',
        'name' => 'Leeroy Jenkins',
        'email' => 'leeeeroy@jenkins.com',
    ]);
});

For more information, see configuration options.

Tracking deploys

By sending your source revision or application version to us when you deploy a new version of your app, you’ll be able to see which deploy each error was introduced in, or seen in.

See our deploy tracking guide for more details.

Next steps