PHP Class AlgoliaSearch\Client

You should instantiate a Client object with your ApplicationID, ApiKey and Hosts to start using Algolia Search API
Exibir arquivo Open project: algolia/algoliasearch-client-php Class Usage Examples

Protected Properties

Property Type Description
$caInfoPath string
$context ClientContext
$curlConstants array
$curlOptions array
$placesEnabled boolean

Public Methods

Method Description
__construct ( string $applicationID, string $apiKey, array | null $hostsArray = null, array $options = [] ) Algolia Search initialization.
__destruct ( ) Release curl handle.
addUserKey ( array $obj, integer $validity, integer $maxQueriesPerIPPerHour, integer $maxHitsPerQuery, array | null $indexes = null ) : mixed Create a new user key.
batch ( array $requests ) : mixed Send a batch request targeting multiple indices.
buildQuery ( array $args ) : string
copyIndex ( string $srcIndexName, string $dstIndexName ) : mixed Copy an existing index.
deleteIndex ( string $indexName ) : mixed Delete an index.
deleteUserKey ( string $key ) : mixed Delete an existing user key.
disableRateLimitForward ( ) Disable IP rate limit enabled with enableRateLimitForward() function.
doRequest ( ClientContext $context, string $method, string $host, string $path, array $params, array $data, integer $connectTimeout, integer $readTimeout ) : mixed
enableRateLimitForward ( string $adminAPIKey, string $endUserIP, string $rateLimitAPIKey ) Allow to use IP rate limit when you have a proxy between end-user and Algolia.
generateSecuredApiKey ( string $privateApiKey, mixed $query, string | null $userToken = null ) : string Generate a secured and public API Key from a list of query parameters and an optional user token identifying the current user.
getLogs ( integer $offset, integer $length = 10, mixed $type = 'all' ) : mixed Return last logs entries.
getUserKeyACL ( string $key ) : mixed Get ACL of a user key.
initIndex ( string $indexName ) : Index Get the index object initialized (no server call needed for initialization).
initPlaces ( string $appId, string $apiKey, array $hostsArray = null, array $options = [] ) : PlacesIndex
isAlive ( ) Call isAlive.
listIndexes ( ) : mixed List all existing indexes return an object in the form: array( "items" => array( array("name" => "contacts", "createdAt" => "2013-01-18T15:33:13.556Z"), array("name" => "notes", "createdAt" => "2013-01-18T15:33:13.556Z") ) ).
listUserKeys ( ) : mixed List all existing user keys with their associated ACLs.
moveIndex ( string $srcIndexName, string $dstIndexName ) : mixed Move an existing index.
multipleQueries ( array $queries, string $indexNameKey = 'indexName', string $strategy = 'none' ) : mixed This method allows to query multiple indexes with one API call.
request ( ClientContext $context, string $method, string $path, array $params, array $data, array $hostsArray, integer $connectTimeout, integer $readTimeout ) : mixed
setAlgoliaUserToken ( string $token ) It's possible to use the following token to track users that have the same IP or to track users that use different devices.
setConnectTimeout ( integer $connectTimeout, integer $timeout = 30, integer $searchTimeout = 5 ) Change the default connect timeout of 2s to a custom value (only useful if your server has a very slow connectivity to Algolia backend).
setExtraHeader ( string $key, string $value ) Allow to set custom headers.
setForwardedFor ( string $ip ) The aggregation of the queries to retrieve the latest query uses the IP or the user token to work efficiently.
updateUserKey ( string $key, array $obj, integer $validity, integer $maxQueriesPerIPPerHour, integer $maxHitsPerQuery, array | null $indexes = null ) : mixed Update a user key.

Protected Methods

Method Description
checkCurlOptions ( array $curlOptions ) : array Checks if curl option passed are valid curl options.
getCurlConstants ( ) : array Get all php curl available options.
invalidOptions ( array $curlOptions = [], string $errorMsg = '' ) throw clear Exception when bad curl option is set.

Private Methods

Method Description
getPlacesIndex ( ) : PlacesIndex

Method Details

__construct() public method

Algolia Search initialization.
public __construct ( string $applicationID, string $apiKey, array | null $hostsArray = null, array $options = [] )
$applicationID string the application ID you have in your admin interface
$apiKey string a valid API key for the service
$hostsArray array | null the list of hosts that you have received for the service
$options array

__destruct() public method

Release curl handle.
public __destruct ( )

addUserKey() public method

Create a new user key.
public addUserKey ( array $obj, integer $validity, integer $maxQueriesPerIPPerHour, integer $maxHitsPerQuery, array | null $indexes = null ) : mixed
$obj array can be two different parameters: The list of parameters for this key. Defined by an array that can contain the following values: - acl: array of string - indices: array of string - validity: int - referers: array of string - description: string - maxHitsPerQuery: integer - queryParameters: string - maxQueriesPerIPPerHour: integer Or the list of ACL for this key. Defined by an array of string that can contains the following values: - search: allow to search (https and http) - addObject: allows to add/update an object in the index (https only) - deleteObject : allows to delete an existing object (https only) - deleteIndex : allows to delete index content (https only) - settings : allows to get index settings (https only) - editSettings : allows to change index settings (https only)
$validity integer the number of seconds after which the key will be automatically removed (0 means no time limit for this key)
$maxQueriesPerIPPerHour integer Specify the maximum number of API calls allowed from an IP address per hour. Defaults to 0 (no rate limit).
$maxHitsPerQuery integer Specify the maximum number of hits this API key can retrieve in one call. Defaults to 0 (unlimited)
$indexes array | null Specify the list of indices to target (null means all)
return mixed

batch() public method

Send a batch request targeting multiple indices.
public batch ( array $requests ) : mixed
$requests array an associative array defining the batch request body
return mixed

buildQuery() public static method

public static buildQuery ( array $args ) : string
$args array
return string

checkCurlOptions() protected method

Checks if curl option passed are valid curl options.
protected checkCurlOptions ( array $curlOptions ) : array
$curlOptions array must be array but no type required while first test throw clear Exception
return array

copyIndex() public method

Copy an existing index.
public copyIndex ( string $srcIndexName, string $dstIndexName ) : mixed
$srcIndexName string the name of index to copy.
$dstIndexName string the new index name that will contains a copy of srcIndexName (destination will be overwritten if it already exist).
return mixed

deleteIndex() public method

Delete an index.
public deleteIndex ( string $indexName ) : mixed
$indexName string the name of index to delete
return mixed an object containing a "deletedAt" attribute

deleteUserKey() public method

Delete an existing user key.
public deleteUserKey ( string $key ) : mixed
$key string
return mixed

disableRateLimitForward() public method

Disable IP rate limit enabled with enableRateLimitForward() function.

doRequest() public method

public doRequest ( ClientContext $context, string $method, string $host, string $path, array $params, array $data, integer $connectTimeout, integer $readTimeout ) : mixed
$context ClientContext
$method string
$host string
$path string
$params array
$data array
$connectTimeout integer
$readTimeout integer
return mixed

enableRateLimitForward() public method

This option will set the X-Forwarded-For HTTP header with the client IP and the X-Forwarded-API-Key with the API Key having rate limits.
public enableRateLimitForward ( string $adminAPIKey, string $endUserIP, string $rateLimitAPIKey )
$adminAPIKey string the admin API Key you can find in your dashboard
$endUserIP string the end user IP (you can use both IPV4 or IPV6 syntax)
$rateLimitAPIKey string the API key on which you have a rate limit

generateSecuredApiKey() public static method

Generate a secured and public API Key from a list of query parameters and an optional user token identifying the current user.
public static generateSecuredApiKey ( string $privateApiKey, mixed $query, string | null $userToken = null ) : string
$privateApiKey string your private API Key
$query mixed the list of query parameters applied to the query (used as security)
$userToken string | null an optional token identifying the current user
return string

getCurlConstants() protected method

Get all php curl available options.
protected getCurlConstants ( ) : array
return array

getLogs() public method

Return last logs entries.
public getLogs ( integer $offset, integer $length = 10, mixed $type = 'all' ) : mixed
$offset integer Specify the first entry to retrieve (0-based, 0 is the most recent log entry).
$length integer Specify the maximum number of entries to retrieve starting at offset. Maximum allowed value: 1000.
$type mixed
return mixed

getUserKeyACL() public method

Get ACL of a user key.
public getUserKeyACL ( string $key ) : mixed
$key string
return mixed

initIndex() public method

Get the index object initialized (no server call needed for initialization).
public initIndex ( string $indexName ) : Index
$indexName string the name of index
return Index

initPlaces() public static method

public static initPlaces ( string $appId, string $apiKey, array $hostsArray = null, array $options = [] ) : PlacesIndex
$appId string
$apiKey string
$hostsArray array
$options array
return PlacesIndex

invalidOptions() protected method

throw clear Exception when bad curl option is set.
protected invalidOptions ( array $curlOptions = [], string $errorMsg = '' )
$curlOptions array
$errorMsg string add specific message for disambiguation

isAlive() public method

Call isAlive.
public isAlive ( )

listIndexes() public method

List all existing indexes return an object in the form: array( "items" => array( array("name" => "contacts", "createdAt" => "2013-01-18T15:33:13.556Z"), array("name" => "notes", "createdAt" => "2013-01-18T15:33:13.556Z") ) ).
public listIndexes ( ) : mixed
return mixed

listUserKeys() public method

List all existing user keys with their associated ACLs.
public listUserKeys ( ) : mixed
return mixed

moveIndex() public method

Move an existing index.
public moveIndex ( string $srcIndexName, string $dstIndexName ) : mixed
$srcIndexName string the name of index to copy.
$dstIndexName string the new index name that will contains a copy of srcIndexName (destination will be overwritten if it already exist).
return mixed

multipleQueries() public method

This method allows to query multiple indexes with one API call.
public multipleQueries ( array $queries, string $indexNameKey = 'indexName', string $strategy = 'none' ) : mixed
$queries array
$indexNameKey string
$strategy string
return mixed

request() public method

public request ( ClientContext $context, string $method, string $path, array $params, array $data, array $hostsArray, integer $connectTimeout, integer $readTimeout ) : mixed
$context ClientContext
$method string
$path string
$params array
$data array
$hostsArray array
$connectTimeout integer
$readTimeout integer
return mixed

setAlgoliaUserToken() public method

It's possible to use the following token to track users that have the same IP or to track users that use different devices.
See also: https://www.algolia.com/doc/faq/analytics/will-the-analytics-still-work-if-i-perform-the-search-through-my-backend
public setAlgoliaUserToken ( string $token )
$token string

setConnectTimeout() public method

Change the default connect timeout of 2s to a custom value (only useful if your server has a very slow connectivity to Algolia backend).
public setConnectTimeout ( integer $connectTimeout, integer $timeout = 30, integer $searchTimeout = 5 )
$connectTimeout integer the connection timeout
$timeout integer the read timeout for the query
$searchTimeout integer the read timeout used for search queries only

setExtraHeader() public method

Allow to set custom headers.
public setExtraHeader ( string $key, string $value )
$key string
$value string

setForwardedFor() public method

If the queries are made from your backend server, the IP will be the same for all of the queries. We're supporting the following HTTP header to forward the IP of your end-user to the engine, you just need to set it for each query.
See also: https://www.algolia.com/doc/faq/analytics/will-the-analytics-still-work-if-i-perform-the-search-through-my-backend
public setForwardedFor ( string $ip )
$ip string

updateUserKey() public method

Update a user key.
public updateUserKey ( string $key, array $obj, integer $validity, integer $maxQueriesPerIPPerHour, integer $maxHitsPerQuery, array | null $indexes = null ) : mixed
$key string
$obj array can be two different parameters: The list of parameters for this key. Defined by a array that can contains the following values: - acl: array of string - indices: array of string - validity: int - referers: array of string - description: string - maxHitsPerQuery: integer - queryParameters: string - maxQueriesPerIPPerHour: integer Or the list of ACL for this key. Defined by an array of string that can contains the following values: - search: allow to search (https and http) - addObject: allows to add/update an object in the index (https only) - deleteObject : allows to delete an existing object (https only) - deleteIndex : allows to delete index content (https only) - settings : allows to get index settings (https only) - editSettings : allows to change index settings (https only)
$validity integer the number of seconds after which the key will be automatically removed (0 means no time limit for this key)
$maxQueriesPerIPPerHour integer Specify the maximum number of API calls allowed from an IP address per hour. Defaults to 0 (no rate limit).
$maxHitsPerQuery integer Specify the maximum number of hits this API key can retrieve in one call. Defaults to 0 (unlimited)
$indexes array | null Specify the list of indices to target (null means all)
return mixed

Property Details

$caInfoPath protected_oe property

protected string $caInfoPath
return string

$context protected_oe property

protected ClientContext,algoliasearch $context
return ClientContext

$curlConstants protected_oe property

protected array $curlConstants
return array

$curlOptions protected_oe property

protected array $curlOptions
return array

$placesEnabled protected_oe property

protected bool $placesEnabled
return boolean