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.

About the Author

Chris Collins

Principal Developer Chris has a wide range of experience in both back-end and front-end development, delivering high-profile projects for the likes of the BBC, The National Gallery and Investec Asset Management. Specialising in back-end PHP and database development, he is a keen advocate of the importance of testing and coding standards in developing quality software.