# PHP

> A client for PHP 8.1 and later with no dependencies. Uses cURL when available, otherwise file_get_contents.

Source: https://findort.ch/en/docs/php/

## Install

<p class="install"><span class="sign">composer require findort/findort</span></p>

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

| 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

```php
    '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](https://findort.ch/en/docs/response/#types) |

## reverse

```php
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

Every result is an immutable `Findort\Place` object.

| Property | Type |
|---|---|
| `id`, `label`, `name`, `type`, `category` | string |
| `address` | array with the [address fields](https://findort.ch/en/docs/response/#address) |
| `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:

```php
$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
    $findort->search('Bern');
} catch (Findort\FindortException $e) {
    if ($e->errorCode === 'rate_limited') {
        sleep(2);
    }
}
```

All codes are listed under [errors](https://findort.ch/en/docs/errors/).
