Start integrating any APIs with the Bearer client for PHP

The Bearer Client has been deprecated. Below you'll find information on its usage, however, we suggest using the Bearer Agent for your API consumption and usage going forward.

Installing & initializing

composer require bearer/bearer-php
use Bearer;
# Initialize the client with your Bearer secret key
$bearer = new Bearer\Client(BEARER_SECRET_KEY);

Replace the BEARER_SECRET_KEY with your Secret Key that can be found on your Developer Keys page.

Calling APIs

Once you've added an API through the Bearer dashboard, you can make request to it as follow:

# GET request to the GitHub API
->get('/repositories', ["query" => ["since" => 364 ]]);

Most common HTTP methods are supported out-of-the-box, including .get() , .post(), .put()and.delete().

To pass parameters, construct your request as follow:

->integration('github') \
->get('/endpoint', [ \
"query": [ 'foo' => 'bear' ], # your query parameters as key/value
"body": [], # your body as key/value or string
"headers": [ 'foo': 'bear' ] # your headers as key/value

Behind the scene, we use the cURL extension for PHP. Thus, each request will return the output of a curl_exec.

Credentials management

By default, all requests will be authenticated using the latest credentials saved into your account for that API. Thus, changing the API credentials in your account will be instantly reflected into your code.

If you want to use custom credentials, for example your customers credentials, first retrieve them on your frontend with the setup component, then update your code to use the .setup() method as follow:

# GET request to the GitHub API using custom credentials
# (retrieve a setupId with the Bearer setup component)
->get('/repositories', ["query" => ["since" => 364 ]]);

Dealing with OAuth APIs

When the API you want to use rely on OAuth as an authentication method, Bearer will handle the OAuth dance for you.

Just add a connect button to your frontend to let your users connect to the service. Then update your code to use the .auth() method as follow:

# POST request using an authId (only for OAuth APIs)
# (generate an authId with the Bearer connect button)
->post('/user/repos', ["body" => ["name" => "Just setting up my" ]]);

Request timeout

The PHP client has a 5 seconds timeout per default. After that period of time, the HTTP connection will be closed by the client.

// sets timeout to 10 seconds
$bearer = new Bearer\Client('BEARER_SECRET_KEY', [CURLOPT_TIMEOUT => 10]);
// sets connect timeout to 1 second
$integration = $bearer->integration('integration_id', [CURLOPT_CONNECTTIMEOUT => 1]);

You can increase the timeout up to 30 seconds, but not more, as it's the limit of our servers.

Tracking requests

The Bearer PHP client allows you to set a correlationId to make logs grouping easier. This can be done by using the special Bearer-Correlation-Id header.

$github->get('/user/repos', [ "headers" => [ "Bearer-Correlation-Id" => "your-correlation-id"] ]);

On the Bearer dashboard, you can then filter the logs using your-correlation-id.

Client reference

This page contains the most common use case for the Bearer API client in PHP.

If you are trying to achieve something not listed here, dive into our PHP client reference to learn more about it. It includes details about the functions, classes, return types, arguments and more. As well as the source code of the client.