PHP Error Tracking installation
Contents
- 1
Install the PHP SDK
RequiredInstall the PostHog PHP SDK via Composer:
Terminal - 2
Initialize the client
RequiredSet your project token and instance address before making any calls:
PHPYou can find your project token and instance address in the project settings page in PostHog.
- 3
Capture exceptions
RequiredUse
captureExceptionto manually capture exceptions and send them to PostHog as$exceptionevents with full stack traces.Basic usage
PHPWith additional properties
You can pass extra properties to include with the exception event:
PHPYou can also pass a plain string if you want to send an error message without a
Throwable. - 4
Enable automatic capture
RecommendedAutomatic capture is opt-in for PHP. When enabled, the SDK installs handlers for uncaught exceptions. With the default
capture_errors: true, it also captures PHP errors and fatal shutdown errors.PHPExisting handlers are preservedThe SDK chains existing exception and error handlers instead of replacing your app's behavior.
- 5
Identify users and attach request context
RecommendedBy default, automatically captured errors are anonymous. Use
context_providerto attach adistinctIdand request metadata to every automatically captured error event.PHPIf
distinctIdis omitted, PostHog sends the event with an auto-generated ID and sets$process_person_profiletofalse. - 6
Configure error tracking options
OptionalPHPOption Type Default Description enabledboolean falseEnables automatic error tracking handlers. Manual captureExceptionworks regardless.capture_errorsboolean trueWhen enabled, also captures PHP errors and fatal shutdown errors in addition to uncaught exceptions. excluded_exceptionsarray of class strings []Throwable classes to skip during automatic capture. max_framesinteger 20Maximum number of stack frames included in $exception_list.context_providercallable or nullnullCallback that returns distinctIdand extra event properties for automatic captures. Verify error tracking
RecommendedTrigger a test exception to confirm events are being sent to PostHog. You should see them appear in the Error Tracking tab.
PHP