diff --git a/php/README.md b/php/README.md index 5a12949..50b70e2 100644 --- a/php/README.md +++ b/php/README.md @@ -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 +```
Using VSCode @@ -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 @@ -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 | diff --git a/php/backup.php b/php/backup.php index a8c189b..4571a83 100644 --- a/php/backup.php +++ b/php/backup.php @@ -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(); @@ -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"); @@ -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"); @@ -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"); @@ -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"); diff --git a/php/change_index_settings.php b/php/change_index_settings.php index dcbf8d0..b79d096 100644 --- a/php/change_index_settings.php +++ b/php/change_index_settings.php @@ -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(); @@ -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)'], @@ -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); ?> diff --git a/php/composer.json b/php/composer.json index 5f473f6..e3d3e6b 100644 --- a/php/composer.json +++ b/php/composer.json @@ -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": { diff --git a/php/composer.lock b/php/composer.lock index 6fb2f42..ccef509 100644 --- a/php/composer.lock +++ b/php/composer.lock @@ -4,57 +4,49 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1b4ac595ed8267b6ea1052b56243ba5d", + "content-hash": "9b1ac9b166451b92a9d981d3cb87d899", "packages": [ { "name": "algolia/algoliasearch-client-php", - "version": "3.1.0", + "version": "4.37.0", "source": { "type": "git", "url": "https://github.com/algolia/algoliasearch-client-php.git", - "reference": "42793a97c99a68adfe8e39eadadd970b5196f208" + "reference": "cffa68164fa1af8a19e40aefe73d4861e7f5a66f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/algolia/algoliasearch-client-php/zipball/42793a97c99a68adfe8e39eadadd970b5196f208", - "reference": "42793a97c99a68adfe8e39eadadd970b5196f208", + "url": "https://api.github.com/repos/algolia/algoliasearch-client-php/zipball/cffa68164fa1af8a19e40aefe73d4861e7f5a66f", + "reference": "cffa68164fa1af8a19e40aefe73d4861e7f5a66f", "shasum": "" }, "require": { "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", - "php": "^7.2 || ^8.0", - "psr/http-message": "^1.0", - "psr/log": "^1.0", - "psr/simple-cache": "^1.0" + "guzzlehttp/psr7": "^2.0", + "php": ">=8.1 !=8.3.0", + "psr/http-message": "^1.1 || ^2.0", + "psr/log": "^1.0 || ^2.0 || ^3.0", + "psr/simple-cache": "^1.0 || ^2.0 || ^3.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.0", - "fzaninotto/faker": "^1.8", - "phpunit/phpunit": "^8.0 || ^9.0", - "symfony/yaml": "^2.0 || ^4.0" + "friendsofphp/php-cs-fixer": "^3.80.0", + "phpstan/phpstan": "^1.12", + "phpunit/phpunit": "^11.0", + "vlucas/phpdotenv": "^5.4" }, "suggest": { "guzzlehttp/guzzle": "If you prefer to use Guzzle HTTP client instead of the Http Client implementation provided by the package" }, - "bin": [ - "bin/algolia-doctor" - ], "type": "library", - "extra": { - "branch-alias": { - "dev-2.0": "2.0.x-dev" - } - }, "autoload": { - "psr-4": { - "Algolia\\AlgoliaSearch\\": "src/" - }, "files": [ - "src/Http/Psr7/functions.php", - "src/functions.php" - ] + "lib/Http/Psr7/functions.php" + ], + "psr-4": { + "Algolia\\AlgoliaSearch\\": "lib/" + } }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -63,10 +55,11 @@ "authors": [ { "name": "Algolia Team", - "email": "contact@algolia.com" + "homepage": "https://alg.li/support" } ], - "description": "Algolia Search API Client for PHP", + "description": "API powering the features of Algolia.", + "homepage": "https://github.com/algolia/algoliasearch-client-php", "keywords": [ "algolia", "api", @@ -76,30 +69,30 @@ ], "support": { "issues": "https://github.com/algolia/algoliasearch-client-php/issues", - "source": "https://github.com/algolia/algoliasearch-client-php/tree/3.1.0" + "source": "https://github.com/algolia/algoliasearch-client-php/tree/4.37.0" }, - "time": "2021-08-31T14:17:19+00:00" + "time": "2025-12-04T13:54:20+00:00" }, { "name": "graham-campbell/result-type", - "version": "v1.0.2", + "version": "v1.1.3", "source": { "type": "git", "url": "https://github.com/GrahamCampbell/Result-Type.git", - "reference": "84afea85c6841deeea872f36249a206e878a5de0" + "reference": "3ba905c11371512af9d9bdd27d99b782216b6945" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/GrahamCampbell/Result-Type/zipball/84afea85c6841deeea872f36249a206e878a5de0", - "reference": "84afea85c6841deeea872f36249a206e878a5de0", + "url": "https://api.github.com/repos/GrahamCampbell/Result-Type/zipball/3ba905c11371512af9d9bdd27d99b782216b6945", + "reference": "3ba905c11371512af9d9bdd27d99b782216b6945", "shasum": "" }, "require": { - "php": "^7.0 || ^8.0", - "phpoption/phpoption": "^1.8" + "php": "^7.2.5 || ^8.0", + "phpoption/phpoption": "^1.9.3" }, "require-dev": { - "phpunit/phpunit": "^6.5.14 || ^7.5.20 || ^8.5.19 || ^9.5.8" + "phpunit/phpunit": "^8.5.39 || ^9.6.20 || ^10.5.28" }, "type": "library", "autoload": { @@ -114,7 +107,8 @@ "authors": [ { "name": "Graham Campbell", - "email": "hello@gjcampbell.co.uk" + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" } ], "description": "An Implementation Of The Result Type", @@ -127,7 +121,7 @@ ], "support": { "issues": "https://github.com/GrahamCampbell/Result-Type/issues", - "source": "https://github.com/GrahamCampbell/Result-Type/tree/v1.0.2" + "source": "https://github.com/GrahamCampbell/Result-Type/tree/v1.1.3" }, "funding": [ { @@ -139,33 +133,153 @@ "type": "tidelift" } ], - "time": "2021-08-28T21:34:50+00:00" + "time": "2024-07-20T21:45:45+00:00" + }, + { + "name": "guzzlehttp/psr7", + "version": "2.8.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "21dc724a0583619cd1652f673303492272778051" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/21dc724a0583619cd1652f673303492272778051", + "reference": "21dc724a0583619cd1652f673303492272778051", + "shasum": "" + }, + "require": { + "php": "^7.2.5 || ^8.0", + "psr/http-factory": "^1.0", + "psr/http-message": "^1.1 || ^2.0", + "ralouphie/getallheaders": "^3.0" + }, + "provide": { + "psr/http-factory-implementation": "1.0", + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.8.2", + "http-interop/http-factory-tests": "0.9.0", + "phpunit/phpunit": "^8.5.44 || ^9.6.25" + }, + "suggest": { + "laminas/laminas-httphandlerrunner": "Emit PSR-7 responses" + }, + "type": "library", + "extra": { + "bamarni-bin": { + "bin-links": true, + "forward-command": false + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "George Mponos", + "email": "gmponos@gmail.com", + "homepage": "https://github.com/gmponos" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com", + "homepage": "https://github.com/sagikazarmark" + }, + { + "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", + "homepage": "https://github.com/Tobion" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com", + "homepage": "https://sagikazarmark.hu" + } + ], + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "psr-7", + "request", + "response", + "stream", + "uri", + "url" + ], + "support": { + "issues": "https://github.com/guzzle/psr7/issues", + "source": "https://github.com/guzzle/psr7/tree/2.8.0" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/psr7", + "type": "tidelift" + } + ], + "time": "2025-08-23T21:21:41+00:00" }, { "name": "phpoption/phpoption", - "version": "1.8.0", + "version": "1.9.4", "source": { "type": "git", "url": "https://github.com/schmittjoh/php-option.git", - "reference": "5455cb38aed4523f99977c4a12ef19da4bfe2a28" + "reference": "638a154f8d4ee6a5cfa96d6a34dfbe0cffa9566d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/schmittjoh/php-option/zipball/5455cb38aed4523f99977c4a12ef19da4bfe2a28", - "reference": "5455cb38aed4523f99977c4a12ef19da4bfe2a28", + "url": "https://api.github.com/repos/schmittjoh/php-option/zipball/638a154f8d4ee6a5cfa96d6a34dfbe0cffa9566d", + "reference": "638a154f8d4ee6a5cfa96d6a34dfbe0cffa9566d", "shasum": "" }, "require": { - "php": "^7.0 || ^8.0" + "php": "^7.2.5 || ^8.0" }, "require-dev": { - "bamarni/composer-bin-plugin": "^1.4.1", - "phpunit/phpunit": "^6.5.14 || ^7.0.20 || ^8.5.19 || ^9.5.8" + "bamarni/composer-bin-plugin": "^1.8.2", + "phpunit/phpunit": "^8.5.44 || ^9.6.25 || ^10.5.53 || ^11.5.34" }, "type": "library", "extra": { + "bamarni-bin": { + "bin-links": true, + "forward-command": false + }, "branch-alias": { - "dev-master": "1.8-dev" + "dev-master": "1.9-dev" } }, "autoload": { @@ -180,11 +294,13 @@ "authors": [ { "name": "Johannes M. Schmitt", - "email": "schmittjoh@gmail.com" + "email": "schmittjoh@gmail.com", + "homepage": "https://github.com/schmittjoh" }, { "name": "Graham Campbell", - "email": "hello@gjcampbell.co.uk" + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" } ], "description": "Option Type for PHP", @@ -196,7 +312,7 @@ ], "support": { "issues": "https://github.com/schmittjoh/php-option/issues", - "source": "https://github.com/schmittjoh/php-option/tree/1.8.0" + "source": "https://github.com/schmittjoh/php-option/tree/1.9.4" }, "funding": [ { @@ -208,29 +324,84 @@ "type": "tidelift" } ], - "time": "2021-08-28T21:27:29+00:00" + "time": "2025-08-21T11:53:16+00:00" + }, + { + "name": "psr/http-factory", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-factory.git", + "reference": "2b4765fddfe3b508ac62f829e852b1501d3f6e8a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-factory/zipball/2b4765fddfe3b508ac62f829e852b1501d3f6e8a", + "reference": "2b4765fddfe3b508ac62f829e852b1501d3f6e8a", + "shasum": "" + }, + "require": { + "php": ">=7.1", + "psr/http-message": "^1.0 || ^2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "PSR-17: Common interfaces for PSR-7 HTTP message factories", + "keywords": [ + "factory", + "http", + "message", + "psr", + "psr-17", + "psr-7", + "request", + "response" + ], + "support": { + "source": "https://github.com/php-fig/http-factory" + }, + "time": "2024-04-15T12:06:14+00:00" }, { "name": "psr/http-message", - "version": "1.0.1", + "version": "2.0", "source": { "type": "git", "url": "https://github.com/php-fig/http-message.git", - "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + "reference": "402d35bcb92c70c026d1a6a9883f06b2ead23d71" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", - "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/402d35bcb92c70c026d1a6a9883f06b2ead23d71", + "reference": "402d35bcb92c70c026d1a6a9883f06b2ead23d71", "shasum": "" }, "require": { - "php": ">=5.3.0" + "php": "^7.2 || ^8.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "2.0.x-dev" } }, "autoload": { @@ -245,7 +416,7 @@ "authors": [ { "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" + "homepage": "https://www.php-fig.org/" } ], "description": "Common interface for HTTP messages", @@ -259,36 +430,36 @@ "response" ], "support": { - "source": "https://github.com/php-fig/http-message/tree/master" + "source": "https://github.com/php-fig/http-message/tree/2.0" }, - "time": "2016-08-06T14:39:51+00:00" + "time": "2023-04-04T09:54:51+00:00" }, { "name": "psr/log", - "version": "1.1.4", + "version": "3.0.2", "source": { "type": "git", "url": "https://github.com/php-fig/log.git", - "reference": "d49695b909c3b7628b6289db5479a1c204601f11" + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/log/zipball/d49695b909c3b7628b6289db5479a1c204601f11", - "reference": "d49695b909c3b7628b6289db5479a1c204601f11", + "url": "https://api.github.com/repos/php-fig/log/zipball/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", "shasum": "" }, "require": { - "php": ">=5.3.0" + "php": ">=8.0.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.1.x-dev" + "dev-master": "3.x-dev" } }, "autoload": { "psr-4": { - "Psr\\Log\\": "Psr/Log/" + "Psr\\Log\\": "src" } }, "notification-url": "https://packagist.org/downloads/", @@ -309,31 +480,31 @@ "psr-3" ], "support": { - "source": "https://github.com/php-fig/log/tree/1.1.4" + "source": "https://github.com/php-fig/log/tree/3.0.2" }, - "time": "2021-05-03T11:20:27+00:00" + "time": "2024-09-11T13:17:53+00:00" }, { "name": "psr/simple-cache", - "version": "1.0.1", + "version": "3.0.0", "source": { "type": "git", "url": "https://github.com/php-fig/simple-cache.git", - "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b" + "reference": "764e0b3939f5ca87cb904f570ef9be2d78a07865" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", - "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/764e0b3939f5ca87cb904f570ef9be2d78a07865", + "reference": "764e0b3939f5ca87cb904f570ef9be2d78a07865", "shasum": "" }, "require": { - "php": ">=5.3.0" + "php": ">=8.0.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "3.0.x-dev" } }, "autoload": { @@ -348,7 +519,7 @@ "authors": [ { "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" + "homepage": "https://www.php-fig.org/" } ], "description": "Common interfaces for simple caching", @@ -360,47 +531,91 @@ "simple-cache" ], "support": { - "source": "https://github.com/php-fig/simple-cache/tree/master" + "source": "https://github.com/php-fig/simple-cache/tree/3.0.0" + }, + "time": "2021-10-29T13:26:27+00:00" + }, + { + "name": "ralouphie/getallheaders", + "version": "3.0.3", + "source": { + "type": "git", + "url": "https://github.com/ralouphie/getallheaders.git", + "reference": "120b605dfeb996808c31b6477290a714d356e822" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822", + "reference": "120b605dfeb996808c31b6477290a714d356e822", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "require-dev": { + "php-coveralls/php-coveralls": "^2.1", + "phpunit/phpunit": "^5 || ^6.5" + }, + "type": "library", + "autoload": { + "files": [ + "src/getallheaders.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ralph Khattar", + "email": "ralph.khattar@gmail.com" + } + ], + "description": "A polyfill for getallheaders.", + "support": { + "issues": "https://github.com/ralouphie/getallheaders/issues", + "source": "https://github.com/ralouphie/getallheaders/tree/develop" }, - "time": "2017-10-23T01:57:42+00:00" + "time": "2019-03-08T08:55:37+00:00" }, { "name": "symfony/polyfill-ctype", - "version": "v1.23.0", + "version": "v1.33.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "46cd95797e9df938fdd2b03693b5fca5e64b01ce" + "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/46cd95797e9df938fdd2b03693b5fca5e64b01ce", - "reference": "46cd95797e9df938fdd2b03693b5fca5e64b01ce", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/a3cc8b044a6ea513310cbd48ef7333b384945638", + "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" + }, + "provide": { + "ext-ctype": "*" }, "suggest": { "ext-ctype": "For best performance" }, "type": "library", "extra": { - "branch-alias": { - "dev-main": "1.23-dev" - }, "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" } }, "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Ctype\\": "" - }, "files": [ "bootstrap.php" - ] + ], + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + } }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -425,7 +640,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.23.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.33.0" }, "funding": [ { @@ -436,50 +651,55 @@ "url": "https://github.com/fabpot", "type": "github" }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, { "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", "type": "tidelift" } ], - "time": "2021-02-19T12:13:01+00:00" + "time": "2024-09-09T11:45:10+00:00" }, { "name": "symfony/polyfill-mbstring", - "version": "v1.23.1", + "version": "v1.33.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "9174a3d80210dca8daa7f31fec659150bbeabfc6" + "reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/9174a3d80210dca8daa7f31fec659150bbeabfc6", - "reference": "9174a3d80210dca8daa7f31fec659150bbeabfc6", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6d857f4d76bd4b343eac26d6b539585d2bc56493", + "reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493", "shasum": "" }, "require": { - "php": ">=7.1" + "ext-iconv": "*", + "php": ">=7.2" + }, + "provide": { + "ext-mbstring": "*" }, "suggest": { "ext-mbstring": "For best performance" }, "type": "library", "extra": { - "branch-alias": { - "dev-main": "1.23-dev" - }, "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" } }, "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Mbstring\\": "" - }, "files": [ "bootstrap.php" - ] + ], + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + } }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -505,7 +725,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.23.1" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.33.0" }, "funding": [ { @@ -516,47 +736,48 @@ "url": "https://github.com/fabpot", "type": "github" }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, { "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", "type": "tidelift" } ], - "time": "2021-05-27T12:26:48+00:00" + "time": "2024-12-23T08:48:59+00:00" }, { "name": "symfony/polyfill-php80", - "version": "v1.23.1", + "version": "v1.33.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php80.git", - "reference": "1100343ed1a92e3a38f9ae122fc0eb21602547be" + "reference": "0cc9dd0f17f61d8131e7df6b84bd344899fe2608" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/1100343ed1a92e3a38f9ae122fc0eb21602547be", - "reference": "1100343ed1a92e3a38f9ae122fc0eb21602547be", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/0cc9dd0f17f61d8131e7df6b84bd344899fe2608", + "reference": "0cc9dd0f17f61d8131e7df6b84bd344899fe2608", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" }, "type": "library", "extra": { - "branch-alias": { - "dev-main": "1.23-dev" - }, "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" } }, "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Php80\\": "" - }, "files": [ "bootstrap.php" ], + "psr-4": { + "Symfony\\Polyfill\\Php80\\": "" + }, "classmap": [ "Resources/stubs" ] @@ -588,7 +809,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php80/tree/v1.23.1" + "source": "https://github.com/symfony/polyfill-php80/tree/v1.33.0" }, "funding": [ { @@ -599,48 +820,56 @@ "url": "https://github.com/fabpot", "type": "github" }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, { "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", "type": "tidelift" } ], - "time": "2021-07-28T13:41:28+00:00" + "time": "2025-01-02T08:10:11+00:00" }, { "name": "vlucas/phpdotenv", - "version": "v5.3.0", + "version": "v5.6.2", "source": { "type": "git", "url": "https://github.com/vlucas/phpdotenv.git", - "reference": "b3eac5c7ac896e52deab4a99068e3f4ab12d9e56" + "reference": "24ac4c74f91ee2c193fa1aaa5c249cb0822809af" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/b3eac5c7ac896e52deab4a99068e3f4ab12d9e56", - "reference": "b3eac5c7ac896e52deab4a99068e3f4ab12d9e56", + "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/24ac4c74f91ee2c193fa1aaa5c249cb0822809af", + "reference": "24ac4c74f91ee2c193fa1aaa5c249cb0822809af", "shasum": "" }, "require": { "ext-pcre": "*", - "graham-campbell/result-type": "^1.0.1", - "php": "^7.1.3 || ^8.0", - "phpoption/phpoption": "^1.7.4", - "symfony/polyfill-ctype": "^1.17", - "symfony/polyfill-mbstring": "^1.17", - "symfony/polyfill-php80": "^1.17" + "graham-campbell/result-type": "^1.1.3", + "php": "^7.2.5 || ^8.0", + "phpoption/phpoption": "^1.9.3", + "symfony/polyfill-ctype": "^1.24", + "symfony/polyfill-mbstring": "^1.24", + "symfony/polyfill-php80": "^1.24" }, "require-dev": { - "bamarni/composer-bin-plugin": "^1.4.1", + "bamarni/composer-bin-plugin": "^1.8.2", "ext-filter": "*", - "phpunit/phpunit": "^7.5.20 || ^8.5.14 || ^9.5.1" + "phpunit/phpunit": "^8.5.34 || ^9.6.13 || ^10.4.2" }, "suggest": { "ext-filter": "Required to use the boolean validator." }, "type": "library", "extra": { + "bamarni-bin": { + "bin-links": true, + "forward-command": false + }, "branch-alias": { - "dev-master": "5.3-dev" + "dev-master": "5.6-dev" } }, "autoload": { @@ -655,13 +884,13 @@ "authors": [ { "name": "Graham Campbell", - "email": "graham@alt-three.com", - "homepage": "https://gjcampbell.co.uk/" + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" }, { "name": "Vance Lucas", "email": "vance@vancelucas.com", - "homepage": "https://vancelucas.com/" + "homepage": "https://github.com/vlucas" } ], "description": "Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.", @@ -672,7 +901,7 @@ ], "support": { "issues": "https://github.com/vlucas/phpdotenv/issues", - "source": "https://github.com/vlucas/phpdotenv/tree/v5.3.0" + "source": "https://github.com/vlucas/phpdotenv/tree/v5.6.2" }, "funding": [ { @@ -684,18 +913,18 @@ "type": "tidelift" } ], - "time": "2021-01-20T15:23:13+00:00" + "time": "2025-04-30T23:37:27+00:00" } ], "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": true, "prefer-lowest": false, "platform": { "php": "^7.2 || ^8.0" }, - "platform-dev": [], - "plugin-api-version": "2.1.0" + "platform-dev": {}, + "plugin-api-version": "2.9.0" } diff --git a/php/generate_key.php b/php/generate_key.php index 153bcca..6d11f61 100644 --- a/php/generate_key.php +++ b/php/generate_key.php @@ -5,37 +5,44 @@ #The generated key will be valid for Search operations, and will be limited to 100 queries per hour. # Install the API client: https://www.algolia.com/doc/api-client/getting-started/install/php/?client=php -require __DIR__ . '/vendor/autoload.php'; +require __DIR__.'/vendor/autoload.php'; + +use Algolia\AlgoliaSearch\Api\SearchClient; $dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->load(); -# Get your Algolia Application ID and (admin) API key from the dashboard: https://www.algolia.com/account/api-keys -# and choose a name for your index. Add these environment variables to a `.env` file: +# Algolia client credentials $ALGOLIA_APP_ID = $_ENV['ALGOLIA_APP_ID']; $ALGOLIA_API_KEY = $_ENV['ALGOLIA_API_KEY']; $ALGOLIA_INDEX_NAME = $_ENV['ALGOLIA_INDEX_NAME']; -# Start the API client +# Initialize the client # https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/ -$client = \Algolia\AlgoliaSearch\SearchClient::create($ALGOLIA_APP_ID, $ALGOLIA_API_KEY); +$client = SearchClient::create($ALGOLIA_APP_ID, $ALGOLIA_API_KEY); # Set permissions for API key # https://www.algolia.com/doc/api-reference/api-methods/add-api-key/#method-param-acl -$acl = ["search"]; -// Set the parameters for API key -// https://www.algolia.com/doc/api-reference/api-methods/add-api-key/#method-param-maxqueriesperipperhour +//Set the parameters for API key +//https://www.algolia.com/doc/rest-api/search/add-api-key -$params = [ - 'description' => 'Restricted search-only API key for algolia.com', - // Rate-limit to 100 requests per hour per IP address - 'maxQueriesPerIPPerHour' => 100 -]; +$acl = [ + 'acl' => [ + 'search', + + 'addObject', + ], + 'description' => 'Restricted search-only API key for algolia.com', + 'maxQueriesPerIPPerHour' => 100 + ]; # Create a new restricted search-only API key print("Creating new key...\n"); -$res = $client->addApiKey($acl, $params)->wait(); + +$res = $client->addApiKey($acl); + +$client->waitForTask($ALGOLIA_INDEX_NAME, $res['key']); $new_key = $res['key']; @@ -45,18 +52,30 @@ echo "Error while creating key\n"; } +//Wait for the API key to be created +$res = $client->waitForApiKey( + $new_key, + 'add', +); + # Test the created key print("Testing key...\n"); # Initialise a new client with the generated key -$client = \Algolia\AlgoliaSearch\SearchClient::create($ALGOLIA_APP_ID, $new_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); +$newClient = SearchClient::create($ALGOLIA_APP_ID, $new_key); # Test the new generated key by performing a search -if ($search_res = $index->search('')) { + +$searchQuery = [ + 'requests' => [ + ['indexName' => $ALGOLIA_INDEX_NAME, + 'query' => '', + 'hitsPerPage' => 50, + ], + ], +]; + +if ($search_res = $newClient->search($searchQuery)) { echo "Successful key test\n"; } else { echo "Failed search with the new key\n"; diff --git a/php/indexing.php b/php/indexing.php index c1c911d..3a2fde3 100644 --- a/php/indexing.php +++ b/php/indexing.php @@ -2,6 +2,8 @@ require __DIR__.'/vendor/autoload.php'; +use Algolia\AlgoliaSearch\Api\SearchClient; + $dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->load(); @@ -12,11 +14,7 @@ # Initialize the client # https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/ -$client = \Algolia\AlgoliaSearch\SearchClient::create($ALGOLIA_APP_ID, $ALGOLIA_API_KEY); - -# Initialize an index -# 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); # Define some objects to add to our index # https://www.algolia.com/doc/api-client/methods/indexing/#object-and-record @@ -32,20 +30,40 @@ ]; # We don't have any objects (yet) in our index -$res = $index->search(''); +$res = $client->search( + ['requests' => [ + ['indexName' => $ALGOLIA_INDEX_NAME, + 'query' => '', + 'hitsPerPage' => 50, + ], + ], + ], +); + print('Current objects: '); -print_r($res['hits']); +print_r($res['results'][0]['hits']); print("\n"); # Save Objects: Add mutliple new objects to an index. # https://www.algolia.com/doc/api-reference/api-methods/add-objects/?client=php print('Save Objects - Adding multiple objects: '); print_r($contacts); -$index->saveObjects($contacts)->wait(); +$res = $client->saveObjects($ALGOLIA_INDEX_NAME, $contacts); + +$client->waitForTask($ALGOLIA_INDEX_NAME, $res['taskID']); + +$res = $client->search( + ['requests' => [ + ['indexName' => $ALGOLIA_INDEX_NAME, + 'query' => '', + 'hitsPerPage' => 50, + ], + ], + ], +); -$res = $index->search(''); print('Current objects: '); -print_r($res['hits']); +print_r($res['results'][0]['hits']); print("\n"); # Save Objects: Replace an existing object with an updated set of attributes. @@ -56,11 +74,22 @@ 'name' => 'FooBar', 'objectID' => '1' ]; -$index->saveObject($new_contact)->wait(); +$res = $client->saveObject($ALGOLIA_INDEX_NAME, $new_contact); + +$client->waitForTask($ALGOLIA_INDEX_NAME, $res['taskID']); + +$res = $client->search( + ['requests' => [ + ['indexName' => $ALGOLIA_INDEX_NAME, + 'query' => '', + 'hitsPerPage' => 50, + ], + ], + ], +); -$res = $index->search(''); print('Current objects: '); -print_r($res['hits']); +print_r($res['results'][0]['hits']); print("\n"); # Partial Update Objects: Update one or more attributes of an existing object. @@ -68,25 +97,46 @@ print('Save Objects - Updating object’s attributes on: '); print_r($contacts[0]); $new_contact = [ - 'email' => 'foo@bar.com', # New attribute - 'objectID' => '1' + 'email' => 'foo@bar.com' # New attribute ]; -$index->partialUpdateObject($new_contact)->wait(); +$res = $client->partialUpdateObject($ALGOLIA_INDEX_NAME, '1', $new_contact); + +$client->waitForTask($ALGOLIA_INDEX_NAME, $res['taskID']); + +$res = $client->search( + ['requests' => [ + ['indexName' => $ALGOLIA_INDEX_NAME, + 'query' => '', + 'hitsPerPage' => 50, + ], + ], + ], +); -$res = $index->search(''); print('Current objects: '); -print_r($res['hits']); +print_r($res['results'][0]['hits']); print("\n"); # Delete Objects: Remove objects from an index using their objectID. # https://www.algolia.com/doc/api-reference/api-methods/delete-objects/?client=php $objectID_to_delete = $contacts[0]["objectID"]; printf('Delete Objects - Deleting object with objectID "%s"', $objectID_to_delete); -$index->deleteObject($objectID_to_delete)->wait(); +$res = $client->deleteObject($ALGOLIA_INDEX_NAME, $objectID_to_delete); + +$client->waitForTask($ALGOLIA_INDEX_NAME, $res['taskID']); + +$res = $client->search( + ['requests' => [ + ['indexName' => $ALGOLIA_INDEX_NAME, + 'query' => '', + 'hitsPerPage' => 50, + ], + ], + ], +); -$res = $index->search(''); print('Current objects: '); -print_r($res['hits']); +print_r($res['results'][0]['hits']); print("\n"); # Replace All Objects: Clears all objects from your index and replaces them with a new set of objects. @@ -103,11 +153,22 @@ ]; print('Replace All Objects - Clears all objects and replaces them with: '); print_r($new_contacts); -$index->replaceAllObjects($new_contacts)->wait(); +$res = $client->replaceAllObjects($ALGOLIA_INDEX_NAME, $new_contacts); + +$client->waitForTask($ALGOLIA_INDEX_NAME, $res['taskID']); + +$res = $client->search( + ['requests' => [ + ['indexName' => $ALGOLIA_INDEX_NAME, + 'query' => '', + 'hitsPerPage' => 50, + ], + ], + ], +); -$res = $index->search(''); print('Current objects: '); -print_r($res['hits']); +print_r($res['results'][0]['hits']); print("\n"); # Delete By: Remove all objects matching a filter (including geo filters). @@ -116,17 +177,32 @@ # Firstly, have an attribute to filter on # https://www.algolia.com/doc/api-client/methods/settings/?client=php -$index->setSettings([ +$res = $client->setSettings($ALGOLIA_INDEX_NAME, +[ 'attributesForFaceting' => ['name'] -])->wait(); +]); + +$client->waitForTask($ALGOLIA_INDEX_NAME, $res['taskID']); -$index->deleteBy([ +$res = $client->deleteBy($ALGOLIA_INDEX_NAME, +[ 'facetFilters' => ['name:NewBar'] # https://www.algolia.com/doc/api-reference/api-parameters/facetFilters/ -])->wait(); +]); + +$client->waitForTask($ALGOLIA_INDEX_NAME, $res['taskID']); + +$res = $client->search( + ['requests' => [ + ['indexName' => $ALGOLIA_INDEX_NAME, + 'query' => '', + 'hitsPerPage' => 50, + ], + ], + ], +); -$res = $index->search(''); print('Current objects: '); -print_r($res['hits']); +print_r($res['results'][0]['hits']); print("\n"); # Get Objects: Get one or more objects using their objectIDs. @@ -134,46 +210,61 @@ $object_id = $new_contacts[0]['objectID']; printf('Get Objects - Getting object with objectID "%s"', $object_id); -$res = $index->getObject($object_id); +$res = $client->getObject($ALGOLIA_INDEX_NAME, $object_id); print('Results: '); print_r($res); print("\n"); # Custom Batch: Perform several indexing operations in one API call. # https://www.algolia.com/doc/api-reference/api-methods/batch/?client=php -$operations = [ - [ - 'action' => 'addObject', - 'indexName' => $ALGOLIA_INDEX_NAME, - 'body' => [ - 'name' => 'BatchedBar', - ] - ], - [ - 'action' => 'updateObject', - 'indexName' => $ALGOLIA_INDEX_NAME, - 'body' => [ - 'objectID' => $object_id, - 'name' => 'NewBatchedBar', - ] - ] -]; print('Custom Batch - Batching the operations: '); -print_r( $operations); -$res = $client->multipleBatch($operations)->wait(); -$res = $index->search(''); +$res = $client->multipleBatch( + ['requests' => [ + ['action' => 'addObject', + 'body' => [ + 'name' => 'BatchedBar', + ], + 'indexName' => $ALGOLIA_INDEX_NAME, + ], + ], + ], +); + +$client->waitForTask($ALGOLIA_INDEX_NAME, $res['taskID'][$ALGOLIA_INDEX_NAME]); + +$res = $client->search( + ['requests' => [ + ['indexName' => $ALGOLIA_INDEX_NAME, + 'query' => '', + 'hitsPerPage' => 50, + ], + ], + ], +); + print('Current objects: '); -print_r($res['hits']); +print_r($res['results'][0]['hits']); print("\n"); # Clear Objects: Clear the records of an index without affecting its settings. # https://www.algolia.com/doc/api-reference/api-methods/clear-objects/?client=php print_r("Clear Objects: Clear the records of an index without affecting its settings.\n"); -$index->clearObjects()->wait(); +$res = $client->clearObjects($ALGOLIA_INDEX_NAME); + +$client->waitForTask($ALGOLIA_INDEX_NAME, $res['taskID']); # We don't have any objects in our index -$res = $index->search(''); +$res = $client->search( + ['requests' => [ + ['indexName' => $ALGOLIA_INDEX_NAME, + 'query' => '', + 'hitsPerPage' => 50, + ], + ], + ], +); + print('Current objects: '); -print_r($res['hits']); \ No newline at end of file +print_r($res['results'][0]['hits']); \ No newline at end of file diff --git a/php/rest_api_return_top_hits.php b/php/rest_api_return_top_hits.php deleted file mode 100644 index 820a607..0000000 --- a/php/rest_api_return_top_hits.php +++ /dev/null @@ -1,59 +0,0 @@ -load(); - -# Get your Algolia Application ID and (admin) API key from the dashboard: https://www.algolia.com/account/api-keys -# and choose a name for your index. Add these environment variables to a `.env` file: -$ALGOLIA_APP_ID = $_ENV['ALGOLIA_APP_ID']; -$ALGOLIA_API_KEY = $_ENV['ALGOLIA_API_KEY']; -$ALGOLIA_INDEX_NAME = $_ENV['ALGOLIA_INDEX_NAME']; - -# The Analytics API can be reached from multiple domains, each specific to a region. -# You should use the domain that matches the region where your analytics data is stored and processed. -# View your analytics region: https://www.algolia.com/infra/analytics -# The following domains are available: -# United States: https://analytics.us.algolia.com -# Europe (Germany): https://analytics.de.algolia.com - -$URL_DOMAIN = $_ENV['URL_DOMAIN']; - -$url = "$URL_DOMAIN/2/searches?index=$ALGOLIA_INDEX_NAME&limit=1000"; - -# Describing HTTP request -$request_options = [ - 'http' => [ - 'method' => 'GET', - 'header' => join("\r\n", [ - "X-Algolia-Application-Id: $ALGOLIA_APP_ID", - "X-Algolia-API-Key: $ALGOLIA_API_KEY", - ]), - ] -]; - -# Sending HTTP request -$result = file_get_contents($url, false, stream_context_create($request_options)); - -if ($result === FALSE) { - exit("Failed HTTP request"); -} - -# Returning 1000 Top Searches -print("1000 Top Searches:\n"); -var_dump($result); - -# Format the resulting json string -$json = json_decode($result); -$formatted_result = json_encode($json, JSON_PRETTY_PRINT); - -# Write json to file -if (file_put_contents("{$ALGOLIA_INDEX_NAME}_top_1000_searches.json", $formatted_result)) - echo "JSON file created successfully...\n"; -else - echo "Oops! Error creating json file...\n"; diff --git a/php/restore.php b/php/restore.php index 237804d..04618ac 100644 --- a/php/restore.php +++ b/php/restore.php @@ -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(); @@ -13,12 +15,8 @@ $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); # Restoring all records with replace all objects method # https://www.algolia.com/doc/api-reference/api-methods/replace-all-objects/ @@ -29,11 +27,8 @@ var_dump($jsonRecords); print("\n"); -# Decode json file -$arrayRecords = json_decode($jsonRecords, true); - # Restore Records -$index->replaceAllObjects($arrayRecords); +$client->replaceAllObjects($ALGOLIA_INDEX_NAME, $jsonRecords); print("Records restored\n"); # Restoring settings with set settings method @@ -49,7 +44,7 @@ $settings = json_decode($jsonSettings, true); # Restore settings -$index->setSettings($settings); +$client->setSettings($ALGOLIA_INDEX_NAME, $settings); print("Settings restored\n"); # Restoring Rules with replace all rules method @@ -58,14 +53,14 @@ # Read json file $jsonRules = file_get_contents("{$ALGOLIA_INDEX_NAME}_rules.json"); print("Rules:\n"); -var_dump($jsonRules); +print_r($jsonRules); print("\n"); # Decode json file $arrayRules = json_decode($jsonRules, true); # Restore Rules -$index->replaceAllRules($arrayRules); +$client->saveRules($ALGOLIA_INDEX_NAME, $arrayRules); print("Rules restored\n"); @@ -82,5 +77,5 @@ $arraySynonyms = json_decode($jsonSynonyms, true); # Restore Synonyms -$index->replaceAllSynonyms($arraySynonyms); +$client->saveSynonyms($ALGOLIA_INDEX_NAME, $arraySynonyms); print("Synonyms restored\n"); \ No newline at end of file diff --git a/php/return_top_hits.php b/php/return_top_hits.php new file mode 100644 index 0000000..03814e7 --- /dev/null +++ b/php/return_top_hits.php @@ -0,0 +1,38 @@ +load(); + +# Algolia client credentials +$ALGOLIA_APP_ID = $_ENV['ALGOLIA_APP_ID']; +$ALGOLIA_API_KEY = $_ENV['ALGOLIA_API_KEY']; +$ALGOLIA_INDEX_NAME = $_ENV['ALGOLIA_INDEX_NAME']; + +# Get application region +# https://www.algolia.com/doc/libraries/sdk/methods/analytics#php + +$ALGOLIA_APPLICATION_REGION = "us"; + +$client = AnalyticsClient::create($ALGOLIA_APP_ID, $ALGOLIA_API_KEY, $ALGOLIA_APPLICATION_REGION); + +$response = $client->getTopSearches($ALGOLIA_INDEX_NAME); + +# Returning 1000 Top Searches +print("1000 Top Searches:\n"); +var_dump($response); + +# Format the resulting json string +$formatted_result = json_encode($response, JSON_PRETTY_PRINT); + +# Write json to file +if (file_put_contents("{$ALGOLIA_INDEX_NAME}_top_1000_searches.json", $formatted_result)) + echo "JSON file created successfully...\n"; +else + echo "Oops! Error creating json file...\n"; diff --git a/php/rules.php b/php/rules.php index 552488f..cc41ad1 100644 --- a/php/rules.php +++ b/php/rules.php @@ -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(); @@ -14,16 +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); # Exporting the rules # https://www.algolia.com/doc/api-reference/api-methods/export-rules/#examples print("Original rules:\n"); -$iterator = $index->browseRules(); +$iterator = $client->browseRules($ALGOLIA_INDEX_NAME); foreach ($iterator as $rule) { var_dump($rule); } @@ -46,15 +44,22 @@ )) ) ); -$index->saveRule($rule)->wait(); +$res = $client->saveRule($ALGOLIA_INDEX_NAME, $object_id, $rule); + +$client->waitForTask($ALGOLIA_INDEX_NAME, $res['taskID']); # Exporting the modified rules # https://www.algolia.com/doc/api-reference/api-methods/export-rules/#examples print("Modified rules:\n"); -$iterator = $index->browseRules(); -foreach ($iterator as $rule) { - var_dump($rule); -} +$query = [ + 'query' => 'flower', +]; + +$res = $client->searchRules( + $ALGOLIA_INDEX_NAME, + $query, +); +print_r($res); print("\n"); ?> diff --git a/php/simple.php b/php/simple.php index 34f1de0..56d8860 100644 --- a/php/simple.php +++ b/php/simple.php @@ -1,7 +1,9 @@ load(); @@ -14,22 +16,34 @@ # 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); # Add new objects to the index # https://www.algolia.com/doc/api-reference/api-methods/add-objects/ -$newObject = ['objectID' => 1, 'name' => 'Foo']; -$res = $index->saveObjects([$newObject]); - -# Wait for the indexing task to complete -# https://www.algolia.com/doc/api-reference/api-methods/wait-task/ -$res->wait(); - -# Search the index for "Fo" -# https://www.algolia.com/doc/api-reference/api-methods/search/ -$objects = $index->search('Fo'); -print_r($objects); +// Add a new record to your Algolia index +// Edit the JSON object based on your record schema +$response = $client->saveObject( + $ALGOLIA_INDEX_NAME, + ['objectID' => '1', + 'name' => 'foo', + ], +); + +var_dump($response); + +// Poll the task status to know when it has been indexed +$client->waitForTask($ALGOLIA_INDEX_NAME, $response['taskID']); + +// Fetch search results, with typo tolerance +$response = $client->search( + ['requests' => [ + ['indexName' => $ALGOLIA_INDEX_NAME, + 'query' => 'foo', + 'hitsPerPage' => 50, + ], + ], + ], +); + +// play with the response +var_dump($response);