🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
API client / Getting started / Customize

Customize your PHP client

You can customize the behavior of the API clients by creating a custom configuration This lets you change timeouts, or add HTTP headers.

To modify all requests made with a client, create a custom configuration. To modify individual requests, pass custom request options.

We released a new version of the PHP API client in public beta. Read the beta documentation for more information.

Use a custom host

You can change the default hosts to which the API client connects:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require __DIR__ . '/vendor/autoload.php';

use Algolia\AlgoliaSearch\SearchClient;
use Algolia\AlgoliaSearch\Config\SearchConfig;

$config = [
    'appId' => 'YourApplicationID',
    'apiKey' => 'YourWriteAPIKey',
    'hosts' => ['yourapplication.example.net']
];

$client = SearchClient::createWithConfig(
    new SearchConfig($config)
);

$index = $client->initIndex('your_index_name');

Changing the hosts can be useful if you want to proxy the search requests through another server, for example, to process the request or response, or to perform custom analytics.

Add HTTP headers to every request

Adding HTTP headers to your configuration allow you to set parameters for every request, for example, a user identifier or an IP address. This can be useful for analytics, geographic searches, or to implement API key rate limits.

For an overview of available HTTP headers, see Add HTTP headers to your requests

1
2
3
4
5
6
7
8
9
use Algolia\AlgoliaSearch\Config\SearchConfig;
use Algolia\AlgoliaSearch\SearchClient;

$config = SearchConfig::create('YourApplicationID', 'YourWriteAPIKey');
$config->setDefaultHeaders([
  'headerName' => 'headerValue'
]);

$client = SearchClient::createWithConfig($config);

Change timeouts for all requests

Network connections and DNS resolution can be slow. That’s why the API clients come with default timeouts.

You shouldn’t change the default timeouts, unless you have a good reason.

1
2
3
4
5
6
$config = SearchConfig::create('YourApplicationID', 'YourWriteAPIKey');
$config->setConnectTimeout(2); // connection timeout in seconds
$config->setReadTimeout(30); // read timeout in seconds
$config->setWriteTimeout(30); // write timeout in seconds

SearchClient::createWithConfig($config);

Pass options to the HTTP client

The PHP API client lets you override what HTTP layer will be used by the search client. You can pass custom options to the underlying HTTP client, for example, to configure a proxy. You can choose between these two HTTP clients:

  • Guzzle, a popular HTTP client for PHP (recommended)
  • A custom HTTP client built on top of curl

Passing options to the Guzzle HTTP client

You can pass any guzzle option.

1
2
3
4
5
6
7
8
9
10
use Algolia\AlgoliaSearch\Algolia;
use GuzzleHttp\Client as GuzzleClient;

$httpClient = new Algolia\AlgoliaSearch\Http\Guzzle6HttpClient(
  new GuzzleClient([
    'proxy' => $proxyAddress,
  ])
);

Algolia::setHttpClient($httpClient);

Passing options to the default PHP client

You can pass any curl option.

1
2
3
4
5
6
7
8
use Algolia\AlgoliaSearch\Algolia;

$httpClient = new Algolia\AlgoliaSearch\Http\Php53HttpClient([
    'CURLOPT_PROXY' => $strProxy,
    'CURLOPT_FOLLOWLOCATION' => 1,
]);

Algolia::setHttpClient($httpClient);
Did you find this page helpful?