geocoder()->geocode( city: 'Warszawa', street: 'Domaniewska', house: '37', country: 'PL' ); $matches = $response->raw()['result'] ?? []; if ($matches === []) { return ['status' => 'NO_RESULTS']; } if (!is_array($matches) || !isset($matches[0])) { throw new RuntimeException('UNEXPECTED_RESULT'); } $first = $matches[0]; return ['status' => 'OK', 'lng' => $first['x'] ?? null, 'lat' => $first['y'] ?? null, 'city' => $first['city'] ?? null, 'quality' => $first['quality'] ?? null]; } function revgeocode(ApiClient $client): array { $response = $client->geocoder()->revgeocode( point: [21.0065983, 52.1832837], getPostalCode: true ); $address = $response->raw()['result'] ?? []; if (!$address) { return ['status' => 'NO_RESULTS']; } if (!is_array($address)) { throw new RuntimeException('UNEXPECTED_RESULT'); } return ['status' => 'OK', 'city' => $address['city'] ?? null, 'street' => $address['street'] ?? null, 'house' => $address['house'] ?? null, 'pcode' => $address['pcode'] ?? null]; } if (realpath($_SERVER['SCRIPT_FILENAME'] ?? '') === __FILE__) { try { $client = buildClient(getenv('AUTOMAPA_SESSION_ID') ?: ''); echo json_encode(['geocode' => geocode($client), 'revgeocode' => revgeocode($client)]); } catch (AutomapaException | RuntimeException $error) { fwrite(STDERR, "SDK_ERROR_OR_SESSION_REQUIRED\n"); exit(1); } }