Customize clients
If you want to change all requests, you can customize the API clients, for example, by using a custom HTTP client, changing the user agent information, or changing the default timeouts.
To customize individual requests, see Request options.
Logging
You can turn on debug logging with the setDebug
method.
To write the log into a file, use the setDebugFile
method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
require_once realpath(__DIR__.'/vendor/autoload.php');
use Algolia\AlgoliaSearch\Api\SearchClient;
use Algolia\AlgoliaSearch\Configuration\SearchConfig;
$config = SearchConfig::create(
appId: 'ALGOLIA_APPLICATION_ID',
apiKey: 'ALGOLIA_API_KEY',
);
$config->setDebug(true);
$config->setDebugFile('path/to/logfile');
$client = SearchClient::createWithConfig(config: $config);
Timeouts
To change the timeouts for all requests, use the appropriate setter methods:
1
2
3
4
5
6
<?php
// Additional code omitted
$config->setConnectTimeout(100);
$config->setReadTimeout(100);
$config->setWriteTimeout(100);
connectTimeout
|
type: Duration
default: 2
Maximum number of seconds to wait for the connection to be established.
To change this default timeout for all requests, use the |
readTimeout
|
type: Duration
default: 5
Maximum number of seconds to wait for a response from the server (for |
writeTimeout
|
type: Duration
default: 30
Maximum number of seconds to wait for a response from the server (for requests other than |
User agent information
The API clients send user agent information as User-Agent
header.
To append your own information to this parameter,
use the addAlgoliaAgent
method:
1
2
3
4
5
6
7
8
9
10
11
<?php
use Algolia\AlgoliaSearch\Support\AlgoliaAgent;
// Additional code omitted
AlgoliaAgent::addAlgoliaAgent(
clientName: $client->getClientConfig()->getClientName(),
segment: 'custom php client',
version: 'optional version'
);
This appends “custom php client (optional version)” to the user-agent
header.
Custom HTTP requesters
You can use your own client for making HTTP requests with a custom configuration.
The client class must implement HttpClientInterface
.
By default, the PHP API clients use GuzzleHttpClient
.
For example, to use the CurlHttpClient
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
require_once realpath(__DIR__.'/vendor/autoload.php');
use Algolia\AlgoliaSearch\Api\SearchClient;
use Algolia\AlgoliaSearch\Configuration\SearchConfig;
use Algolia\AlgoliaSearch\Http\CurlHttpClient;
use Algolia\AlgoliaSearch\RetryStrategy\ApiWrapper;
use Algolia\AlgoliaSearch\RetryStrategy\ClusterHosts;
// Create a custom config
$config = SearchConfig::create(
appId: 'ALGOLIA_APPLICATION_ID',
apiKey: 'ALGOLIA_API_KEY',
);
// Get the servers for your Algolia cluster
$clusterHosts = ClusterHosts::createFromAppId(applicationId: $appID);
// Create a new HTTP client (implements `HttpClientInterface`)
$customHttpClient = new CurlHttpClient();
$apiWrapper = new ApiWrapper(
http: $customHttpClient,
config: $config,
clusterHosts: $clusterHosts,
);
// Create a custom Search API client
$client = new SearchClient(apiWrapper: $apiWrapper, config: $config);
$client = new SearchClient(config: $config);
Headers
To add headers to all requests,
use the setDefaultHeaders
method.
1
2
3
4
5
<?php
// Additional code omitted
$config->setDefaultHeaders(['extra-header' => 'greetings']);