Customize clients
You can customize the Search API client, for example, by using a custom HTTP client, or by changing the user agent information.
To customize individual requests, for example, with headers or query parameters, see Request options.
Custom HTTP requester
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
<?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);
Custom user agent
The PHP Search API client sends user agent information
in the user-agent
header.
To append your own information, use the addAlgoliaAgent
function:
1
2
3
4
5
6
7
8
9
10
<?php
// Other imports and setup code omitted
use Algolia\AlgoliaSearch\Support\AlgoliaAgent;
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.