Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get data over HTTP with PHP on my server? #492

Closed
andreabazerla opened this issue Dec 18, 2016 · 11 comments
Closed

How to get data over HTTP with PHP on my server? #492

andreabazerla opened this issue Dec 18, 2016 · 11 comments
Labels

Comments

@andreabazerla
Copy link

I need help, please! I'm very confused.

How I can print on my server at address .../.../test.php data as ?lat=%LAT&lon=%LON with custom URL modality over HTTP with PHP?

(I will offer you a coffee, your app is great!)

Thanks!

@mendhak
Copy link
Owner

mendhak commented Dec 18, 2016

OK I haven't touched PHP in a long time but let me try to answer...

In CustomURL settings - http://yourserver/test.php?lat=%LAT&lon=%lon

In your PHP page, something like

<?php
    echo $_GET['lat']; 
    echo $_GET['lon'];
?>

Of course from there you'll need to write it to your file or database, the echo just prints it to screen and nobody will see that.

So for file, for example, you could do

file_put_contents("lookatthis.txt", $_GET['lat']." ".$_GET['lon'] , FILE_APPEND );

@andreabazerla
Copy link
Author

Yep, it's right but it doesn't work!
Where am I doing wrong?

GPSLogger for Android

http://.../test.php?lat=%LAT

test.php

<?php

    if (isset($_GET["lat"])) {
        echo $_GET["lat"];
    } else {
        echo "ERROR";
    }

?>

http://.../test.php

ERROR

@mendhak
Copy link
Owner

mendhak commented Dec 18, 2016

Wait - are you hitting the page directly in your own browser? You won't see anything that way because there's no querystring variables being sent. To see it working in your own browser, go

http://.../test.php?lat=1111&lon=2222

GPSLogger will be sending lat/lon to that URL but you won't be able to see that because the values are being echoed back to the app, not to yourself. You'll have to write some code to take those lat/lon values and store them somewhere like a file or a DB. Then you can test end-to-end.

@mendhak
Copy link
Owner

mendhak commented Dec 18, 2016

Try writing from the PHP page to a file

file_put_contents("lookatthis.txt", $_GET['lat']." ".$_GET['lon'] , FILE_APPEND );

Then hit the page from GPSLogger. Then look for a lookatthis.txt file

@andreabazerla
Copy link
Author

andreabazerla commented Dec 18, 2016

Oh it's works! I'm too confused.

Why I can't get lat and lon directly in test.php, to save into PHP variables and then to print them?

Thank you, @mendhak!

@mendhak
Copy link
Owner

mendhak commented Dec 18, 2016

Web pages are request-response. When you hit that page it looks at what querystring you sent, prints it to the page and responds with that page back to you. Similarly when GPSLogger adds its querystring, the response is sent back to GPSLogger.

So you can definitely save lat/lon to PHP variables, just that you have to also send them in the querystring. In your example you were hitting test.php but not sending any lat/lon.

If you hit

http://.../test.php?lat=1&lon=2

You should see it saved to variable then printed out to page

@andreabazerla
Copy link
Author

So what is the best solution to get lat, lon, etc with your app, in PHP to store them in a MySQL database and to print them in a web page in your opinion? I need some advices about it, because maybe I'm doing it wrong.

@mendhak
Copy link
Owner

mendhak commented Dec 18, 2016

You'll need two parts to this.

The first is a 'receiver.php' page which GPSLogger sends its data to every few minutes or whatever you've configured. In this page, write the values to a file or database.

The second is a 'display.php' - when you browse to this page it goes and queries the database or file and displays those values on the page.

If you're going to do a lot of logging, maybe consider a database instead of a file.

Might be worth mentioning - export-mike has written an API which can look at GPX files uploaded to Dropbox. So you'd have to enable Dropbox upload and your display page would query the Dropbox API instead.

@mendhak mendhak closed this as completed Dec 20, 2016
@andreabazerla
Copy link
Author

andreabazerla commented Dec 23, 2016

Thank you @mendhak, now it works!

I've created a PHP file that get latitude and longitude from your app over HTTP and insert them into my database online every n seconds.

try {
    $connnection = new PDO("mysql:host=XXX;dbname=XXX", "XXX", "XXX");
    $connnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $latitude = $_GET["lat"];
    $longitude = $_GET["lon"];

    if ($latitude != null && $longitude != null) {
        $sql = "INSERT INTO GPS (LATITUDE, LONGITUDE)
                VALUES ('" . floatval($latitude) . "', '" . floatval($longitude) . "')";
        $connnection->exec($sql);
    }
} catch(PDOException $e) {
    echo $e->getMessage();
}

@mendhak
Copy link
Owner

mendhak commented Dec 23, 2016

Hey that's great to know. Future searchers will benefit from this too, so thanks

@jacknab
Copy link

jacknab commented Feb 9, 2017

what is the full url string your using in the custom URL option?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants