PHP
A client for PHP 8.1 and later with no dependencies. Uses cURL when available, otherwise file_get_contents.
Install
composer require findort/findort
<?php
require 'vendor/autoload.php';
$findort = new Findort\Findort(getenv('FINDORT_KEY'), ['lang' => 'en']);
$place = $findort->geocode('Bundesplatz 3, Bern', ['country' => 'ch']);
echo $place?->label; // Bundesplatz 3, 3011 Bern
echo $place?->location->lat; // 46.94…On servers use a secret key sk_…. Requirements: PHP 8.1 or later and ext-json. ext-curl makes it faster.
Creating a client
new Findort\Findort(?string $key = null, array $options = [])| Option | Type | Default | Description |
|---|---|---|---|
baseUrl | string | https://api.findort.ch | API address |
lang | string | Language for all requests: de, fr, it, en | |
country | string or list | Country filter for all searches: 'ch' or ['ch', 'li'] | |
timeout | float | 8.0 | Time limit in seconds |
transport | callable | Your own HTTP function for tests: fn(string $url, array $headers): array{int, string} |
search
$places = $findort->search('Bahnhofstr 1 Zür', [
'limit' => 5,
'types' => ['address', 'poi'],
'near' => [47.3769, 8.5417],
]);
foreach ($places as $place) {
echo $place->label, PHP_EOL;
}| Option | Type | Description |
|---|---|---|
limit | int | 1 to 20, default 5 |
lang | string | language for this request only |
country | string or list | country filter for this request only |
near | [lat, lon] or 'lat,lon' | prefer nearby results |
bbox | [minLon, minLat, maxLon, maxLat] or string | strictly limit to a rectangle |
types | list or string | only these types |
reverse
$places = $findort->reverse(46.9465, 7.4443, ['limit' => 1, 'types' => 'address']);
echo $places[0]->label ?? 'nothing found';Options: limit, lang, radius (kilometres, 0.01 to 50), types.
geocode
The best match as a Place, or null. Takes the same options as search.
$place = $findort->geocode('Zytglogge, Bern');Place
Every result is an immutable Findort\Place object.
| Property | Type |
|---|---|
id, label, name, type, category | string |
address | array with the address fields |
location | Findort\Location with lat and lon |
bbox | ?array as [minLon, minLat, maxLon, maxLat] |
Plus shortcuts: street(), housenumber(), postcode(), city() (city or locality) and countryCode(). Each returns null when the value is missing.
Location::distanceTo() returns the straight-line distance in kilometres:
$bern = $findort->geocode('Bahnhof Bern');
$thun = $findort->geocode('Bahnhof Thun');
printf('%.1f km', $bern->location->distanceTo($thun->location));Errors
On an error response the client throws Findort\FindortException. getCode() is the HTTP status, errorCode the Findort code. If the server is unreachable, errorCode is network_error and the status is 0.
try {
$findort->search('Bern');
} catch (Findort\FindortException $e) {
if ($e->errorCode === 'rate_limited') {
sleep(2);
}
}All codes are listed under errors.