Jump to content

Connecting to a Program


pbywater

Recommended Posts

i wanna try get php to execute a command to a process/application running on a linux box(same computer).

the application that is running is called asterisk.

 

asterisk uses a CLI, how i normally do this is:

Open a terminal

type in: asterisk -r <--- connects to the CLI

          reload <----- reloads the config files

          exit <-- exits the terminal/CLI

Link to comment
https://forums.phpfreaks.com/topic/52461-connecting-to-a-program/
Share on other sites

If you're trying to interactively use a program, by interactively, I mean send input and receive out butput via std in / out I'd use proc_open.  Here's an example where I've used proc open to run php through the cgi binary and pass it output from std in then return the output sent by the cgi binary in std out:

 

<?php
// ..
	$descriptorspec = array(
	   0 => array("pipe", "r"),  
	   1 => array("pipe", "w"),  
	);

	$pipes = null;

                // Open our process, HTTP_CONF_CGI_EXECUTABLE holds the path to our program.
                $process = proc_open(HTTP_CONF_CGI_EXECUTABLE, $descriptorspec, $pipes);

	// This is our input pipe, anythinhg we write to here will get fed to program.
                $pipeIn = $pipes[0];
                // This is our output pipe, anything the program writes will end up here.
	$pipeOut = $pipes[1];

                // Write the contents of the request to the cgi bianry
	fwrite($pipeIn, $this->request->getContent());

                // We're done with our input stream, so close it. 
                fclose($pipeIn);
        
                // Get the output.
	$content = stream_get_contents($pipeOut);

                // We're done with our output stream, so close it.
	fclose($pipeOut);

                // Close the process.
	proc_close($process);
?>

 

Best,

 

Patrick

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.