This page documents an earlier version of the API client. For the latest version, see Upgrade.
Upgrade from version 2 to version 3
The architecture of version 3 of the PHP API client stays the same, but it no longer supports PHP < 7.2. Update your PHP version to benefit from the new features.Upgrade from version 1 to version 2
The design of the latest version of the PHP client is almost the same as the earlier version to make upgrading as smooth as possible. The names of the client and index classes changed toSearchClient
and SearchIndex
with similar method names.
This new version is compatible with the same PHP versions,
from 5.3 to the latest. We recommended to use at least PHP 7.1.
Please keep in mind while updating:
- All deprecated methods and features from version 1 have been removed.
- You can’t manage API keys on the
Index
class. Use theClient
class instead. You can add index restrictions to get the same limitations. Delete existing keys on the index from the dashboard.
Upgrade the library
With composer
In yourcomposer.json
file:
- Update your
algolia/algoliasearch-client-php
dependency to^2.0
- If your PHP version is >= 5.5, require the dependency
guzzlehttp/guzzle: "^6.0"
Without composer
- Download the client from GitHub and unzip it in your project.
- Inside the client root folder, run
./bin/install-dependencies-without-composer
. - In your code, require the
autoload.php
file in the client root folder instead of thealgoliasearch.php
file.
Namespace and class names
The namespace changed fromAlgoliaSearch
to Algolia\AlgoliaSearch
.
The main Client
and Index
classes are named SearchClient
and SearchIndex
.
This clarifies which client accesses the Search API and which one access the others
(Analytics and Monitoring APIs).
Client instantiation
Replace the instantiation of the client as shown below.PHP
Instantiating with configuration
You can instantiate all clients with configuration objects. This is useful to change the way a client behaves. All setters have been removed. If, for instance, you rely onsetExtraHeaders
or setConnectTimeout
,
you need to change your code to use a configuration object:
PHP
forwardToReplicas
: set the default value offorwardToReplicas
to true.setDefaultForwardToReplicas
: change the default value of forwardToReplicas (false
by default)setBatchSize
: all write operations create batch automatically, you can change the batch size (1000
by default)setWaitTaskTimeBeforeRetry
: set a different interval time before eachgetTask
call when waiting
Using curl options
If you were passing curl options to theClient
, pass them to the HttpClient
instead.
You have to do this once, the library uses this HttpClient
for new clients you instantiate.
PHP
Analytics instantiation
Similarly, you need to update the way you initialize theAnalyticsClient
.
PHP
Optional methods parameters
To have the most consistent, predictable, and future-proof method signature, the API client follows three rules:- All required parameters have a single argument
- All optional arguments are passed in a
requestOptions
array as the last argument - The client never sets any default values
requestOptions
array.
You should go through all methods you use to check if you’re using optional parameters.
You can compare your code to the full list of method signature changes.
For example:
PHP
PHP
List of method signature changes
Before | After |
---|---|
setSettings($settings, true) | setSettings($settings, ['forwardToReplicas' => true]) |
copyIndex('source', 'dest') | copyIndex('source', 'dest') |
scopedCopyIndex('source', 'dest', ['settings', 'synonyms']) | copyIndex('source', 'dest', ['scope' => ['settings', 'synonyms']]) |
batchSynonyms($objects, true, false) | saveSynonyms($objects, ['forwardToReplicas' => true]) |
batchSynonyms($objects, true, true) | replaceAllSynonyms($objects, ['forwardToReplicas' => true]) |
batchSynonyms($objects, false, false) | saveSynonyms($objects) |
batchSynonyms($objects, false, true) | replaceAllSynonyms($objects) |
saveObjects($objects) | saveObjects($objects) |
saveObjects($objects, 'id') | saveObjects($objects, ['objectIDKey' => 'id]) |
addObjects($objectsWithObjectId) | saveObjects($objectsWithObjectId) |
addObjects($objectsWithoutObjectId) | saveObjects($objectsWithoutObjectId, ['autoGenerateObjectIDIfNotExist' => 'id]) |
$client->setExtraHeader('header-name', 'header-value') | $config->setDefaultHeaders(['header-name', 'header-value']) and pass the configuration to the client |
New methods
The new version introduces some new methods. Most of these are helpers for which the feature was already available, but required either deeper understanding or more custom code.copySettings
: copy settings between indices.copySynonyms
: copy synonyms between indices.copyRules
: copy rules between indices.replaceAllObjects
: add new objects to an index and remove all existing ones, atomically.replaceAllSynonyms
: add new synonyms to an index and remove all existing ones, atomically.replaceAllRules
: add new rules to an index and remove all existing ones, atomically.AccountClient::copyIndex
: copy in an index between Algolia applications. Useful when doing client work.
Removed methods
- All deprecated methods and features from version 1 have been removed. Most of the time, the feature is still available and was only renamed.
- API keys can’t be managed on the
Index
, only on theClient
. Add index restrictions to get the same limitations, and delete existing index keys from the Dashboard.
Exceptions
The namespace of the classAlgoliaException
exception has changed:
PHP
Doctor
Algolia Doctor is a command-line tool that helps you debug your Algolia implementation. It tests your environment and configuration to display useful information, like libraries to install orphp.ini
settings to update.