GPS Zip Code Radius Web Service
Posted on 23. Oct, 2006 by Will Fitch in PHP, Resources
Update! Due to the demand for an easier interface, I have developed a SOAP service offering more functionality and requires less coding on your end. You may see the overview here: http://www.phpfever.com/soap-zip-code-web-service.html.
Ok….
So I’ve created this script based off of the GPS info. It allows you to specify a zip code and radius (in mileage), and it will return the following values in XML:
- stateName
- city
- zipCode
- latitude
- longitude
- distance (from the specified zip code)
- timeZone
It is available via GET at the following URL:
http://www.phpfever.com/web-services/zip_radius.php
There are some GET variables you will have to specify, so read on with a tutorial using CURL.
If you have a script that’s actually going to use this (i.e. dealer locator, dating site), you will probably want to use CURL to get the data necessary.
After making the request, based off of your needs, you will have to parse the XML using your choice of functions/classes provided by PHP or PEAR. Here is a sample script using the CURL extension:
-
<?php
-
-
// URL
-
$zip_code = 42223; // FORT CAMPBELL ZIP CODE
-
$radius = 50; // 50 MILES
-
-
$request_uri =
-
‘http://www.phpfever.com/web-services/
-
zip_radius.php?zip_code=’.$zip_code.‘&radius=’.$radius;
-
-
// Initialize the session
-
$curl = curl_init($request_uri);
-
-
// Set curl options
-
curl_setopt($curl, CURLOPT_HEADER, true);
-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
-
-
// Make the request
-
$response = curl_exec($curl);
-
-
// Close the curl session
-
curl_close($curl);
-
-
// Get HTTP Status code from the response
-
-
// Check the HTTP Status code
-
switch( $status_code[0] ) {
-
case 200:
-
// Success
-
//print_r($response);
-
break;
-
case 503:
-
break;
-
case 403:
-
break;
-
case 400:
-
break;
-
default:
-
// Unknown error code. Just print it out
-
}
-
-
// Get the XML from the response, bypassing the header
-
-
$xml = null;
-
}
-
-
// Output the XML
-
?>
That’s all it takes. I will be working on other scripts for exporting in a different manner. In the meantime, XML is always a good option (better than nothing).


![[del.icio.us]](http://www.willfitch.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.willfitch.com/wp-content/plugins/bookmarkify/digg.png)
![[Google]](http://www.willfitch.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://www.willfitch.com/wp-content/plugins/bookmarkify/linkedin.png)
![[StumbleUpon]](http://www.willfitch.com/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Windows Live]](http://www.willfitch.com/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.willfitch.com/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.willfitch.com/wp-content/plugins/bookmarkify/email.png)


William
08. Dec, 2006
Do you know where i could get my GPS service by cell phones?
my friends download GPS software to cell phones, and their phone become the navigation system. so cool! but, how i can get
one too?!
will.fitch
11. Dec, 2006
http://www.google.com/gmm is one option. This tutorial is actually meant for those who want to host GPS capabilities on their website.
Zip Code Proximity Search | SomeoneFun.com
12. Dec, 2006
[...] I found an excellent API at phpfever.com and am working to implement a ZIP code proximity search on this site with it. [...]
Teerawut
03. Jul, 2007
Good
Kaori D'Alessio
19. Jul, 2007
how do i use the zipcodes by radius service from asp.net? i get a strange error all the time saying that type text/html was returned when type text/xml was expected. is this a security issue.
will.fitch
26. Jul, 2007
Hi Kaori,
I will be implementing a SOAP service tonight for you to use. For all others, I will be removing the REST service for the newer, better SOAP method.
MOBY
10. Aug, 2007
Bookmarks…
I can’t add your post to Digg. How I do this?…