Page 1 of 1

PHP and Curl :telnet

Posted: Wed Oct 06, 2010 2:49 pm
by Jamescgr
Dear Forum,

Let me set the scene, I am a general techie all-rounder but don't have much PHP experience. I understand some code, but I am not a programmer. Where required I muddle through with the help of Google and forums such as this. Point being; I need help, and maybe a little bit more than most.

What I am trying to achieve is the following: -

I have a mobile VoIP softphone that sends an httprequest to a webserver to obtain a balance for the user account. The softphone app sends a pre-defined URL to the webserver with some user detail variables within it, for example - http://xxx.xxx.xxx.xxx/creditcheck.php? ... e&password The PHP script then needs to make a call to an API that is accessed using telnet to port 5812 and once connected via telnet it has to carry out a query to the API that then queries a DBase database, that query returns the user balance which then needs to echo to the webpage for the softphone to read it.

The issue that I have at the moment is I cannot get the PHP page to send any commands to the API once the connection is established via telnet.
I initially tried to do this use fsocket but the API sends back some telnet based info once the connection established, which is supposed to set the telnet client into the right state, but causes the PHP to hang and eventually bomb out. Therefore I done some thinking and found that PHP can send shell based commands to the OS using the exec function, and therefore I have been investigating using curl and its telnet protocol. Because curl is able to emulate the telnet responses I am now able to establish a connection to the API using telnet port 5812. I now have run into another issue where the connection is actually established but I am unable to send any details to the API.

I need to authenticate into the API first of all, so I have created the following PHP code: -

<?php

exec('curl -v telnet://xxx.xxx.xxx.xxx:5812');
exec('username');
exec('password');
echo exec('api_query_with_user_variables');

?>

As I have said, the connection is established as if I leave the webpage long enough the API times out the connection because no username and password is entered after 3 minutes, once it times out the other exec commands are sent and I get back an echo to the webpage telling me the api_query_with_user_variables is not a valid command from the Linux OS.

So from what I have worked out when the connection to the API via telnet is established no further exec commands are sent until the API closes, as it seems until this process finishes the PHP script will not issue any further commands.

What I need the PHP to do is send the further exec commands without waiting for the API to quit, as it is the API that I want to receive these commands. I may be trying to use this function incorrectly, but this is as far as my knowledge has let me go. I have also tried other functions such as system to send the curl command but I get the same results.

I have also looked at speaking directly to DBase Db via PHP but it required to install extra elements to the PHP install and I cannot risk affecting the live system. Also I don't want to install another webserver and give it access to the Base DB files as this would create a security risk with this system, and would go against our internal IT policies. I have also looked at using curl directly in PHP but again this required extra elements.

Therefore I need to complete this with the existing PHP setup that I have, and therefore would like to look at the exec based solution, or something similar. So can anyone see an issue with what I am doing, and maybe shed some light on how I continue to send commands to the API once the telnet connection is established, and possibly give me some code examples on how to do this?

Much appreciated :D

Re: PHP and Curl :telnet

Posted: Wed Oct 06, 2010 4:07 pm
by Weirdan
As manual says, you need to pass data to curl's stdin and read responses from curl's stdout. To do this from PHP you need proc_open() (might not be available on your PHP installation though) .

Alternatively you may try something like this: http://www.dali.net.nz/Telnet.class.php.txt