PHP
Ein Client für PHP ab 8.1, ohne Abhängigkeiten. Nutzt cURL, wenn vorhanden, sonst file_get_contents.
Installieren
composer require findort/findort
<?php
require 'vendor/autoload.php';
$findort = new Findort\Findort(getenv('FINDORT_KEY'), ['lang' => 'de']);
$place = $findort->geocode('Bundesplatz 3, Bern', ['country' => 'ch']);
echo $place?->label; // Bundesplatz 3, 3011 Bern
echo $place?->location->lat; // 46.94…Auf dem Server nimmst du einen geheimen Key sk_…. Voraussetzungen: PHP 8.1 oder neuer und ext-json. Mit ext-curl geht es schneller.
Client erstellen
new Findort\Findort(?string $key = null, array $options = [])| Option | Typ | Vorgabe | Beschreibung |
|---|---|---|---|
baseUrl | string | https://api.findort.ch | Adresse der API |
lang | string | Sprache für alle Anfragen: de, fr, it, en | |
country | string oder list | Länderfilter für alle Suchen: 'ch' oder ['ch', 'li'] | |
timeout | float | 8.0 | Zeitlimit in Sekunden |
transport | callable | Eigene HTTP-Funktion für 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 | Typ | Beschreibung |
|---|---|---|
limit | int | 1 bis 20, Vorgabe 5 |
lang | string | Sprache nur für diese Anfrage |
country | string oder list | Länderfilter nur für diese Anfrage |
near | [lat, lon] oder 'lat,lon' | Resultate in der Nähe bevorzugen |
bbox | [minLon, minLat, maxLon, maxLat] oder string | hart auf ein Rechteck begrenzen |
types | list oder string | nur diese Typen |
reverse
$places = $findort->reverse(46.9465, 7.4443, ['limit' => 1, 'types' => 'address']);
echo $places[0]->label ?? 'nichts gefunden';Optionen: limit, lang, radius (Kilometer, 0.01 bis 50), types.
geocode
Der beste Treffer als Place oder null. Nimmt dieselben Optionen wie search.
$place = $findort->geocode('Zytglogge, Bern');Place
Jedes Resultat ist ein unveränderliches Objekt Findort\Place.
| Eigenschaft | Typ |
|---|---|
id, label, name, type, category | string |
address | array mit den Adressfeldern |
location | Findort\Location mit lat und lon |
bbox | ?array als [minLon, minLat, maxLon, maxLat] |
Dazu kommen Abkürzungen: street(), housenumber(), postcode(), city() (Stadt oder Ortsteil) und countryCode(). Alle geben null zurück, wenn der Wert fehlt.
Location::distanceTo() rechnet die Luftlinie in Kilometern:
$bern = $findort->geocode('Bahnhof Bern');
$thun = $findort->geocode('Bahnhof Thun');
printf('%.1f km', $bern->location->distanceTo($thun->location));Fehler
Bei einer Fehlerantwort wirft der Client Findort\FindortException. getCode() ist der HTTP-Status, errorCode der Findort-Code. Ist der Server nicht erreichbar, ist errorCode gleich network_error und der Status 0.
try {
$findort->search('Bern');
} catch (Findort\FindortException $e) {
if ($e->errorCode === 'rate_limited') {
sleep(2);
}
}Alle Codes stehen unter Fehler.