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
<?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

PHP
new Findort\Findort(?string $key = null, array $options = [])
OptionTypeDefaultDescription
baseUrlstringhttps://api.findort.chAPI address
langstringLanguage for all requests: de, fr, it, en
countrystring or listCountry filter for all searches: 'ch' or ['ch', 'li']
timeoutfloat8.0Time limit in seconds
transportcallableYour own HTTP function for tests: fn(string $url, array $headers): array{int, string}
PHP
$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;
}
OptionTypeDescription
limitint1 to 20, default 5
langstringlanguage for this request only
countrystring or listcountry filter for this request only
near[lat, lon] or 'lat,lon'prefer nearby results
bbox[minLon, minLat, maxLon, maxLat] or stringstrictly limit to a rectangle
typeslist or stringonly these types

reverse

PHP
$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.

PHP
$place = $findort->geocode('Zytglogge, Bern');

Place

Every result is an immutable Findort\Place object.

PropertyType
id, label, name, type, categorystring
addressarray with the address fields
locationFindort\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:

PHP
$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.

PHP
try {
    $findort->search('Bern');
} catch (Findort\FindortException $e) {
    if ($e->errorCode === 'rate_limited') {
        sleep(2);
    }
}

All codes are listed under errors.