vladshut/postcode-api-client

Client library for the postcodeapi.nu web service.

1.0 2019-05-11 08:15 UTC

This package is auto-updated.

Last update: 2024-04-11 19:11:38 UTC


README

PostcodeAPIClient is a PHP client library for the PostcodeAPI.nu web service.

Requirements

PostcodeAPIClient works with PHP 5.5.0 or up. This library depends on the HTTPPlug, see http://docs.php-http.org/en/latest/httplug/introduction.html.

Installation

PostcodeAPIClient can easily be installed using Composer:

composer require vladshut/postcode-api-client

Usage

Instantiate the client and replace the API key with your personal credentials:

// Use the composer autoloader to load dependencies
require_once 'vendor/autoload.php';

// initiate client
$apiKey = 'replace_with_your_own_api_key';
// In this example we made use of the Guzzle6 as HTTPClient in combination with an HTTPPlug compatible adapter.
$client = new \VS\PostcodeAPI\Client(
    new Http\Adapter\Guzzle6\Client(
        new GuzzleHttp\Client([
            'headers' => [
                'X-Api-Key' => $apiKey
            ]
        ])
    )
);

// call endpoints
$response = $client->getAddresses('5041EB', 21);
$response = $client->getAddress('0855200000061001');
$response = $client->getPostcode('5041EB');

// Note that this call is only available with a premium account
$response = $client->getPostcodes('51.566405', '5.077171');

Note that to be able to run the example above you should have ran the following command, to have Guzzle6 and the Adapter available.

composer require php-http/guzzle6-adapter

Within Symfony project

We recommend to use Guzzle, to be able to use Guzzle in combination with the PostcodeApiClient you should also make use of the Guzzle6Adapter. By running the following command you automatically install Guzzle aswel.

composer require php-http/guzzle6-adapter

And add the following service definitions:

project.http.guzzle.client:
    class: GuzzleHttp\Client
    arguments:
        - { headers: { X-Api-Key: 'replace_with_your_own_api_key' } }

project.http.adapter.guzzle.client:
    class: Http\Adapter\Guzzle6\Client
    arguments:
        - '@project.http.guzzle.client'

project.client.postal_code:
    class: VS\PostcodeAPI\Client
    arguments:
        - '@project.http.adapter.guzzle.client'

You should now be able use the project.client.postal_code service to make requests to the PostcodeAPI.