I have a PHP class that must launch a Java main as a separate process.
The execution must be synchronous and after its end I must read the stderr to handle and log potential errors.
I see that the most popular techniques to run separate processes from PHP are exec() and proc_open(). However, I do not understand which I should use and how.
Exec(), being synchronous and very easy to use, should be the best choice for me. However, from what I see here on StackOverflow: PHP StdErr after Exec(), it seems that the only way to get the stderr is to explicitly create a file for it. It does not seem to me like an elegant solution.
On turn, proc_open seems a really powerful tool, and it does allow direct, separate access to stdin, stdout and stderr. For what I need to do, the code should be something like this:
$proc = proc_open($cmd,[2 => ['pipe','w']],$pipes);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
proc_close($proc);
$error = var_dump($stderr)
The problem is proc_open is asynchronous, so if I get it right, I can not be sure that when I run stream_get_contents I will get any relevant message (errors might occur later!).
What should I do?