Page 1 of 1

Wanting to get a variable from Telnet Server

Posted: Fri Dec 26, 2008 9:19 pm
by madbohem
I have been running FlightGear on my computer, which is an open source flight simulator.

In the set up you are able to output FlightGear's information through a built in Telnet Server. (http://localhost:5500/), which gives you a tree structure and you can actually input different things into the program through a web browser, potentially even flying a plane through this interaction.

Through the tree structure you can access all sorts of things and change the values (http://localhost:5500/position/latitude-deg) and the internal Telnet Server is generating a form written like this:

<html lang="en"><head>

<title>FlightGear - /position/latitude-deg</title>
</head><body>
<form method="get" action="/position/latitude-deg"><b>/position/latitude-deg</b> = <input name="value" size="15" value="37.61474135" maxlength="2047" type="text"><input value="update" name="submit" type="submit"></form></body></html>

What i want to do is to be able to interact with FlightGear independently from its served up pages, i.e., I want to access what it serves up from my own page, using PHP, to get a variable from the form(s) it is generating.

Here is something i have tried and it fails, giving me an undefined index value:

<?php
$page = file_get_contents('http://localhost:5500/position/latitude-deg');

echo $page;

$mvalue = $_REQUEST['value'];
echo $mvalue;
?>

Thank you for any time and input in advance.

Re: Wanting to get a variable from Telnet Server

Posted: Sat Dec 27, 2008 12:16 am
by requinix
First off, telnet is a protocol like HTTP and FTP. It's normally on port 23 (HTTP is 80, FTP is 21).
The thing you're talking about is simply HTTP over a different port.

Code: Select all

$mvalue = $_REQUEST['value'];
echo $mvalue;
You're trying to get the submitted value before the form's even been submitted. If you ask me, that doesn't make sense.

Check that the form's been submitted and only then display the value.

Code: Select all

<?php
$page = file_get_contents('http://localhost:5500/position/latitude-deg');
 
echo $page;
 
if (!empty($_GET["submit"])) {
    $mvalue = $_REQUEST['value'];
    echo $mvalue;
}
?>
Note that the form is pointing to "/position/latitude-deg" on the server. If you are using that same form in your own PHP application, this script must also be located at /position/latitude-deg. If it isn't then it won't work.

Re: Wanting to get a variable from Telnet Server

Posted: Sat Dec 27, 2008 12:44 am
by madbohem
its not about the form being submitted. when you simply connect (http://localhost:5500/position/latitude-deg) flightgear (a flight simulator program) generates a form that can be used to change everything from latitude, longitude, and any other part of the program. the form appears with a default value in the submit box... and thats the value i want in my php variable. this default value corresponds to the location in the simulator. however, i figured out a solution to this just treating the html the telnet server generates as a string and parsing it out to get my default value.

$page = file_get_contents('http://localhost:5500/position/latitude-deg');
$mvalue = substr($page,223,11);
echo $mvalue

this is basically what i am doing now as a solution and that works fine for what i want. now i just need to make sure i only take the value i want.

Re: Wanting to get a variable from Telnet Server

Posted: Sat Dec 27, 2008 1:57 am
by madbohem
btw i am still open to a more elegant, pull the default value, from the form answer.

Re: Wanting to get a variable from Telnet Server

Posted: Sat Dec 27, 2008 2:11 am
by requinix
Unfortunately it's not valid XML, otherwise it'd make things easy. What version of PHP do you have?

The generic "use a regular expression" answer should suffice here.

Code: Select all

$page = file_get_contents('http://localhost:5500/position/latitude-deg');
preg_match('/value="([+-]?\d+(\.\d+)?)"/', $page, $matches);
 
echo "Current latitude is ", $matches[1];

Re: Wanting to get a variable from Telnet Server

Posted: Sat Dec 27, 2008 2:22 am
by madbohem
my version of php is 5.2.7 off of a wamp server.

and that worked, thank you very much.