2013-01-03 2 views
2

명령 행 인수가 아닌 스트림에서 입력을받는 특정 명령 행 프로그램이 있습니다 (예 : scanf를 사용하여 입력을받는 C 프로그램).이 함수와 어떻게 상호 작용할 수 있습니까? PHP를 사용하는 앱? exec(), shell_exec()는 명령 행 인수를 통한 입력이 아니기 때문에 여기서 도움이되지 않습니다. 필자는 PHP 스크립트와 커맨드 라인 프로그램 사이에 일정한 입/출력 연결이 필요하므로 사용자가 PHP에 입력을 제공 할 수 있습니다. PHP는이를 프로그램에 전달한 다음 사용자에게 표시되는 출력을 얻습니다.PHP와 명령 행 프로그램의 상호 작용

답변

2

이 경우 proc_open을 사용할 수 있습니다. 해당 페이지에서

예 :

<?php 
$descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from 
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to 
    2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to 
); 

$cwd = '/tmp'; 
$env = array('some_option' => 'aeiou'); 

$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env); 

if (is_resource($process)) { 
    // $pipes now looks like this: 
    // 0 => writeable handle connected to child stdin 
    // 1 => readable handle connected to child stdout 
    // Any error output will be appended to /tmp/error-output.txt 

    fwrite($pipes[0], '<?php print_r($_ENV); ?>'); 
    fclose($pipes[0]); 

    echo stream_get_contents($pipes[1]); 
    fclose($pipes[1]); 

    // It is important that you close any pipes before calling 
    // proc_close in order to avoid a deadlock 
    $return_value = proc_close($process); 

    echo "command returned $return_value\n"; 
} 
?> 

출력 :

Array 
(
    [some_option] => aeiou 
    [PWD] => /tmp 
    [SHLVL] => 1 
    [_] => /usr/local/bin/php 
) 
command returned 0 
+0

감사합니다. 그게 도움이 :) –