Search code examples
phpgeoip

getCity/state from IP address using PHP


I am working on a client project, where he needs the city/state of the user by taking his ip address.

1) he has purchased maxmind GeoIP2 Precision, but no idea how to implement the code in php. documentation says some instruction to download composer.exe, when i downloaded it, it is asking me to download php.exe then after download php.exe, i tried to install composer.exe then it tried to install composer.phar and gave me this error

"Some settings on your machine make Composer unable to work properly. Make sure that you fix the issues listed below and run this script again:

The openssl extension is missing, which means that secure HTTPS transfers are impossible. If possible you should enable it or recompile php with --with-openssl "

Please suggest me the easiest way to get the city/state from IP address using PHP.

Thanks


Solution

  • This API will get your location details. Try this

        <?php
        $ip = '168.192.0.1'; // your ip address here
        $query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
        if($query && $query['status'] == 'success')
        {
            echo 'Your City is ' . $query['city'];
            echo '<br />';
            echo 'Your State is ' . $query['region'];
            echo '<br />';
            echo 'Your Zipcode is ' . $query['zip'];
            echo '<br />';
            echo 'Your Coordinates are ' . $query['lat'] . ', ' . $query['lon'];
        }
        ?>
    

    Here are the outputs you can use. Call them using the key;

        <?php
        array (
          'status' => 'success',
          'country' => 'COUNTRY',
          'countryCode' => 'COUNTRY CODE',
          'region' => 'REGION CODE',
          'regionName' => 'REGION NAME',
          'city' => 'CITY',
          'zip' => 'ZIP CODE',
          'lat' => 'LATITUDE',
          'lon' => 'LONGITUDE',
          'timezone' => 'TIME ZONE',
          'isp' => 'ISP NAME',
          'org' => 'ORGANIZATION NAME',
          'as' => 'AS NUMBER / NAME',
          'query' => 'IP ADDRESS USED FOR QUERY',
        );
        ?>
    

    This will not be reliable for mobile. If you are on mobile, you can use the following javascript

        <script>
        var x = document.getElementById("demo");
        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition);
            } else {
                x.innerHTML = "Geolocation is not supported by this browser.";
            }
        }
        function showPosition(position) {
            x.innerHTML = "Latitude: " + position.coords.latitude + 
            "<br>Longitude: " + position.coords.longitude; 
        }
        </script>
    

    This will use the gps built in to the device and get the cordinates. Then you can cross reference with a database filled with cords. (thats one option, and thats what i do).

    You can find a city lat lng databases online if you search and if thats what you chose to do for mobile.