0

I need to test some php cli script which uses stdout, stderr and return error code.

  • exec seems that it does not return stderr.
  • system does not return stdout (only last line), stderr.
2

1 Answer 1

1

proc_open could be used.

File: script.php

echo 'Standart output'; //stdout

error_log('Error output'); //stderr

exit(1); //return

File: test.php

<?php

$descriptorspec = array(
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to
    2 => array("pipe", "w") // stderr is a pipe that the child will write to
);

$process = proc_open('php script.php', $descriptorspec, $pipes);
if (is_resource($process))
    {
    echo 'stdout: ' . stream_get_contents($pipes[1]) . PHP_EOL;
    fclose($pipes[1]);

    echo 'stderr: ' . stream_get_contents($pipes[2]) . PHP_EOL;
    fclose($pipes[2]);

    $return_value = proc_close($process);
    echo 'return: ' . $return_value . PHP_EOL;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.