GPS Zip Code Radius Web Service By Will Fitch 23 October 2006 at 9:16 pm


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:

  1. stateName
  2. city
  3. zipCode
  4. latitude
  5. longitude
  6. distance (from the specified zip code)
  7. 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
  1. <?php
  2.  
  3. // URL
  4. $zip_code = 42223; // FORT CAMPBELL ZIP CODE
  5. $radius = 50; // 50 MILES
  6.  
  7. $request_uri =
  8. ‘http://www.phpfever.com/web-services/
  9. zip_radius.php?zip_code=’.$zip_code.‘&radius=’.$radius;
  10.  
  11. // Initialize the session
  12. $curl = curl_init($request_uri);
  13.  
  14. // Set curl options
  15. curl_setopt($curl, CURLOPT_HEADER, true);
  16. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  17.  
  18. // Make the request
  19. $response = curl_exec($curl);
  20.  
  21. // Close the curl session
  22. curl_close($curl);
  23.  
  24. // Get HTTP Status code from the response
  25. $status_code = array();
  26. preg_match(‘/ddd/’, $response, $status_code);
  27.  
  28. // Check the HTTP Status code
  29. switch( $status_code[0] ) {
  30. case 200:
  31. // Success
  32. //print_r($response);
  33. break;
  34. case 503:
  35. die(‘Server Error.’);
  36. break;
  37. case 403:
  38. die(‘Forbidden.’);
  39. break;
  40. case 400:
  41. die(‘Bad Request.’);
  42. break;
  43. default:
  44. // Unknown error code. Just print it out
  45. die($status_code[0]);
  46. }
  47.  
  48. // Get the XML from the response, bypassing the header
  49. if (!($xml = strstr($response, ‘l’))) {
  50.  
  51. $xml = null;
  52. }
  53.  
  54. // Output the XML
  55. echo $xml;
  56. ?>

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] [Digg] [dzone] [Furl] [Google] [Reddit] [Slashdot] [Sphere] [Yahoo!]
Add comment

7 responses : “ GPS Zip Code Radius Web Service ”

  1. 1
    William :

    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?!

  2. 2
    will.fitch :

    http://www.google.com/gmm is one option. This tutorial is actually meant for those who want to host GPS capabilities on their website.

  3. 3
    Zip Code Proximity Search | SomeoneFun.com :

    [...] I found an excellent API at phpfever.com and am working to implement a ZIP code proximity search on this site with it. [...]

  4. 4
    Teerawut :

    Good

  5. 5
    Kaori D'Alessio :

    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.

  6. 6
    will.fitch :

    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.

  7. 7
    MOBY :

    Bookmarks…

    I can’t add your post to Digg. How I do this?…

Leave a Reply