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.
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,
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $query = @unserialize(file_get_contents('http://ip-api.com/php/')); if($query && $query['status'] == 'success'){ echo 'Your City is ' . $query['city']; echo '<br />'; echo 'Your State is ' . $query['region']; echo '<br />'; echo 'Your Zip Code is ' . $query['zip']; echo '<br />'; echo 'Your Coordinates are ' . $query['lat'] . ', ' . $query['lon']; } |
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:
- Auto Add Country Code in Input using JavaScript
- Increase PHPMyAdmin Import Size Ubuntu and XAMPP
- Login with Google Account using PHP Step by Step
- How to Embed PDF in Website Using HTML
Happy Coding..!
[…] Get Current City State and Zip Code Using PHP […]
[…] Get Current City State and Zip Code Using PHP […]
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
[…] Get Current City State and Zip Code Using PHP […]