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

Algolia’s PHP API client lets you use Algolia’s APIs from your PHP app. Compared to using the APIs directly, using the PHP API client has these benefits:

  • Network retry strategy. The API client automatically retries connecting to another Algolia server, if the connection fails. Thanks to that, using the API clients is covered by Algolia’s SLA.

  • Reindexing. The API clients let you reindex your records in a single operation.

  • Automatic batching. When you add records to your index, the API client automatically batches your records to make fewer API requests.

Algolia’s PHP API client is open source. Find the source code on GitHub.

Do you want to integrate Algolia into your Laravel or Symfony app? Use Algolia’s Laravel integration and Symfony integration to get started even quicker.

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

Quickstart sample app

To download and run a self-contained example, see the PHP quickstart on GitHub.

Install the PHP API client

The Algolia PHP API client requires PHP version 7.2 or later.

Install the package via Composer:

$
composer require algolia/algoliasearch-client-php

Install manually

  1. Download the client from GitHub and unzip it in your project folder.
  2. Inside the client root folder, run ./bin/install-dependencies-without-composer.
  3. In your code, require the autoload.php in the client root folder.

Test your installation

If you haven’t already, create an Algolia account and create a new Algolia app to get started.

To test whether you can connect to Algolia, run a simple program that adds a record to a new index, searches the index, and print the results.

  1. Copy the following code sample into a text editor. If you’re signed in, the code samples below show your Algolia application ID. If you’re not signed in, replace YourApplicationID with your Algolia application ID and YourWriteAPIKey with your Write API Key. You can find both in your Algolia account.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    <?php
    // hello_algolia.php
    require __DIR__."/vendor/autoload.php";
    use Algolia\AlgoliaSearch\SearchClient;
    
    // Connect and authenticate with your Algolia app
    $client = SearchClient::create("YourApplicationID", "YourWriteAPIKey");
    
    // Create a new index and add a record
    $index = $client->initIndex("test_index");
    $record = ["objectID" => 1, "name" => "test_record"];
    $index->saveObject($record)->wait();
    
    // Search the index and print the results
    $results = $index->search("test_record");
    var_dump($results["hits"][0]);
    
  2. Save the file as hello_algolia.php. Go to the directory with the file you just created and run inside a terminal:

    1
    
    php hello_algolia.php
    
  3. If the program ran successfully, you should see:

    1
    2
    3
    4
    5
    6
    7
    
    array(3) {
       ["name"]=>
       string(11) "test_record"
       ["objectID"]=>
       string(1) "1"
       ["_highlightResult"]=>
       ... }
    

You can inspect your index now in the Algolia dashboard.

Next steps

Now you can interact with the Algolia Search API, you can look at the available methods, for example, for search or indexing.

Other APIs, for example for Algolia Recommend or Analytics, come with their own clients. To get an overview, see Initialize the API client.

Did you find this page helpful?