api-postcode/php-client
PHP client for api-postcode.nl to look up Dutch address details by postcode and house number. Install via Composer, create a PostcodeClient with your token, and fetch street, city, house number, zip code, latitude, and longitude.
Installation
composer require api-postcode/php-client
Add to composer.json if using a custom package name (e.g., api-postcode/php-postcode).
First Request
use ApiPostcode\Client;
$client = new Client('YOUR_API_KEY');
$response = $client->getPostcode('EC1A 1BB'); // Example UK postcode
dd($response->getData());
Key Files
src/Client.php: Core class for API interactions.src/Response.php: Handles API responses (status, data, errors).src/Exceptions/: Custom exceptions (e.g., ApiException).$client = new Client('YOUR_API_KEY');
$response = $client->reverseGeocode(51.5074, -0.1278); // Latitude, Longitude
$address = $response->getData()['address'];
Basic Requests
$client->getPostcode('SW1A 1AA');
$client->reverseGeocode(51.5074, -0.1278);
$client->autocomplete('London');
Custom Endpoints
Extend the Client class or use the low-level request() method:
$response = $client->request('postcodes', ['postcode' => 'W1A 0AX']);
Batch Processing Loop through postcodes with error handling:
$postcodes = ['EC1A 1BB', 'SW1A 1AA', 'W1A 0AX'];
foreach ($postcodes as $postcode) {
try {
$data = $client->getPostcode($postcode)->getData();
// Process $data
} catch (\ApiPostcode\Exceptions\ApiException $e) {
Log::error("Failed for $postcode: " . $e->getMessage());
}
}
Laravel Service Provider Bind the client to the container for dependency injection:
$this->app->singleton(Client::class, function ($app) {
return new Client(config('services.postcode.api_key'));
});
Usage in controllers:
use ApiPostcode\Client;
public function __construct(Client $client) {
$this->client = $client;
}
Caching Responses Cache responses for 1 hour (adjust TTL as needed):
$cacheKey = "postcode_{$postcode}";
$data = Cache::remember($cacheKey, now()->addHours(1), function () use ($client, $postcode) {
return $client->getPostcode($postcode)->getData();
});
Rate Limiting Implement a queue job for bulk requests to avoid hitting rate limits:
PostcodeJob::dispatch($postcode)->onQueue('postcode');
API Key Management
.env:
POSTCODE_API_KEY=your_key_here
.env to version control.Error Handling
\ApiPostcode\Exceptions\ApiException for HTTP errors (e.g., 404, 429). Always wrap requests in try-catch:
try {
$client->getPostcode('INVALID_POSTCODE');
} catch (\ApiPostcode\Exceptions\ApiException $e) {
// Handle invalid postcode or rate limits
}
Deprecated Methods
Curl Configuration
curl under the hood. If you encounter SSL issues, configure defaults:
$client = new Client('YOUR_API_KEY', [
CURLOPT_SSL_VERIFYPEER => false, // Disable for testing (not recommended for production)
]);
Enable Verbose Logging
Pass a LoggerInterface to the client for debugging:
use Psr\Log\LoggerInterface;
$client = new Client('YOUR_API_KEY', [], new Monolog\Logger('postcode'));
Inspect Raw Responses Access the raw response object for debugging:
$response = $client->getPostcode('EC1A 1BB');
dd($response->getRawResponse()); // Full HTTP response
Custom Response Parsing
Extend the Response class to add custom parsing logic:
class CustomResponse extends \ApiPostcode\Response {
public function getFormattedAddress() {
$data = $this->getData();
return $data['address']['line1'] . ', ' . $data['address']['city'];
}
}
Mocking for Tests
Use Laravel’s HTTP client or mock the Client class:
$mockClient = Mockery::mock(Client::class);
$mockClient->shouldReceive('getPostcode')
->once()
->andReturn(new \ApiPostcode\Response(200, ['data' => ['postcode' => 'EC1A 1BB']]));
Adding Headers Pass custom headers via the constructor:
$client = new Client('YOUR_API_KEY', [
'headers' => [
'X-Custom-Header' => 'value',
],
]);
Default Endpoint
The client assumes the API endpoint is https://api.postcodes.io. Override it if using a custom URL:
$client = new Client('YOUR_API_KEY', ['base_uri' => 'https://custom-api.example.com']);
Response Format
The API returns JSON by default. If the response format changes, update the parseResponse() method in Client.php.
How can I help you explore Laravel packages today?