Skip to content

Instantly share code, notes, and snippets.

@tored
Created November 7, 2024 14:00
Show Gist options
  • Save tored/90d9ce9a5ea26b6d369ff162a43893ad to your computer and use it in GitHub Desktop.
Save tored/90d9ce9a5ea26b6d369ff162a43893ad to your computer and use it in GitHub Desktop.
function execute(string $cmd, ?string $stdin = null): array
{
$proc = proc_open($cmd, [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes);
if ($stdin !== null) {
fwrite($pipes[0], $stdin);
}
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$exit = proc_close($proc);
return [$stdout, $stderr, $exit];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment