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 ? 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? Imagine your computer as your apartment. Just like each apartment has a unique address for receiving mail, your computer has an IP address that helps it receive information from the internet. This IP address acts like a label that identifies your specific device on the vast network of computers, ens 2 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 How to determine the user IP address using node.js ? Node.js is an open-source, back-end JavaScript runtime environment that runs on the web engine and executes JavaScript code. There are various platforms such as Windows, Linux, Mac OS  where Node.js can run. The Domain Name System is a hierarchical and decentralized naming system for computers etc t 2 min read How to get complete current page URL in PHP ? In PHP. there is a superglobal variable that can provide you with the URL of the current working page. The $_SERVER is an inbuilt variable that can access the page URL, after that, you have to check that the URL is following which type of HTTP protocol. What is Superglobal? Superglobals are already 1 min read How to use cURL to Get JSON Data and Decode JSON Data in PHP ? In this article, we are going to see how to use cURL to Get JSON data and Decode JSON data in PHP.cURL: It stands for Client URL.It is a command line tool for sending and getting files using URL syntax.cURL allows communicating with other servers using HTTP, FTP, Telnet, and more.Approach:We are goi 2 min read How to Pass JSON Data in a URL using CURL in PHP ? In this article, we will see how to pass the JSON Data in a URL using CURL in PHP, along with understanding the different ways for passing the data in a URL through the illustrations. The cURL stands for client URL, which allows us to connect with other URLs & use their responses in our code, i. 7 min read How to generate a drop down list of timezone using PHP ? The timezone_identifiers_list() function is used to generate a dropdown list of timezone with PHP. This function is used to return an indexed array containing all the timezone identifiers. The datetimezone object is sent as a parameter to the timezone_identifiers_list() function and it returns an in 3 min read Like