How I can get visitor latitude and longitude using geolocation API using PHP/curl (no JavaScript)?
one text
I need to get the latitude and longitude of the visitor of the website using PHP code using Google Geolocation API.
I have already developed the code, but it is showing the lat and long of the server where the website resides and not showing the lat and long of the visitor.
See below the code I have written to get lat and long.
$city = $state = $country = "";
$params = array();
$ch = curl_init('https://www.googleapis.com/geolocation/v1/geolocate?key='.$api_key);
$data_string = '';
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$result = json_decode($result, true);
/*
$result = array(
'location' => array(
'lat' => 23.0004983,
'lng' => 72.5323024,
)
);
*/
Source