When we were building a website for a US based organisation we encountered all sorts of issues with reliable access to a zipcode API. In the end, we figured it'd be easier if we built our own.
Postcodez is a very simple site that gives our readers access to a simple zipcode API with various functions. It's easy to create a fully functioning zipcode search website based upon the information the API provides. In fact, the simple Postcodez
website is itself API-centric - meaning that all our own requests are made via the API.
This post will show you how to:
- Determine the distance Between two zip codes with PHP.
- Retrieve information on a single zip code.
- Retrieve information on multiple zip codes.
- Retrieve zip codes within a radius of another zip.
- Retrieve zip codes within a radius of geographic coordinates
Note: This API is now unsupported.
Determine the Distance Between Two Zipcodes with PHP
This function will calculate the distance between two zipcodes. With an input of two zipcodes, the returned XML includes the distance (in miles, kilometers and nautical miles), the two placenames, counties, state codes and state.
Endpoint
postcodez.com/api/distance/[zipcode]/[zipcode]/api.xml
Retrieve Information on a Single Zip Code
The following function is used to generate information on a single zip code.
Endpoint
postcodez.com/api/zip/[zipcode]/api.xml
Retrieve Information on Multiple Zip Codes
The request we make to the API for multiple zip codes is the same as if we were making a request for a single zip code. We're using two functions so that you have more flexibility when formatting text.
Endpoint
postcodez.com/api/zip/[zipcode],[zipcode]/api.xml
There are a few checks in the function that'll basically check if each zip is 5 characters in length and a number. If not, it's removed from the string of zipcodes that are included in the XML request.
Retrieve Zip Codes within a Radius of another Zip
.
This function will retrieve zip codes that are in a defined radius of another.
Endpoint
postcodez.com/api/nearby/[zip]/[dist][unit]/api.xml
Retrieve Zip Codes within a Radius of Geographic Coordinates
Endpoint
postcodez.com/api/geo/[lat]/[lng]/[dist][unit]/api.xml
This function will retrieve zip codes that are in a defined radius of a geographic coordinate. Available units are miles and kilometers (miles by default).
Considerations
More information on the basic API and error codes can be found here . If you use this API in a project, it is expected that you would provide appropriate attribution.