Link to home
Start Free TrialLog in
Avatar of projects
projects

asked on

GEO lon/lat reliable server and code needed

Currently, I am using freegeoip.net to set the geo lat/lon of a server when it starts up using a bash script.

Bash code for my function;

function get_set_geo()
{
    # Get GEO LAT/LON

    LATLON=$(wget -q -O - http://freegeoip.net/xml | awk -F'<|>' '/Latitude|Longitude/ {print $3}')

    LAT=$(cut -f1 -d" " <<< $LATLON)
    LON=$(cut -f2 -d" " <<< $LATLON)

    echo "Latitude: " $LAT
    echo "Longitude:" $LON

    $CURLSEC -F function=set_geo -F geo_lat=$LAT -F geo_lon=$LON
}

Open in new window


The variable contents are stored to Mysql
geo_lat       decimal(10,8)       No       None       NULL       
geo_lon       decimal(11,8)       No       None       NULL

I was told in another question that this service is not very accurate so I would like to use another one.

I also found http://www.datasciencetoolkit.org/developerdocs#ip2coordinates which looks very interesting to me because it seems to also add the ability to use address information. I actually have address information for the server in it's database record on mysql. In other words, I could use it's current public IP and it's address to feed to this service.
The service must allow free use, even for commercial use. The server will only ever connect once to get it's geo lat/lon unless it has been moved. If it has been moved, it's db lat/lon settings will be defaulted back to 0.00000000 so the code is run again.

This is the next part of the question, I need code to complete this.

The above code connects to a php app which in turn reads/writes to the database. I need to add code to the bash function above which will check to see if lat/lon are 0.00000000. If so, then run the function, if anything else, such as it's already been set, then do not run the function and continue the script.

Currently, my code only sets and doesn't know if the lat/lon was already set. Here is the php side.

                elseif ($_POST['function'] === 'set_geo') {
                                $sql = 'UPDATE clients SET
                                geo_lat = "' . $_POST['geo_lat'] . '", geo_lon = "' . $_POST['geo_lon'] . '" WHERE id = ' . $clientid ;
                                echo "\n SQL: $sql";
                                mysql_query($sql) or die('Query failed: ' . mysql_error());
                 }

Open in new window


What I need are to check the record of this clientid, see if it's lat/lon have been set. If set, those records will contain lat/lon coordinates but if not set, they will have 0.00000000 0.00000000 which means, let's set the coordinates.

NOTES
CURLSEC is a variable which contains the entire curl connection string to authenticate and communicate with php. The script already knows it's name/password.

The $clientid is determined by php so that the proper record will be chosen when the server connects to php.

In other words, authentication and figuring out which record to write to are already in the code and not needed.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

The service must allow free use, even for commercial use...
Really?  Why?  If the data has any economic value to you, you might consider the value versus the (usually modest) cost of using a professional service.

I've used MaxMind with good results.  Some of their data is free and some is paid.  The paid service is better (you get what you pay for) but you might be satisfied with the free data.   This article describes the way to consume MaxMind data,
https://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_3437-IP-Address-to-Country-in-PHP.html
Avatar of projects
projects

ASKER

You are making assumptions. I give back in other ways and when I start using something on a regular basis, I usually always contribute. Not sure what that has to do with the question however as this is simply what I need at this time.

Also, mine is a two part question, one being the service, second being that I need someone to share some code with me :)
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I can't take a chance on code that 'might' work since the script is in production :). I am happy to test it since it's just a function so should not break anything.

Not sure what you need by test data, there isn't any and is why I am trying to put this together so that I can have that. Right now, the code is only what you see.


As for deprecated, thank for mentioning it each time you reply to questions I post because I need to remember to get this final part updated at some point. This will be done last however.
I would need to know what to put in the bash script as well to run or don't run with a new, more reliable free service.