Home » Get Current City State and Zip Code Using PHP

Get Current City State and Zip Code Using PHP

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 (ip-api.com)

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

Note:

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,

$ip = "YOR_IP_ADDRESS";
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));

Output:

Your City is YOUR_CITY_NAME
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?
YesNo

2 thoughts on “Get Current City State and Zip Code Using PHP”

  1. Pingback: How to Get Client Hardware Information Using PHP - PHPCODER.TECH

  2. Pingback: isset vs empty vs is_null in PHP With Example - PHPCODER.TECH

Leave a Reply

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