Project Collaboration Invitation - ZIP Code Search API

Hello coders.

I’m new here so I wanted to say hello.
I’m a professional hobbyist full-stack coder.
I mostly build WordPress MultiSite websites on the LAMP stack.

However, in the last few months, I have decided to deep dive into learning fundamental programming skills to learn deeply how the code works inside of the higher level stuff I use. I am glad I found this site.

So I am working on learning how to process API requests and returned datasets in JSON using PHP and JavaScript.

I am working on an application to find all zip codes within a certain radius of my target zip code and then look up the city, state, county and distance from the target zip. Then I will use the returned JSON data set to build an application that cross-references a list of dealers providing a set of services to customers in that zip code and display them in a nice list. I am working with some external data management systems that have API interfaces to allow me to query the datasets and get json datasets back as well as send updated to the datasets via post requests.

I am looking for a team of folks with good front-end design skills and an interest in learning backend and middleware skills to help me on the project.

I have a cool WebIDE coding system that we can all team code on the codebase so it will allow us to all see what each of us is typing in real time as well as chat with each other.

Reply if you are interested in chatting about this. I will share access links to the WebIDE with you.

Thanks for your consideration.

Michael.

I’ve done this and let me tell you the problem with this approach.
You’ll get complaints from users. Why?
Because it’s not going to be the result they expected.

Zip Codes are irregular boundaries.

The difference between one zip code and the next is just (1) street block. You’ll get users that will complain, “I entered my zip code to find all nearby (fill-in-the-blanks) and XXXX business didn’t show up, even though it’s just a block from where I live.” – but it was in another zip code area so your program didn’t list it in the results.

Also, this “certain radius of my target zip” is kinda meaningless because a) zip code areas are irregular, and (b) radius means there’s a center point. Where is the center point of an irregular zip code area? The zip code area can be long and skinny, or fat blob but has irregular areas extending out.

The problem will always be for people living on the edge of these zip codes.

“Distance” calculation between zip code areas is also meaningless… distance to where? The edge of the zip code area, or the estimated “center” point of that irregular area?

A better approach will be to get the Lat/Lng of each of your points of interests (POI), and the Lat/Lng of the user’s location and use that for the calculation of nearby POIs. Then you can perform a radius calculation (say, 5 miles radius) of surrounding POI based on the user’s location.

… or you can use the lat/lng values to grab a “square area” based on the radius… so you’ll get all POIs within the circle and some outside the circle, just off the edges.

An SQL query can spit out all 2, 5, 10 mile radius POIs from your user’s location very easily and fast.

In JS, distance between (2) lat/lng points can be computed as: (pretty simple enough to translate to other languages)

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}

This distance is “as the crow flies”, i.e. point to point… and not street or travel distance… which will be a higher value.

If you want a more exact travel distance, you’d have to use Google Maps API

Some tips for your project. Good luck!

2 Likes

Thanks for the welcome Randeldawson.

It is really refreshing to connect with other folks with an interest in coding. Thanks for giving me a heads up on things. I will spend lots of time here filling in the areas that my hybrid formal and self-taught coding explorations have left yearning for answers.

Fate seems to lead me to the answers only when I am ready to understand them. I think fate has delivered me to the holy grail of answers…

Thanks, owel.

You bring up great points about the irregular shape of zip code areas. My initial study of this project brought me to a web service that has an api which will return a list of the zip codes in the given radius as json or xml. if I simply provide the center zip code and the radius size in miles and the format I want the data returned to me in. ie. 98012 and 5 miles and JSON.

It is a subscription service which is ok at the moment as I feel the expense and time to reinvent the wheel and do all the maintenance of the dataset of zip code data from the usps and other sources of this info are too time-consuming.

My project is really about having a group of people meet at a venue on a regular basis. Each of these venues will be unique to each zip code. The group of people will represent providers of unique skill sets with only one provider of each unique skill set represented and allowed at each venue.

The categories of skill sets represented are fixed and are the same for each group venue.

What my algorithm and eventual application solution must do is to prepare a list of providers for potential consumers of the skill sets to discover a completed list of providers who offer that skill set. The problem is that there may not be a provider of a certain skill category that is a member of the venue represented by the zip code the consumer lives in, so the algorithm must then search outward to the next closest zip code area
and confirm there is an active group in the zip code and see if there is a provider of the yet unrepresented skill set available from that group to be listed in the resultant list for the consumer to view.

I am using another web-based data management service which has an API to store all the db tables of skill providers and venues and associated things. This service allows me to embed the tables in external websites or send api calls and get raw json back as well as do posts to change the tables using php, javascript or some other language.

My stakeholder is a spreadsheet wiz but knows nothing about coding web apps. So he is doing all the data entry using the data management system and I am writing the web ui and backend and middleware to tie all the web services together. I may even try using ifttt and zapier if possible.

SO…

I have gotten some code working to grab the json from the web service and convert the json to a php array so I can work with the individual members of the data set. ( there may be a better way but I can’t seem to figure out how to programmatically access individual members of the JSON object returned.)

Here is a sample of what I get from the code I have.

<!DOCTYPE html>
<html>
<body>

<?php

/*
** ZipFinder
** Michael McGinn
** 3/30/2018
**
**
** Here is what I have so far.  I have stiped out the api key and the web site of the zip service so I don't spam.
** This is rough ugly code but you get the idea I hope.
** I simply send data to this app via a web form post.
*/




$key="";
$zipf = $_POST['zip'];
$zipa="&zip=".$zipf;
$radiusa="&radius=".$_POST["radius"];
$formata="&format=".$_POST["format"];
$url="https://www.example.com/webservices/radius.php?";
$jsoncall=$url.$key.$zipa.$radiusa.$formata;
//echo "<BR>".$jsoncall;

?json = file_get_contents($jsoncall);//grabs the output of the api call

//echo "<br><BR>json=".$json."<br><BR>";


$dealers=array();
$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);
$field=0;
foreach ($jsonIterator as $key => $val) {
	
    if(is_array($val)) {
        $field=0;
        $dealers[$key][$field][0]=$key;
        $dealer=$key;
        
        
    } else {
      
        $dealers[$dealer][$field][1]=$key;
        $dealers[$dealer][$field][2]=$val;
        $field++;

    }
    
    
}

echo "Number of Dealers=".count($dealers)."<BR>";
$dealercount=count($dealers)-1;

for($i=0;$i<=$dealercount;$i++){
echo "<BR>Exracted Value=".$dealers[$i][0][0];
echo "<BR>Exracted Value=".$dealers[$i][0][1];
echo "<BR>Exracted Value=".$dealers[$i][0][2];
echo "<BR>Exracted Value=".$dealers[$i][1][1];
echo "<BR>Exracted Value=".$dealers[$i][1][2];
echo "<BR>Exracted Value=".$dealers[$i][2][1];
echo "<BR>Exracted Value=".$dealers[$i][2][2];
echo "<BR>Exracted Value=".$dealers[$i][3][1];
echo "<BR>Exracted Value=".$dealers[$i][3][2];
echo "<BR>Exracted Value=".$dealers[$i][4][1];
echo "<BR>Exracted Value=".$dealers[$i][4][2];
echo "<BR>----<BR>";
}
?>
</body>
</html>

Here is the output.

https://www.example.com/webservices/radius.php?key=?????&zip=98012&radius=5&format=json

json={"results":[{"zip":"98012","city":"Bothell","county":"Snohomish","state":"WA","distance":"0.0"},{"zip":"98082","city":"Mill Creek","county":"Snohomish","state":"WA","distance":"1.7"},{"zip":"98021","city":"Bothell","county":"Snohomish","state":"WA","distance":"2.8"},{"zip":"98087","city":"Lynnwood","county":"Snohomish","state":"WA","distance":"3.0"},{"zip":"98036","city":"Lynnwood","county":"Snohomish","state":"WA","distance":"3.1"},{"zip":"98037","city":"Lynnwood","county":"Snohomish","state":"WA","distance":"3.4"},{"zip":"98208","city":"Everett","county":"Snohomish","state":"WA","distance":"4.3"},{"zip":"98046","city":"Lynnwood","county":"Snohomish","state":"WA","distance":"4.5"}]}

Number of Dealers=9

Exracted Value=0
Exracted Value=zip
Exracted Value=98012
Exracted Value=city
Exracted Value=Bothell
Exracted Value=county
Exracted Value=Snohomish
Exracted Value=state
Exracted Value=WA
Exracted Value=distance
Exracted Value=0.0
----

Exracted Value=1
Exracted Value=zip
Exracted Value=98082
Exracted Value=city
Exracted Value=Mill Creek
Exracted Value=county
Exracted Value=Snohomish
Exracted Value=state
Exracted Value=WA
Exracted Value=distance
Exracted Value=1.7
----

Exracted Value=2
Exracted Value=zip
Exracted Value=98021
Exracted Value=city
Exracted Value=Bothell
Exracted Value=county
Exracted Value=Snohomish
Exracted Value=state
Exracted Value=WA
Exracted Value=distance
Exracted Value=2.8
----

Exracted Value=3
Exracted Value=zip
Exracted Value=98087
Exracted Value=city
Exracted Value=Lynnwood
Exracted Value=county
Exracted Value=Snohomish
Exracted Value=state
Exracted Value=WA
Exracted Value=distance
Exracted Value=3.0
----

Exracted Value=4
Exracted Value=zip
Exracted Value=98036
Exracted Value=city
Exracted Value=Lynnwood
Exracted Value=county
Exracted Value=Snohomish
Exracted Value=state
Exracted Value=WA
Exracted Value=distance
Exracted Value=3.1
----

Exracted Value=5
Exracted Value=zip
Exracted Value=98037
Exracted Value=city
Exracted Value=Lynnwood
Exracted Value=county
Exracted Value=Snohomish
Exracted Value=state
Exracted Value=WA
Exracted Value=distance
Exracted Value=3.4
----

Exracted Value=6
Exracted Value=zip
Exracted Value=98208
Exracted Value=city
Exracted Value=Everett
Exracted Value=county
Exracted Value=Snohomish
Exracted Value=state
Exracted Value=WA
Exracted Value=distance
Exracted Value=4.3
----

Exracted Value=7
Exracted Value=zip
Exracted Value=98046
Exracted Value=city
Exracted Value=Lynnwood
Exracted Value=county
Exracted Value=Snohomish
Exracted Value=state
Exracted Value=WA
Exracted Value=distance
Exracted Value=4.5
----

Exracted Value=
Exracted Value=
Exracted Value=
Exracted Value=
Exracted Value=
Exracted Value=
Exracted Value=
Exracted Value=
Exracted Value=
Exracted Value=
Exracted Value=
----

So I’m now working on the code to process this list and do the search for the groups by zip and prepare the list of skill providers.

I am also open to any hints on how to refactor this code so I can skip parsing the json into the array and just directly access each member of the json object.

example:

$nextzip=$jsonIterator??? //what is the syntax so that $nextzip would be set to the 2nd zip code in the returned json dataset.

Wanna work on this with me?

Update:

I figured out how to access the individual members of the json result set.

$json = file_get_contents('https://www.???.com/webservices/radius.php?key=????????&zip=98012&radius=5&format=json');
$myresult = json_decode($json);
//$json = file_get_contents($jsoncall);//grabs the output of the api call
echo "<br><BR>json=".$json."<br><BR>";
echo "<br>NUMBER OF RESULTS:".count($myresult->results);
$resultsa=count($myresult->results);
for($l=0;$l<=$resultsa;$l++){
echo "<BR>ZIP:".$myresult->results[$l]->zip;
echo "<BR>CITY:".$myresult->results[$l]->city;
echo "<BR>COUNTY:".$myresult->results[$l]->county;
echo "<BR>ST:".$myresult->results[$l]->st;
echo "<BR>DISTANCE:".$myresult->results[$l]->distance;
}

Hunting on stack overflow.  found it.