How to get Geolocation using PHP-cURL from IP Address ? Last Updated : 17 Jun, 2021 Comments Improve Suggest changes Like Article Like Report Geolocation refers to the identification of the geographical location of a user or computer device. In this article, we will create a web page where the user can enter the IP Address of any device, and then the server will provide Geolocation of the IP address fetching the following details using the IP Geolocation API. Continent NameCountry NameCountry Alpha-2 CodeCountry Alpha-3 CodeCountry Numeric CodeCountry International Call Prefix CodeCurrency CodeLatitudeLongitude Approach: Call API via HTTP GET request using cURL in PHP.Convert API JSON response to array using PHP json_decode() function.Retrieve IP data from API response. Example: The following code gets the location from IP address using PHP cURL. PHP <?php if(isset($_POST['submit'])) { $userIP = $_POST['ip']; $apiURL = 'https://api.ipgeolocationapi.com/geolocate/'.$userIP; $ch = curl_init($apiURL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $apiResponse = curl_exec($ch); curl_close($ch); $ipData = json_decode($apiResponse, true); if(!empty($ipData)){ $continent = $ipData['continent']; $country_code_alpha2 = $ipData['alpha2']; $country_code_alpha3 = $ipData['alpha3']; $country_name = $ipData['name']; $country_code_numeric = $ipData['country_code']; $international_prefix = $ipData['international_prefix']; $currency_code = $ipData['currency_code']; $latitude = $ipData['geo']['latitude']; $longitude = $ipData['geo']['longitude']; echo 'Continent Name: '.$continent.'<br/>'; echo 'Country Name: '.$country_name.'<br/>'; echo 'Country Alpha-2 Code: '.$country_code_alpha2.'<br/>'; echo 'Country Alpha-3 Code: '.$country_code_alpha3.'<br/>'; echo 'Country Numeric Code: '.$country_code_numeric.'<br/>'; echo 'Country International Call Prefix Code: ' . $international_prefix.'<br/>'; echo 'Currency Code: '.$currency_code.'<br/>'; echo 'Latitude: '.$latitude.'<br/>'; echo 'Longitude: '.$longitude; } else{ echo 'Not a valid IP'; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>Get Location</title> </head> <body> <h1>Get Location Using IP Address</h1> <form method='post' enctype='multipart/form-data'> <label>Give IP address for check location</label> <input type='text' name='ip' /> <input type='submit' value='Submit' name='submit' /> <a href="index.php">Reset</a> </form> </body> </html> Output: Valid IP address: When the user enters valid IP address. Invalid IP address: When the user enters an invalid IP address in the input text control. Comment More infoAdvertise with us Next Article How to get Geolocation using PHP-cURL from IP Address ? A atul07 Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-cURL PHP-Questions +1 More Similar Reads How to get client IP address using JavaScript? Knowing a client's IP address can be useful for various purposes like personalizing content, tracking user activity, or offering location-based services. However, JavaScript running in the browser doesnât have direct access to this information due to security reasons. Using External APIs to Get the 3 min read How to get city name by using geolocation ? In this article, we will learn how to get the city name of the user using reverse geocoding. We will be using a 3rd party API service provider(Location IQ) to help us to convert the latitude and longitude we receive into an address. Steps: Get user coordinates. Get city name. Note: A LocationIQ acco 3 min read How to Promisify geolocation API to get current position using JavaScript ? In this article, we are going to use Promisify geolocation API into Promise-based API. Prerequisite: JavaScript Promise Approach: As we know the navigator.geolocation.getCurrentPosition is a callback-based API, so we can easily convert it into a Promise-based API. To promisify the geolocation API, 3 min read How to get cookies from curl into a variable in PHP ? The cURL standing for Client URL refers to a library for transferring data using various protocols supporting cookies, HTTP, FTP, IMAP, POP3, HTTPS (with SSL Certification), etc. This example will illustrate how to get cookies from a PHP cURL into a variable. The functions provide an option to set a 2 min read How to get IP Address of clients machine in PHP ? An IP address is used to provide an identity to a device connected to a network. IP address stands for Internet Protocol address. An IP address allows to track the activities of a user on a website and also allows the location of different devices that are connected to the network to be pinpointed a 2 min read Like