您好,登录后才能下订单哦!
这期内容当中小编将会给大家带来有关shell_exec 中怎么捕获标准错误流,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
实际上Swoole提供的System::exec()行为上与PHP的shell_exec是完全一致的,我们写一个shell_exec的同步阻塞版本,执行后发现同样拿不到标准错误流输出的内容,会被直接打印到屏幕。
<?php
$result = shell_exec('unknown');
var_dump($result);htf@htf-ThinkPad-T470p:~/workspace/debug$ php s.php sh: 1: unknown: not found NULL htf@htf-ThinkPad-T470p:~/workspace/debug$
那么如何解决这个问题呢?答案就是使用proc_open+hook实现。
Swoole\Runtime::setHookFlags(SWOOLE_HOOK_ALL);
Swoole\Coroutine\run(function () {
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w"),
);
$process = proc_open('unknown', $descriptorspec, $pipes);
var_dump($pipes);
var_dump(fread($pipes[2], 8192));
$return_value = proc_close($process);
echo "command returned $return_value\n";
});使用proc_open,传入了3个描述信息:
fd 为 0 的流是标准输入,可以在主进程内向这个流写入数据,子进程就可以得到数据
fd 为 1 的流是标准输出,这里可以得到执行命令的输出内容
fd 为 2 的 pipe stream 就是 stderr ,读取 stderr 就能拿到错误信息输出
使用fread就可以拿到标准错误流输出的内容。
htf@htf-ThinkPad-T470p:~/workspace/swoole/examples/coroutine$ php proc_open.php
array(3) {
[0]=>
resource(4) of type (stream)
[1]=>
resource(5) of type (stream)
[2]=>
resource(6) of type (stream)
}
string(26) "sh: 1: unknown: not found
"
command returned 32512
htf@htf-ThinkPad-T470p:~/workspace/swoole/examples/coroutine$上述就是小编为大家分享的shell_exec 中怎么捕获标准错误流了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。