The Belgian mail service, Bpost, has an API to
Create an \Spatie\BpostAddressWebservice\Address
, and validate it.
$addressValidator = AddressValidator::create();
$address = Address::create([
'streetName' => 'Samberstraat',
'streetNumber' => '69',
'boxNumber' => 'D',
'postalCode' => '2060',
'municipalityName' => 'Antwaarpe',
'country' => 'BELGIE',
]);
$validatedAddress = $addressValidator->validate($address);
The validated address can contain errors and warnings. If it has any error or warning, we say that it has issues.
$validatedAddress->hasErrors(); // false
$validatedAddress->hasWarnings(); // true
$validatedAddress->hasIssues(); // true
An array of errors, warnings, or issues can be retrieved from the validated address.
$validatedAddress->errors(); // []
$validatedAddress->warnings()[0]->attribute(); // 'municipalityName'
$validatedAddress->warnings()[0]->message(); // 'anomaly_in_field'
$validatedAddress->issues()[0]->attribute(); // 'municipalityName'
$validatedAddress->issues()[0]->message(); // 'anomaly_in_field'
A validated addresses' attributes can be retrieved.
$validatedAddress->streetName; // 'Samberstraat'
$validatedAddress->streetNumber; // '69'
$validatedAddress->boxNumber; // ''
$validatedAddress->postalCode; // '2060'
$validatedAddress->municipalityName; // 'Antwaarpe'
$validatedAddress->country; // 'BELGIE'
$validatedAddress->toArray();
// [
// 'streetName' => 'SAMBERSTRAAT',
// 'streetNumber' => '69',
// 'boxNumber' => '',
// 'postalCode' => '2060',
// 'municipalityName' => 'ANTWERPEN',
// 'country' => 'BELGIE',
// ]
You can also validate up to 100 addresses at a time. An array of ValidatedAddresses
will be returned.
$addressValidator = AddressValidator::create();
$validatedAddresses = $addressValidator->validateMany([
Address::create([
'streetName' => 'Samberstraat',
'streetNumber' => '69',
'boxNumber' => 'D',
'postalCode' => '2060',
'municipalityName' => 'Antwaarpe',
'country' => 'BELGIE',
]),
// ...
]);
Address webservice documentation is available on bpost.be.
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
composer require spatie/bpost-address-webservice
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
The MIT License (MIT). Please see License File for more information.