|
| 1 | +# Postio PHP SDK |
| 2 | + |
| 3 | +[](https://packagist.org/packages/postio/postio) |
| 4 | +[](https://packagist.org/packages/postio/postio) |
| 5 | +[](https://opensource.org/licenses/MIT) |
| 6 | + |
| 7 | +PHP SDK for the [Postio API](https://postio.co.uk) — UK address, email, and |
| 8 | +phone validation. Backed by Royal Mail PAF and Ordnance Survey. PSR-18 over |
| 9 | +Guzzle, typed `readonly` value objects. |
| 10 | + |
| 11 | +## Install |
| 12 | + |
| 13 | +```bash |
| 14 | +composer require postio/postio |
| 15 | +``` |
| 16 | + |
| 17 | +Requires PHP 8.1+. |
| 18 | + |
| 19 | +## 30-second example |
| 20 | + |
| 21 | +```php |
| 22 | +<?php |
| 23 | + |
| 24 | +require 'vendor/autoload.php'; |
| 25 | + |
| 26 | +use Postio\PostioClient; |
| 27 | + |
| 28 | +$client = new PostioClient(apiKey: 'pk_live_...'); // or set POSTIO_API_KEY |
| 29 | + |
| 30 | +$result = $client->address->search('downing street'); |
| 31 | +foreach ($result->results as $hit) { |
| 32 | + echo $hit->udprn . ': ' . $hit->suggestion . PHP_EOL; |
| 33 | +} |
| 34 | + |
| 35 | +echo 'request id: ' . $result->meta->requestId . PHP_EOL; |
| 36 | +``` |
| 37 | + |
| 38 | +## API |
| 39 | + |
| 40 | +| Method | Returns | |
| 41 | +|---|---| |
| 42 | +| `$client->address->search($q, $maxResults)` | `AddressSearchEnvelope` | |
| 43 | +| `$client->address->postcode($postcode, $maxResults)` | `AddressPostcodeEnvelope` | |
| 44 | +| `$client->address->udprn($udprn)` | `AddressUdprnEnvelope` | |
| 45 | +| `$client->email->validate($address)` | `EmailEnvelope` | |
| 46 | +| `$client->phone->validate($number)` | `PhoneEnvelope` | |
| 47 | +| `$client->connect()` | `ConnectSuccess` | |
| 48 | + |
| 49 | +## Errors |
| 50 | + |
| 51 | +Every non-2xx response throws a typed exception. `PostioException` is the |
| 52 | +base, with subclasses per status code: |
| 53 | + |
| 54 | +```php |
| 55 | +use Postio\Exception\PostioInvalidKeyException; |
| 56 | +use Postio\Exception\PostioOutOfCreditException; |
| 57 | +use Postio\Exception\PostioRateLimitException; |
| 58 | + |
| 59 | +try { |
| 60 | + $client->address->postcode('not-a-postcode'); |
| 61 | +} catch (PostioInvalidKeyException $e) { |
| 62 | + // 401 |
| 63 | +} catch (PostioOutOfCreditException $e) { |
| 64 | + // 402 |
| 65 | +} catch (PostioRateLimitException $e) { |
| 66 | + // 429 — $e->retryAfter has the suggested wait in seconds |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +Every exception carries `status`, `errorCode`, `details`, `requestId`, and |
| 71 | +`envelope`. Quote `requestId` when reporting issues. |
| 72 | + |
| 73 | +## Configuration |
| 74 | + |
| 75 | +```php |
| 76 | +$client = new PostioClient( |
| 77 | + apiKey: 'pk_live_...', |
| 78 | + baseUrl: 'https://api.postio.co.uk/v1', // default |
| 79 | + timeout: 10.0, // seconds |
| 80 | + retries: 2, // 0 to disable |
| 81 | + headers: ['x-tracking-id' => '...'], // extra headers |
| 82 | +); |
| 83 | +``` |
| 84 | + |
| 85 | +Default retry policy: 2 retries on 408/409/429/5xx + network/timeout, |
| 86 | +exponential backoff with full jitter (500ms → 8s cap). |
| 87 | + |
| 88 | +## Frameworks |
| 89 | + |
| 90 | +The SDK is framework-agnostic. Inject `PostioClient` once via your |
| 91 | +container: |
| 92 | + |
| 93 | +**Laravel** — bind in a service provider: |
| 94 | + |
| 95 | +```php |
| 96 | +$this->app->singleton(PostioClient::class, fn () => new PostioClient( |
| 97 | + apiKey: config('services.postio.key'), |
| 98 | +)); |
| 99 | +``` |
| 100 | + |
| 101 | +**Symfony** — register as a service in `services.yaml`: |
| 102 | + |
| 103 | +```yaml |
| 104 | +Postio\PostioClient: |
| 105 | + arguments: |
| 106 | + $apiKey: '%env(POSTIO_API_KEY)%' |
| 107 | +``` |
| 108 | +
|
| 109 | +## Links |
| 110 | +
|
| 111 | +- [Docs](https://postio.co.uk/docs) |
| 112 | +- [API reference (OpenAPI)](https://postio.co.uk/openapi.json) |
| 113 | +- [Changelog](./CHANGELOG.md) |
| 114 | +- [Issues](https://github.com/postio-uk/postio-php/issues) |
| 115 | +
|
| 116 | +## License |
| 117 | +
|
| 118 | +MIT — see [LICENSE](./LICENSE). |
| 119 | +
|
| 120 | +> *Postio is a trading name of Onno Group Limited, registered in |
| 121 | +> England & Wales (company no. 08622799). Registered office: |
| 122 | +> Suite 22 Trym Lodge, 1 Henbury Road, Westbury-On-Trym, Bristol BS9 3HQ.* |
0 commit comments