0

I'm trying to get the location of a device at the time of loading a web page and passing that through to two variables in PHP (lat, long) so it can be manipulated further down the script. Currently, I have the data entered manually by tasker through the URL, but I'd like to have it available in a normal web page too without having to put the data into the URL by hand each time.

I was trying to use the getCurrentPosition() JavaScript to do this, but I can't find a way to get it back into the PHP. I have tried to write two separate files but all I seem to get back from the file is the JS script because it has not yet processed the file. So not the actual data itself.

PHP File:

$arrContextOptions=array(
    "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);  

ob_start();

$location = file_get_contents('https://myfakeurl.com/getlocation.php', false, stream_context_create($arrContextOptions));

$GPSCoord = explode(",", $location);
$coord['lat'] = trim($GPSCoord[0]);
$coord['long'] = trim($GPSCoord[1]);


echo "Longitude: " . $coord['long'] . "<br>";
echo "Lattitude: " . $coord['lat'] . "<br>";

?>

and JS file:

<p id="coord"></p>

<script>
var x = document.getElementById("coord");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.innerHTML = position.coords.latitude + "," + 
    position.coords.longitude;
}

getLocation()
</script>

The JS needs to run on the devices too which is part of the problem otherwise you don't get the correct GPS coords

How can I make this work? I feel like this is an Alice in Wonderland rabbit hole...

1 Answers1

0

The js code will not run if you use php's file_get_contents function, because of javascript is client side language. It's necessary to open the page from browser to run javascript. You can place javascript code on loaded webpage, catch location data and send to php with ajax. Try something like this

backend.php

<?php
if(isset($_POST['lng'], $_POST['lat'])) {
    echo $_POST['lng'].':'.$_POST['lat'];
}
?>

frontend.html

<html>
<head>
<meta charset="utf-8">
</head>
<body>
    <div id="response">
    </div>
<script>
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(sendPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function sendPosition(position) {
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "http://fakeurl/backend.php", false);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.send("lng="+position.coords.longitude+"&lat="+position.coords.latitude); 

    document.getElementById('response').innerHTML = xhttp.responseText;
}  

getLocation();
</script>
</body>
</html>
  • I get an error saying: frontend.php:15 Uncaught ReferenceError: xhttp is not defined at sendPosition (frontend.php:15) If I run the backend.php file... I get no output at all – Nathaniel Baker Sep 27 '18 at 06:29
  • Oops sorry, I have just edited the code. I forgot to declare xhttp – Shota Noniashvili Sep 29 '18 at 07:29
  • I get no response out of this... atleast the error is gone now though haha – Nathaniel Baker Sep 30 '18 at 08:15
  • If you want to get response in frontend.html you need to get backend.php-s response with xhttp.responseText. I have edited again please see working code – Shota Noniashvili Sep 30 '18 at 11:58
  • I want to get a response out of backend.php...as all my other scripts are in that part of the page.... and I want to add the output from the PHP code, but need to get the GPS location from the JS. p.s. i'm not getting any response out of the revised code either. – Nathaniel Baker Oct 01 '18 at 12:08