algolia / places

:globe_with_meridians: Turn any <input> into an address autocomplete

Home Page:https://community.algolia.com/places/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

getting data with CURL (PHP) returns different from the example results with places.js

pratamishus opened this issue · comments

BUG: I am trying to get places search with Algolia but with my server. I tried to reproduce the search exactly as it does the provided example (https://community.algolia.com/places/examples.html - the first simple input), but the system returns a little different result. In particular - if you enter Paris in the input you will see that first it shows the Paris city, and then other results, which is a correct way. But when I do the very same request with CURL it returns Paris 17e Arrondissement, then Paris 8e Arrondissement and others. The actual Paris city appears only as the 5th result.

$cURLConnection = curl_init('https://places-dsn.algolia.net/1/places/query?x-algolia-agent=Algolia%20for%20JavaScript%20(3.35.1)%3B%20Browser%20(lite)%3B%20Algolia%20Places%201.18.2&x-algolia-application-id=MYAPPID&x-algolia-api-key=MYAPIKEY');
curl_setopt($cURLConnection, CURLOPT_POST, 1);
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, '{"params":"hitsPerPage=10&language=en&query='.$keyword.'"}');

curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($cURLConnection);
curl_close($cURLConnection);
exit($result);

Thanks

Found the required parameters. The problem was that the aroundLatLngViaIP is on by default, making the results show results close to the server location. I also did the code the right way this time through the REST API documentation and did the gzip compression to speedup getting the results:

$cURLConnection = curl_init('https://places-dsn.algolia.net/1/places/query');
curl_setopt($cURLConnection, CURLOPT_POST, 1);
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, '{"query": "'.$keyword.'", "language" : "fr", "aroundLatLngViaIP" : "false", "hitsPerPage" : "10"}');

curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$headers = [
	'X-Algolia-Application-Id: MyAppKey',
	'X-Algolia-API-Key: MyAPIKey',
	'Accept: application/json',
	'Accept-Encoding: gzip, deflate, br',
	'Connection: keep-alive',
	'content-type: application/x-www-form-urlencoded',
	'Host: places-dsn.algolia.net'
];

curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($cURLConnection);
curl_close($cURLConnection);
if ( (ord($result[0])==0x1f) && (ord($result[1])==0x8b) ) {
	// skip header and ungzip the data
	$result = gzinflate(substr($result,10));
}
commented

@pratamishus Indeed you found it!

FYI if you are interested in having the results close to your user location for better relevance, you can use forward your client's IP in the X-Forwarded-For header