Home / PHP integration guide
PHP integration guide
Call the LatLongAPI from PHP with a JSON POST request, server-side license configuration, and HTTP error handling.
Configure your application
Use PHP 8 with the cURL extension enabled. Save the example as example.php.
In a Bash or compatible shell, set LATLONG_API_BASE_URL to your assigned HTTPS origin and LATLONG_API_LICENSE to your license. For testing, use the test origin below with a test license. Replace YOUR_TEST_LICENSE with the license issued for that environment. Keep the license outside source control and browser code.
export LATLONG_API_BASE_URL="https://v1-dev.latlongapi.com"
export LATLONG_API_LICENSE="YOUR_TEST_LICENSE"Request an address
<?php
$base = getenv('LATLONG_API_BASE_URL');
$api = getenv('LATLONG_API_LICENSE');
if (!$base || !$api) throw new RuntimeException('Set API configuration');
$ch = curl_init(rtrim($base, '/') . '/api/v1/reverse');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode([
'lat' => 51.5007, 'long' => -0.1246, 'api' => $api
], JSON_THROW_ON_ERROR),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($body === false || $status !== 200) {
throw new RuntimeException('Request failed; HTTP ' . $status);
}
curl_close($ch);
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
echo $data['result']['short_address'];Run the example
php example.phpUse the result
The example prints result.short_address. Read result.long_address for a longer description. Check nullable city and country fields before displaying them.
Handle failures in your service
Set a timeout, catch request errors, and record the status and request identifier without logging the license. Retry temporary failures with bounded backoff. A 401 requires a valid license; a 422 requires correcting the input.