Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion php/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ This quickstart demonstrates various usages of the the [Algolia PHP API Client](

- An Algolia account. If you don't have one already, [create an account for free](https://www.algolia.com/users/sign_up).
- A PHP ^7.2 local environment, or [Docker](https://www.docker.com/get-started).
- Install [Composer](https://getcomposer.org/).
- Install the latest version of the Algolia PHP API Client by running this command:

```bash
php composer require algolia/algoliasearch-client-php
```
<details>
<summary>Using VSCode</summary>

Expand All @@ -20,6 +25,10 @@ This quickstart demonstrates various usages of the the [Algolia PHP API Client](
1. Create an Algolia Application and an [Algolia Index](https://www.algolia.com/doc/guides/getting-started/quick-start/tutorials/getting-started-with-the-dashboard/#indices)
2. Copy the file [.env.example](.env.example) and rename it to `.env`
3. Set the environment variables `ALGOLIA_APP_ID`, `ALGOLIA_API_KEY` and `ALGOLIA_INDEX_NAME` in the `.env` file. You can obtain those from the [Algolia Dashboard](https://www.algolia.com/api-keys/). The `ALGOLIA_API_KEY` should be the "Admin API Key" (necessary for indexing).
4. If you'd like to use the quickstart file `rest_api_return_top_hits.php`, please include the environment variable `URL_DOMAIN` in the `.env` file. The following URLs are available.

**United States:** [https://analytics.us.algolia.com](https://analytics.us.algolia.com)
**Europe (Germany):** [https://analytics.de.algolia.com](https://analytics.de.algolia.com)

## How to use

Expand All @@ -40,5 +49,5 @@ php simple.php
| [rules.php](./rules.php) | Export rules and add a new rule to an index |
| [backup.php](./backup.php) | Backup an index |
| [restore.php](./restore.php) | Restore an index |
| [rest_api_return_top_hits.php](./rest_api_return_top_hits.php) | Get top 1000 searches with Analytics REST API |
| [return_top_hits.php](./return_top_hits.php) | Get top 1000 searches with Analytics REST API |
| [generate_key.php](./generate_key.php) | Generate a rate-limited search only API key |
18 changes: 8 additions & 10 deletions php/backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Install the API client: https://www.algolia.com/doc/api-client/getting-started/install/php/?client=php
require __DIR__.'/vendor/autoload.php';

use Algolia\AlgoliaSearch\Api\SearchClient;

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

Expand All @@ -13,18 +15,14 @@
$ALGOLIA_INDEX_NAME = $_ENV['ALGOLIA_INDEX_NAME'];

# Start the API client
# https://www.algolia.com/doc/api-client/getting-started/initialize/php/?client=php#initialize-the-search-client
$client = \Algolia\AlgoliaSearch\SearchClient::create($ALGOLIA_APP_ID, $ALGOLIA_API_KEY);

# Create an index (or connect to it, if an index with the name `ALGOLIA_INDEX_NAME` already exists)
# https://www.algolia.com/doc/api-client/getting-started/initialize/php/?client=php#initialize-the-search-client
$index = $client->initIndex($ALGOLIA_INDEX_NAME);
# https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/
$client = SearchClient::create($ALGOLIA_APP_ID, $ALGOLIA_API_KEY);

# Get all records from an index
# https://www.algolia.com/doc/api-reference/api-methods/browse/#get-all-records-from-an-index
# Use an API key with `browse` ACL
print("All the records:");
$records = $index->browseObjects();
$records = $client->browse($ALGOLIA_INDEX_NAME,);
var_dump($records);
print("\n");

Expand All @@ -43,7 +41,7 @@
# Retrieve settings for an index
# https://www.algolia.com/doc/api-reference/api-methods/get-settings/#retrieve-settings-for-an-index
print("Index settings:\n");
$settings = $index->getSettings();
$settings = $client->getSettings($ALGOLIA_INDEX_NAME, 1);
var_dump($settings);
print("\n");

Expand All @@ -59,7 +57,7 @@
# Export rules
# https://www.algolia.com/doc/api-reference/api-methods/export-rules/
print("Rules:\n");
$rules = $index->browseRules();
$rules = $client->browseRules($ALGOLIA_INDEX_NAME);
var_dump($rules);
print("\n");

Expand All @@ -78,7 +76,7 @@
# Export synonyms
# https://www.algolia.com/doc/api-reference/api-methods/export-synonyms/
print("Synonyms:\n");
$synonyms = $index->browseSynonyms();
$synonyms = $client->browseSynonyms($ALGOLIA_INDEX_NAME);
var_dump($synonyms);
print("\n");

Expand Down
17 changes: 9 additions & 8 deletions php/change_index_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Install the API client: https://www.algolia.com/doc/api-client/getting-started/install/php/?client=php
require __DIR__.'/vendor/autoload.php';

use Algolia\AlgoliaSearch\Api\SearchClient;

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

Expand All @@ -14,15 +16,12 @@

# Start the API client
# https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/
$client = \Algolia\AlgoliaSearch\SearchClient::create($ALGOLIA_APP_ID, $ALGOLIA_API_KEY);

# Create an index (or connect to it, if an index with the name `ALGOLIA_INDEX_NAME` already exists)
# https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/#initialize-an-index
$index = $client->initIndex($ALGOLIA_INDEX_NAME);
$client = SearchClient::create($ALGOLIA_APP_ID, $ALGOLIA_API_KEY);

# Set index settings
# https://www.algolia.com/doc/api-reference/api-methods/set-settings/
$index->setSettings(
$res = $client->setSettings(
$ALGOLIA_INDEX_NAME,
[
'searchableAttributes' => ['actors', 'genre'],
'customRanking' => ['desc(rating)'],
Expand All @@ -31,12 +30,14 @@
[
'forwardToReplicas' => true
]
)->wait();
);

$client->waitForTask($ALGOLIA_INDEX_NAME, $res['taskID']);

# Printing settings
# https://www.algolia.com/doc/api-reference/api-methods/get-settings/
print("Index settings:\n");
$settings = $index->getSettings();
$settings = $client->getSettings($ALGOLIA_INDEX_NAME, 1);
var_dump($settings);

?>
2 changes: 1 addition & 1 deletion php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"prefer-stable": true,
"require": {
"php": "^7.2 || ^8.0",
"algolia/algoliasearch-client-php": "^3.0",
"algolia/algoliasearch-client-php": "^4.37",
"vlucas/phpdotenv": "^5.3"
},
"config": {
Expand Down
Loading