I coded this to be able to update the position of my car live on the internet. I used netstumbler's scripting only to have an easy way to access GPS from PHP, this dosen't use the scanning functions at all, although it would be easy to add them (have it send ap location instead of all gps info). Requires the PHP ActiveScript extension to be installed. (
http://us2.php.net/manual/en/install...tivescript.php)
Netstumbler Script:
PHP Code:
//<?php // Have IDE see this as PHP and not HTML
set_time_limit(0);
$Data = array();
function OnGPSPosition ($Latitude, $Longitude, $Altitude) {
global $Data;
$Data['lat'] = (double) $Latitude;
$Data['long'] = (double) $Longitude;
$Data['alt'] = (double) $Altitude;
$Data['nogps'] = false;
update();
}
function OnGPSSpeed ($Speed) {
global $Data;
$Speed = (double) $Speed;
$Speed = $Speed * 1.150779;
$Data['speed'] = $Speed;
$Data['nogps'] = false;
update();
}
function OnGPSNoFix () {
global $Data;
$Data['nogps'] = true;
update();
}
function update () {
global $Data;
if (!isset($Data['lat']) || !isset($Data['long']) || !isset($Data['alt']) || !isset($Data['speed'])) {
$Data['lat'] = 0;
$Data['long'] = 0;
$Data['alt'] = 0;
$Data['speed'] = 0;
$Data['nogps'] = true;
}
$Data['time'] = time();
file_put_contents('GPSData', serialize($Data));
}
Run this from command line to update,
PHP Code:
<?php
chdir('C:\\Program Files\\Network Stumbler');
set_time_limit(0);
$LastTime = 0;
while (true) {
if (file_exists('GPSData')) {
$Data = unserialize(file_get_contents('GPSData'));
var_dump($Data);
if ($Data['time'] !== $LastTime) {
if ($Data['lat'] != 0 && $Data['long'] != 0) {
$cURL = curl_init();
curl_setopt_array($cURL, array(
CURLOPT_URL => "http://url.to/Updater.php?spd={$Data['speed']}&alt={$Data['alt']}&lat={$Data['lat']}&lng={$Data['long']}&tim={$Data['time']}",
CURLOPT_HEADER => false,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_TIMEOUT => 5,
));
$ret = curl_exec($cURL);
curl_close($cURL);
if ($ret === false) {
file_put_contents('CACHED_DATA', serialize($Data)."\r\n", FILE_APPEND);
}
}
$LastTime = $Data['time'];
echo "\r\n";
sleep(10);
}else{
echo "\r\n";
sleep(2);
}
}
}
Updater.php: puts data into database, put on a webserver
PHP Code:
<?php
include('SQL.inc');
if (isset($_REQUEST['spd']) && isset($_REQUEST['alt']) && isset($_REQUEST['lat']) && isset($_REQUEST['lng']) && isset($_REQUEST['tim'])) {
$SQL->query("INSERT INTO `data` (`Time`,`Latitude`,`Longitude`,`Altitude`,`Speed`) VALUES ('{$_REQUEST['tim']}','{$_REQUEST['lat']}','{$_REQUEST['lng']}','{$_REQUEST['alt']}','{$_REQUEST['spd']}')");
}
XMLData.php: Returns database contents as an XML document:
PHP Code:
<?php
include('SQL.inc');
header('Content-type: text/xml');
$XMLWriter = new XMLWriter();
$XMLWriter->openMemory();
$XMLWriter->setIndent(true);
$XMLWriter->setIndentString("\t");
$XMLWriter->startDocument('1.0', 'UTF-8');
$XMLWriter->startElement('GPS');
$XMLWriter->startElement('data');
$Row = $SQL->query("SELECT * FROM `data` ORDER BY `Time` DESC LIMIT 1")->fetch_assoc();
$XMLWriter->writeAttribute('alt', $Row['Altitude']);
$XMLWriter->writeAttribute('spd', $Row['Speed']);
$XMLWriter->writeAttribute('tim', $Row['Time']);
$XMLWriter->writeAttribute('cti', time());
$Result = $SQL->query("SELECT * FROM `data` ORDER BY `Time` DESC LIMIT 120");
while ($Row = $Result->fetch_assoc()) {
$XMLWriter->startElement('point');
$XMLWriter->writeAttribute('lat', $Row['Latitude']);
$XMLWriter->writeAttribute('lng', $Row['Longitude']);
$XMLWriter->endElement();
}
$XMLWriter->endElement();
$XMLWriter->endElement();
$XMLWriter->endDocument();
echo $XMLWriter->outputMemory();
SQL.inc : connects to SQL database
PHP Code:
<?php
$SQL = new mysqli('localhost', 'root', 'password', 'gps_db', 3306);
I have another file that uses the Google Maps API that downloads, parses and displays the XML data on a map, but it has too much of my site code mixed in to post right now.
If internet connection is unavaliable or lost, the updating script will serialize the $Data array and write it to a file every time the update fails. A script to unserialize the data, an write it to the database one you have access to an interent connection can asily be coded, I just haven't found it neccissary yet. I originally had it using file_get_contents(), but switched to using cURL in order to be able to set a short timeout so that it dosen't lock up if internet is lost. I plan on adding password protection to the update script later.