2014-11-25 3 views
-1

PHP 스크립트와 C++ 프로그램 사이에서 매개 변수를 전달하려고합니다. 내 PHP 스크립트는 당신이 도움을 줄 수, 나는 내 C++ 프로그램이 나에게 문자열을 반환 할 (하지만 난 정말 그것을 수행하는 방법 아무 생각이) 다음이C++에서 PHP 스크립트로 문자열 전달하기

<?php 
    $ip = $_GET["q"]; 
    $array = str_getcsv($ip); 
    foreach($array as $line){ 
     exec("./a.exe", $line, $output); 
     echo $output; 
    } 
?> 

처럼 보인다?

+0

PHP는'a.exe'의 표준 출력을 원합니다. –

답변

1

당신, 정말 아주 간단합니다 이것에 대해 올바른 방법을거야 ...하지만 (실행에 의해 넣어 문자열을 받고) 귀하의 질문에 대답하지 않도록 :

int main (int argc, char **argv) 
{ 
    printf("This is a line\n"); 
    puts("Another line"); 
    stc::cout << "Last bit"; 
    return 0; 
} 

코드 컴파일 할 때 exec을 통해 실행할 수 있습니다. 이 함수의 서명은 in the docs 찾을 수 있습니다

string exec (string $command [, array &$output [, int &$return_var ]]) 

그것이 문자열 (명령 출력의 마지막 라인 인)를 2 번째의 인수로 (출력의 각 행을 나타내는) 배열을 할당하고 있음을 알려줍니다 종료 코드가 있으므로, 세 번째 인수에 할당됩니다

$last = exec('./a.exe', $full, $status); 
if ($status != 0) { 
    echo 'Something didn\'t go quite right'; 
} else { 
    echo 'Last line of output was: ', $last, PHP_EOL, 
     'The full output looked like this: ', PHP_EOL, 
     implode(PHP_EOL, $full); 
} 

은 실행중인 프로그램과 실제 상호 작용을 사용하려면 exec, shell_exec, passthru 그 함수를 도랑해야합니다. 그들은 단지 일에 종사하고 있지 않습니다. 아마도 실제로 원하는 것은 the proc_open function입니다. 이렇게하면 프로그램에서 사용하는 stderr, stdinstdout 스트림에 액세스하고 stdin에 쓸 수있어 효과적으로 프로세스와 상호 작용할 수 있습니다.

$descriptorspec = array(
    0 => array("pipe", "r"), 
    1 => array("pipe", "w"), 
    2 => array("file", "/tmp/error-output.txt", "a") 
); 

$process = proc_open('./a.exe', $descriptorspec, $pipes); 
if (!is_resource($process)) 
    exit(1);//error 
foreach ($array as $line) { 
    fwrite($pipes[0], $line."\n");//added the EOL, just in case 
    fflush($pipes[0]);//flush 
    usleep(100);//wait for a bit 
    //unsure about this bit, though, perhaps fread is a better choice 
    $output = stream_get_contents($pipes[1]);//get output 
    fflush($pipes[0]);//reminds me a bit of fflush(stdin) though. So I'd probably leave this out 
} 
array_map('fclose', $pipes);//close streams 
proc_close($process); 

이 당신을 위해 작동하는 경우, 참조 문서를 보면, 일부 proc_open 예제를 찾을 : 워드 프로세서에 주어진 첫 번째 예제를 기반으로

, 이것은 총 가치가있다. 얼마 전, stderr 스트림에 무언가가 기록 될 때까지 명령을 자동으로 반복하는 PHP 스크립트를 작성했습니다. 나는 코드를 github에 올려 놓았습니다. 그럴 가치가 있습니다. 나는 또한 소스에 링크했습니다. this related question

관련 문제