Algolia DevCon
Oct. 2–3 2024, virtual.

The PHP API clients let you interact with Algolia’s APIs from your PHP backend.

If you’re building a Laravel app, see Algolia for Laravel. If you’re building a Symfony app, see Algolia SearchBundle for more information.

Install the library

All API clients are part of the algoliasearch-client-php package, which you can install with composer.

1
composer require algolia/algoliasearch-client-php "^4.0"

Test your installation

To test your installation, try running a short program that adds a record to a test index, searches your index, and prints the results.

  1. If you haven’t already, create an Algolia account.

  2. Copy the following code into a new file hello_algolia.php. Replace ALGOLIA_APPLICATION_ID and ALGOLIA_API_KEY with values from your account. Make sure to use an API key with addObject and search permissions.

    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
    31
    32
    
    <?php
    // File: hello_algolia.php
    require_once realpath(__DIR__ . "/vendor/autoload.php");
    
    use Algolia\AlgoliaSearch\Api\SearchClient;
    $appID = "ALGOLIA_APPLICATION_ID";
    // API key with `addObject` and `search` ACL
    $apiKey = "ALGOLIA_API_KEY";
    $indexName = "test-index";
    
    $client = SearchClient::create($appID, $apiKey);
    
    // Create a new record
    $record = [
      "objectID" => "object-1",
      "name" => "test record",
    ];
    
    // Add the record to an index
    $saveResp = $client->saveObject($indexName, $record);
    
    // Wait until indexing is done
    $client->waitForTask($indexName, $saveResp['taskID']);
    
    // Search for 'test'
    $searchResponse = $client->search(
      ['requests' => [
        ['indexName' => $indexName, 'query' => 'test']
      ]],
    );
    
    echo json_encode($searchResponse);
    

    In production, don’t include your credentials in your code. Use environment variables instead.

  3. Run: php hello_algolia.php

If the command is successful, you’ll see the API response in JSON format.

Next steps

You can view your new index in the Algolia dashboard.

Did you find this page helpful?
PHP API clients v4