All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

PHP Get Current Location Latitude Longitude

Last Updated : Mar 11, 2024

PHP Get Current Location Latitude Longitude

In this article we will show you the solution of PHP get current location latitude longitude, now we used a PHP extension named cURL here to get the current location latitude and longitude. Let us understand the cURL first.

cURL: cURL stands for Client URL and is a library that lets you make HTTP requests.

cURL is a PHP extension allowing us to receive and send information via the URL syntax.

By doing so cURL makes it easy to commination between different websites and domains.

We also used the json_decode() function to convert objects to JSON to PHP.

Step By Step Guide On PHP Get Current Location Latitude Longitude :-

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> php get current location latitude longitude </title>
    <link rel="stylesheet" href="">
    <h1 style = "color : rgb(113, 221, 113) ;"> Welcome to TalkersCode </h1>
    <h3> php get current location latitude longitude </h3>
echo "<pre>" ;
print_r($_SERVER['REMOTE_ADDR']) ;
echo "</br>" ;
echo gethostbyname('’) ;
$ip_val = curl_init(''.$ip) ;
curl_setopt($ip_val, CURLOPT_RETURNTRANSFER, true) ;
$json_vlue = curl_exec($ip_val) ;
curl_close($ip_val) ;
$ip_result = json_decode($json_vlue, true) ;
echo "<pre>" ;
print_r($ip_result) ;
<table border = "1">
        <th> IP Address </th>
        <th> country </th>
        <th> city </th>
        <th> latitude </th>
        <th> longitude </th>
        <td> <?php echo $ip_result['ip'] ; ?> </td>
        <td> <?php echo $ip_result['country'] ; ?> </td>
        <td> <?php echo $ip_result['city'] ; ?></td>
        <td> <?php echo $ip_result['latitude'] ; ?> </td>
        <td> <?php echo $ip_result['longitude'] ; ?> </td>
  1. First, we write <! DOCTYPE html> to mention the version of the HTML file
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Attach an external CSS file using <link> tag
  5. <h1> tag used to add heading here and also adding the inline CSS here.
  6. Write <?php to write php function and close tag with ?>
  7. At first using the super global variable $_SERVER[‘REMOTE_ADDR’] that returns IP address of the user
  8. Echo the gethostbyname() of to get any host ip address.
  9. Now getting the geolocation with the country using with curl_init() function that initialize new session and $ip variable with the $_SERVER[‘REMOTE_ADDR’]
  10. Then use the curl_setopt function to set the option for the cURL session
  11. $json_vlue with the curl_exec() to get the data in JSON format
  12. Now close the session with curl_close()
  13. To decode the JSON value to php value we used json_decode here
  14. Print_r to display the $ip_result
  15. Now create a table with the headings of IP address, country, city, longitude, latitude, etc. Also, set the border to 1 to add a border to the table.
  16. Using echo to print the required JSON data in a form of a table

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to get current location latitude longitude using php.

I hope this tutorial on php helps you. I hope this article on PHP get current location latitude longitude helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪