Skip to content

Treblle with PHP

Requirements

  • PHP 7.4+

Dependencies

Installation

You can install Treblle for PHP via Composer. Simply run the following command:

Terminal window
$ composer require treblle/treblle-php

Getting started

Next, create a FREE account on treblle.com to get an API key and Project ID. After you have those simply initialize Treblle in your API code like so:

<?php
declare(strict_types=1);
use GuzzleHttp\Client;
use Treblle\Factory\TreblleFactory;
require_once __DIR__.'/../vendor/autoload.php';
error_reporting(E_ALL);
ob_start();
$treblle = TreblleFactory::create('_YOUR_API_KEY_', '_YOUR_PROJECT_ID_');

That’s it. Your API requests and responses are now being sent to your Treblle project. Just by adding that line of code you get features like: auto-documentation, real-time request/response monitoring, error tracking and so much more.

Configuration options

Debug mode

The third parameter sent to TreblleFactory::create factory method is a boolean flag indicating whether we want to use Treblle in debug mode. Enabling debug mode is helpful when you want to understand what’s happening under-the-hood and allow Treblle errors to bubble up.

<?php
declare(strict_types=1);
use GuzzleHttp\Client;
use Treblle\Factory\TreblleFactory;
// DON'T FORGET TO AUTOLOAD COMPOSER DEPENDENCIES
require_once __DIR__.'/../vendor/autoload.php';
error_reporting(E_ALL);
ob_start();
$treblle = TreblleFactory::create(
'_YOUR_API_KEY_',
'_YOUR_PROJECT_ID_',
false, // Debug mode
);

Masking sensitive information

Treblle masks sensitive information from the request parameters before it even leaves your server. The following parameters are automatically masked: password, pwd, secret, password_confirmation, cc, card_number, ccv, ssn, credit_score. You can extend this list by providing your own custom keywords by doing the following:

<?php
declare(strict_types=1);
use GuzzleHttp\Client;
use Treblle\Factory\TreblleFactory;
// DON'T FORGET TO AUTOLOAD COMPOSER DEPENDENCIES
require_once __DIR__.'/../vendor/autoload.php';
error_reporting(E_ALL);
ob_start();
/*
* Pass an array of words that you would like to be masked
* as a fourth parameter when initializing Treblle
*/
$treblle = TreblleFactory::create(
'_YOUR_API_KEY_',
'_YOUR_PROJECT_ID_',
false, // Debug mode
['keyword', 'maskme', 'sensitive']
);

Overriding HTTP client and Treblle endpoint URL

The fifth parameter passed to TreblleFactory::create factory method is an array of config options allowing you to override some components of Treblle.

The following values are supported:

  • client - an instance of Guzzle client configured to behave as you want (e.g. controling the timeout or other aspects)
  • url - Treblle API endpoint URL you want to use
<?php
declare(strict_types=1);
use GuzzleHttp\Client;use Treblle\Factory\TreblleFactory;
// DON'T FORGET TO AUTOLOAD COMPOSER DEPENDENCIES
require_once __DIR__.'/../vendor/autoload.php';
error_reporting(E_ALL);
ob_start();
/*
* Pass an array of words that you would like to be masked
* as a fourth parameter when initializing Treblle
*/
$treblle = TreblleFactory::create(
'_YOUR_API_KEY_',
'_YOUR_PROJECT_ID_',
false, // Debug mode
['keyword', 'maskme', 'sensitive'],
['client' => new Client(), 'url' => 'https://custom.treblle.com']
);