To Get Current City, State, and Zip Code Using PHP using PHP, we use IP Geolocation API to get the data of Current City, State, and Zip Code as well as coordinates.

This location API is useful when you develop any location or tracking-based projects using PHP. To get the data we use file_get_contents() function and for each loop to get all the data at once.

Get Current City, State, and Zip Code Using PHP
Get Current City, State, and Zip Code Using PHP

Introduction to IP Geolocation API (

This API provides some major things as follows,

  • Fast, accurate, reliable
  • Free for non-commercial use, no API key required
  • Easy to integrate, available in JSON, XML, CSV, Newline, PHP
  • Serving more than 1 billion requests per day, trusted by thousands of businesses

Here is complete source code to Get Current City, State, and Zip Code Using PHP using PHP,

The source code is also an answer of below queries which is best suitable,

  • get current location address in PHP
  • get city name from IP address using PHP
  • get location from IP address PHP script
  • how to get country name from IP address in PHP


If you did not provide an IP address at the end of the API URL then it takes the current location details. And if you provide the IP address that looks like this,

$query = @unserialize(file_get_contents(''.$ip));


Your State is STATE_NAME
Your Zip Code is ZIP_CODE
Your Coordinates are LOCATION_CORDINATE

Here is the complete code to get all the data of your location, I just show some data on the example but you can retrieve more data from the same.

I think this is the complete solution to get the data because it is also provide unlimited request to the user.

Also Check:

Happy Coding..!

Was this article helpful?

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

4 thoughts on “Get Current City State and Zip Code Using PHP”
  1. i didnt enter the ip so it has to get my current location but it didnt get my current location it gave me other location and other lat long

Leave a Reply

Your email address will not be published. Required fields are marked *