I'd do this in two parts:
- A geocoding script, run once and results stored in persistent cache (eg. database). This way you will avoid hitting rate limits and speed up the final lookup.
- Script for calculating distance, run when required or cache this too to build a lookup table storing distances between each postcode and all the others. As you only have 100 zips this lookup table would not be very large.
Geocoding
<?php
// Script to geocode each ZIP code. This should only be run once, and the
// results stored (perhaps in a DB) for subsequent interogation.
// Note that google imposes a rate limit on its services.
// Your list of zipcodes
$zips = array(
'47250', '43033', '44618'
// ... etc ...
);
// Geocode each zipcode
// $geocoded will hold our results, indexed by ZIP code
$geocoded = array();
$serviceUrl = "http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:%s&sensor=false";
$curl = curl_init();
foreach ($zips as $zip) {
curl_setopt($curl, CURLOPT_URL, sprintf($serviceUrl, urlencode($zip)));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$data = json_decode(curl_exec($curl));
$info = curl_getinfo($curl);
if ($info['http_code'] != 200) {
// Request failed
} else if ($data->status !== 'OK') {
// Something happened, or there are no results
} else {
$geocoded[$zip] =$data->results[0]->geometry->location;
}
}
Calculating distance
As Mark says, there are a swathe of good examples for this, such as Measuring the distance between two coordinates in PHP