How to Obtain the Zip Code of an Address from the Google Maps API using PHP


In this article, we go over how to obtain the zip code of an address from the google maps API using PHP.

To see this code in action, see the following page: Zip Code Finder.

In this example, for the google maps API, we are going to use the API when it is encoded in JSON format. The other format is XML, which can also be readily used. However, for this example, we focus on JSON. To see the code of how to obtain the zip code when the data in the API is in XML format, see the following link: Code to Get the Zip Code from the Google Maps API in XML Format with PHP.

PHP has built-in functions that allows it to work easily with JSON data or XML data.

When a user inputs a street address into the the google maps API, you can always obtain the zip code of that address, because all street addresses have zip codes.

If you enter just the city and state, google maps may or may not have the zip code. This is because many cities are huge and have multiple zip codes. So if you enter an address such as New York, NY, the google maps API doesn't give the zip code, because there are multiple zip codes for this city. However, if you enter in a full street address, the google maps API will always have the zip code in the API.

So to access the google maps API, the URL is, Cross Bay Blvd, Queens, NY 11414

So the hyperlink above brings us to the google maps API where the address is 159-40 Cross Bay Blvd, Queens, NY 11414. This represents the address of a McDonalds in Queens, New York.

The generic link to access the google maps API page of any location is,

After the word address, you simply put in the location that you want to enter. This can be any type of location such as a state or a city in a state or a full street address. You can really put in any address. But as stated before, the google maps API will only return a zip code for a full street address or a city that only has one zip code.

Using this base URL, we can have a user enter in an address, concatenate it to the above URL, putting in the address in the URL, retrieve the page with PHP, decode the JSON data with PHP, and then extract the data that we want from the API. As said before, we will extract the zip code for the address. The zip code is in 5-digit format without the 4-digit extension.

So let's go on to the code necessary to do this. This is shown below. We'll separate it into blocks.

First Block of PHP Code

So the first block of PHP code does the majority of the work as far as PHP code is concerned.

It takes the address from the form that a user has entered and puts into the URL representing the google maps API. It then retrieves the page, decodes the JSON data, and extracts the zip code from the google maps API.

So we'll go through the code above.

So we create a PHP variable named $location. We use the PHP superglobal array, $_POST to extract the data from the text box with the address that the user has entered in. So now we have the address that the user has entered in.

We then create an if statement so that we can know whether the data has been entered into the text box or not. We do not want the following code to run if the text box is empty.

We then create the a $url variable. This holds the URL of the google maps api for the address entered in. We have the base of the URL and then we have in the address ($location variable) by the concatenator. We put it in a urlencode() function so that if the user enters spaces in the address, this is accounted for.

We then create a variable called $jsoncontent. THis holds the all of the data produced in the goole maps api. Since this data is in JSON format, I call this data $jsoncontent.

Next, we decode the json data with the PHP json_decode() function. This converts the JSON data into a PHP array.

Lastly, we extract the zip code from this google maps API data.

Since the zip code is found under the first element of results, this is located in ['results'][0]. Under results, it is found in address_components in the 8th element of address_components, which is ['address_components'][7] (this is because arrays start at the 0th element). Then the zip code is found in the long_name element.

We create the PHP variable $zipcode and set them equal to this value.

Be aware, though, that depending on what state you are dealing with, it may not be in the 8th element of the address_components tag in the data. For example, for California, the zip code will be found at the 7th element of address_components, which is ['address_components'][6]. So if you're just dealing with one locality, you can just tailor your code for that locality. However, if you are dealing with a variety of places, you may have to create if statements depending on the location. For example, if the $location variable has "CA" (California) to output the ['address_components'][6]. If the $location variable has "NY" (New York) to output the ['address_components'][7].

We just made this code very simple, so it doesn't have if statements checking where the location is. So if you type in a California address, you will get the postal code suffix instead of the 5-digit postal code.

So it's a little tricky but you may even argue that google maps API is not the best for getting zip codes of locations.


Next we have the HTML code that we need so that we can create a form where a user can enter the address into.

It's a very simple form that just has a text box for entering the address and a submit button for submitting the data through.

So here we have a form. Since we want the form to submit the data input into to this current page, we leave the action attribute empty. Therefore, the data entered into the form stays on this page. The method is POST, since we don't need it appended to the current page's URL. Instead we want it appended to the URL we have in the code. Using POST, therefore, is better than using GET.

We then create the text box and assign it the name location_entered. We set its value equal to $location, which is the PHP variable that stores the address that the user has entered. This is so that we can see the address that the user has entered into the text box even after the submit button is clicked.

We then have a submit button so that the form can be submitted to the server.

This concludes the HTML code.

Second Block of PHP Code

We then have our last block of PHP code, which is very simple, put underneath the text box.

The last block of PHP simply outputs the zip code for the address that has been entered in.

And this concludes how we can use the google maps API to get data in JSON format to get the zip code of the address.

HTML Comment Box is loading comments...