This project extends the Application Insights API surface to support PHP.

0.1.1 2020-10-26 13:37 UTC

This package is auto-updated.

Last update: 2021-11-26 16:15:23 UTC


This project was forked from and builds upon the official php sdk from windows, which is no longer mantained. As of now, it only supports exception logging.



This SDK is NOT maintained or supported by Microsoft even though they've contributed to it in the past. Note that Azure Monitor only provides support when using supported SDKs.


  • PHP version ^7.1


Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Open a command console, enter your project directory and execute:

$ composer require halloverden/application-insights-sdk


Once installed, you can send exception telemetry to Application Insights. Here are a few samples.

Note: before you can send data to you will need an instrumentation key. Please see the Getting an Application Insights Instrumentation Key section for more information.

Initializing the client and setting the instrumentation key and other optional configurations

$telemetryClient = new \ApplicationInsights\Telemetry_Client();
$context = $telemetryClient->getContext();

// Necessary
$context->setInstrumentationKey('YOUR INSTRUMENTATION KEY');

// Optional
$context->getUserContext()->setId('YOUR USER ID');
$context->getApplicationContext()->setVer('YOUR VERSION');
$context->getLocationContext()->setIp('YOUR IP');

// Start tracking throwables

Setup Operation context

For correct Application Insights reporting you need to setup Operation Context, reference to Request

$telemetryClient->getContext()->getOperationContext()->setName('GET Index');

Sending an exception telemetry, with custom properties and metrics

    // Do something to throw an exception
catch (\Exception $ex)
    $telemetryClient->trackException($ex, ['InlineProperty' => 'test_value'], ['duration_inner' => 42.0]);

Set the Client to gzip the data before sending


Registering an exception handler

class Handle_Exceptions
    private $_telemetryClient;

    public function __construct()
        $this->_telemetryClient = new \ApplicationInsights\Telemetry_Client();
        $this->_telemetryClient->getContext()->setInstrumentationKey('YOUR INSTRUMENTATION KEY');

        set_exception_handler(array($this, 'exceptionHandler'));

    function exceptionHandler(\Exception $exception)
        if ($exception != NULL)