Wanting to get a variable from Telnet Server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
madbohem
Forum Newbie
Posts: 4
Joined: Fri Dec 26, 2008 9:03 pm

Wanting to get a variable from Telnet Server

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Wanting to get a variable from Telnet Server

Post 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.
madbohem
Forum Newbie
Posts: 4
Joined: Fri Dec 26, 2008 9:03 pm

Re: Wanting to get a variable from Telnet Server

Post 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.
madbohem
Forum Newbie
Posts: 4
Joined: Fri Dec 26, 2008 9:03 pm

Re: Wanting to get a variable from Telnet Server

Post by madbohem »

btw i am still open to a more elegant, pull the default value, from the form answer.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Wanting to get a variable from Telnet Server

Post 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];
madbohem
Forum Newbie
Posts: 4
Joined: Fri Dec 26, 2008 9:03 pm

Re: Wanting to get a variable from Telnet Server

Post by madbohem »

my version of php is 5.2.7 off of a wamp server.

and that worked, thank you very much.
Post Reply