algolia/algoliasearch-client-php

Error in Quick Start Code: Incorrect Namespace and Method

manuelricci opened this issue ยท 2 comments

Hi,

I'm not sure if this is the right place to report this issue. If it's not, please feel free to close this issue and let me know where I should post it.

In the code snippet provided in the "Get Started" section to start using Algolia, there are some errors:

<?php
require(__DIR__."/vendor/autoload.php");

use Algolia\AlgoliaSearch\SearchClient; 

$url = "https://dashboard.algolia.com/sample_datasets/movie.json";
$response = file_get_contents($url);
$records = json_decode($response, true);

$client = SearchClient::create("ID4NV3D7MX", "xxx");
$index = $client->initIndex("your_index_name"); 

$index->saveObjects($records, [ 'autoGenerateObjectIDIfNotExist' => true ]);

use Algolia\AlgoliaSearch\SearchClient will throw an error because SearchClient doesn't exist in that namespace. From v4 onwards, the correct namespace is Algolia\AlgoliaSearch\Api\SearchClient.

Furthermore, initIndex is no longer available in the new class, so it must be removed.

Maybe is better to show the Get Started example available in the PHP docs.

Have a nice day

Hey @manuelricci ๐Ÿ‘‹๐Ÿป

Thank you very much for your report! We are in the process of updating our website to reflect these changes.
We will close this issue when it's done.

Btw, I took the liberty to remove your admin API key from the code snippet you shared, but it's still available in the issue history.
I'd suggest you go to your account and rotate it from the settings page! ๐Ÿ˜

Thanks

Ops! Thank you very much for the edit and for the quick reply.

Have a good day