# Guzzle

PHP

[Guzzle](http://docs.guzzlephp.org/) is a PHP HTTP client that sends HTTP requests and integrates well with web services. Integrating Blackfire with Guzzle lets you **profile programmatically** your websites, HTTP APIs, or web services.

## Installation

Add [Blackfire PHP SDK](sdk#php-sdk-installation) as a dependency on your project (1.21+ version).

> **Caution**
>
> This document assumes Guzzle **version 6+**.

## Profiling

The Blackfire PHP SDK provides a **Guzzle middleware** that eases the profiling of requests made via Guzzle:

```php
use GuzzleHttp\Client as GuzzleClient;
use Blackfire\Client as BlackfireClient;
use Blackfire\ClientConfiguration;
use Blackfire\Bridge\Guzzle\Middleware as BlackfireMiddleware;

$blackfire = new BlackfireClient(new ClientConfiguration($clientId, $clientToken));

$guzzle = new GuzzleClient([
    // useful if you want to simulate a user experience
    'cookies' => true,
]);

$guzzle->getConfig('handler')->push(BlackfireMiddleware::create($blackfire), 'blackfire');
```

Whenever you want to profile a request, set the `blackfire` option to `true`:

```php
use Blackfire\Client as BlackfireClient;
use Blackfire\ClientConfiguration;
use GuzzleHttp\Client as GuzzleClient;
use Blackfire\Bridge\Guzzle\Middleware as BlackfireMiddleware;

$blackfire = new BlackfireClient(new ClientConfiguration($clientId, $clientToken));

$guzzle = new GuzzleClient(['cookies' => true]);
$guzzle->getConfig('handler')->push(BlackfireMiddleware::create($blackfire), 'blackfire');

$response = $guzzle->request('GET', 'https://www.symfony.com/blog/', array(
    'blackfire' => true
));

// get the profile for the request
$profile = $blackfire->getProfile($response->getHeader('X-Blackfire-Profile-Uuid')[0]);
```

If you execute this script, a profile will be created on your Blackfire account and tests defined in your `.blackfire.yaml` if any will be executed as well. Note that the profile UUID is stored in the `X-Blackfire-Profile-Uuid`response header by the Blackfire middleware.

If you want to better control the profile configuration, like the title of the profile, pass a `\Blackfire\Profile\Configuration` instance as the `blackfire` option value:

```php
$config = new \Blackfire\Profile\Configuration();
$config->setTitle('Blog Home');

$response = $client->request('GET', 'https://www.symfony.com/blog/', array(
    'blackfire' => $config,
));
```

Learn more about [profiling configuration settings](sdk#php-sdk-profile-configuration) in the PHP SDK documentation.
