We recently had a requirement which involved returning search results within a specific radius of a user-specified postcode. To do this, we needed a mechanism for converting a user’s postcode to a set of latitude and longitude coordinates. After some research, we settled on the Postcodes.io
service: an open source service based on open data.
To make our requests to postcodes.io, we used Guzzle
. Guzzle is a superb PHP HTTP client library, but what makes it a perfect fit for this requirement is the additional Guzzle Services
component. This library wraps the core Guzzle functionality, allowing you to describe web services and parse responses into model structures in a straightforward manner.
The application we developed this functionality for is based on the Symfony2 framework, so we encapsulated this functionality in a bundle so it could potentially be reused across other projects. The bundle provides a Guzzle client that is configured with all of the operations available to the postcodes.io API; this is defined as a service through Symfony2′s dependency injection, meaning it’s simple to inject the client into your controllers or other services.
We’ve open sourced our PostcodesIoBundle
; it’s available on Box UK’s GitHub account
and also via Packagist
for installation as a Composer dependency.
To stay up-to-date with the latest tech news and views, be sure to check out our weekly tech round-up posts - and sign up to our mailing list to have them delivered direct to your inbox.