Places - Authenticate using PHP and Curl

function httpPost( $url, $data )
{
 $curl = curl_init( $url );
 curl_setopt( $curl, CURLOPT_POST, true );
 curl_setopt( $curl, CURLOPT_POSTFIELDS, $data );
 curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
 $headers =  array(
   'X-Algolia-Application-Id: '.AL_APP_ID,
   'X-Algolia-API-Key: '.AL_APP_KEY
);
 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
doDebug('curl-headers',$headers);
 $response = curl_exec( $curl );
 curl_close( $curl );
 return $response;
 }

I am using the code above to try and access the Places using the following URL
https://places-dsn.algolia.net/1/places/query

Which works fine unless I try and authenticate with the Application ID (plSIJRKTUUU7) and Search Key from my account at which point I get

Invalid Application-ID or API key

I suspect I am missing something obvious on the documentation and I am very new to algolia.

Hey @janetaubman,

We recommend you use our API Clients instead of calling the API directly.

Regarding Algolia Places, and search operations in general, we recommend you use our frontend libraries to avoid the additional roundtrip to your server.

Is there a special reason you do not want to use: https://community.algolia.com/places/documentation.html ?

I can not use Javascript in the context I am using the API. We need to go via our server as more than just the lookup is going on.

The PHP client just looks like a huge overhead, just to get something I already have working put in place. it currently works fine directly other than the authentication routine.

Hi @janetaubman,

The API Client comes with a re-try strategy cooked in, which would be valuable in case our main domain server is un-reachable.

Please note that if you are not using our API Client, the SLA does not apply.

I highly recommend you use the API Client for now. In the future we might want to release a separate client for places indeed.

Please can you point me to the example of doing a Place search using the PHP client. Thank you.

I think this is the method you are looking for: https://github.com/algolia/algoliasearch-client-php/blob/master/src/AlgoliaSearch/PlacesIndex.php#L60

I worked out that I need the search function, but I am struggling to work out the connection to call.

I have
require_once(‘algoliasearch-client-php-master/algoliasearch.php’);
$client = new \AlgoliaSearch\Client(AL_APP_ID, AL_APP_KEY));

Which fails as it fails to connect to the DNS for my APP_ID of plSIJRKTUUU7

Can you provide the requires I need to do prior and the call I need to use?

I finally found the primary Project did not seem to be working properly even with the PHP client.

I have now got to a point where I can get a Client object created, but still can not work out how to tell the search to use Places.

This is what I have currently

 require_once('algoliasearch-client-php-master/algoliasearch.php');

$client = new \AlgoliaSearch\Client("3EHUGBEXI1", "xxxx Master key xxxxx");
var_dump($client);
$places = new \AlgoliaSearch\PlacesIndex($client->context);

The last line does not work giving the following

Fatal error: Cannot access protected property AlgoliaSearch\Client::$context in /var/sites/c/calico-pie.co.uk/public_html/srv/algolia.php on line 7

and I do need to get this working, despite lots of google searching and reviewing the documentation I just can not see how to get the PHP API to talk to Places,

Do I need to set an option when I create the Client Object?

After more digging in the code I have now got

$client = new \AlgoliaSearch\Client("3EHUGBEXI1", "xxx");

$places = $client->initPlaces("3EHUGBEXI1", "xxx");
$text = 'Wellington, Somerset';

$data = json_encode(array('query' => $text,'language' => 'GB','hitsPerPage' => 3));

$result = $places->search($data);
var_dump($result);

The first section appears to work OK and I can get a list of tables back.

How ever when I use the second one, I immediately get

Uncaught exception ‘AlgoliaSearch\AlgoliaException’ with message ‘Invalid Application-ID or API key’

After more digging in the code I have now got

$client = new \AlgoliaSearch\Client("3EHUGBEXI1", "xxx");

$places = $client->initPlaces(‘plSIJRKTUUU7’, “yyy”);
$text = ‘Wellington, Somerset’;

$data = json_encode(array('query' => $text,'language' => 'GB','hitsPerPage' => 3));

$result = $places->search($data);
var_dump($result);

The first section appears to work OK and I can get a list of tables back.

How ever when I use the second one, I immediately get

Uncaught exception ‘AlgoliaSearch\AlgoliaException’ with message ‘Invalid Application-ID or API key’

I have also been trying the JS client. but I can’t get that to log searches against my dashboard.

I am using

var placesAutocomplete = places({
container: document.querySelector(’#address-input’),
apiId: ‘plSIJRKTUUU7’,
apiKey: ‘yyy’
});

The place complete works, but no logging occurs.

Here is the correct way to use Algolia Places with the PHP API Client. Note that you’ll need a registered account (Places-specific appId and apiKeys):

$appId = 'YOUR_PLACES_APP_ID';
$apiKey = 'YOUR_PLACES_API_KEY';

$client = new \AlgoliaSearch\Client($appId, $apiKey);
$places = $client->initPlaces($appId, $apiKey);

$result = $places->search('Paris');
var_dump($result);

Tested with @janetaubman and it works :slight_smile:

1 Like

Thanks very much for your help with this.

Just to mention the search can take a second parameter as an array of options so you can select for example the language and the number of results you want

Hi @janetaubman,

FYI you can follow the PR allowing you to use the Places client without credentials here:


You can also register for free here to get up to 100k req / month:

And to get more info on the pure REST API of Places, it’s here:
https://community.algolia.com/places/rest.html

Enjoy

1 Like