PHP 函数proc_open笔记

来源:互联网 发布:人工智能研究生专业 编辑:程序博客网 时间:2024/05/16 17:22
双管道,多线程执行命令: 使用示例:

test.php:

$descriptorspec=array(
    0=>STDIN,
    1=>STDOUT,
    2=>STDERR
);

$process=proc_open('mysql -u root -p',$descriptorspec,$pipes)

这样 运行php test.php 看见的效果将和终端直接运行mysql命令一模一样。

$descriptorspec说明, 它表述了执行命令的执行文件获得从哪儿获得STDIN以及STDOUT输出到哪儿, STDERR输出到哪儿。

他一般有三种形式, 资源,管道,文件

管道表示:

$descriptorspec=array(
    0=>array('pipe','r'),
    1=>array('pipe','w'),
    2=>array('pipe','w'),
);

管道操作:

if(is_resource($process)){
    fwrite($pipes[0],'content');

    while($ret=fgets[$pipes[1]){
       echo ''.$ret;
    }
    while($ret=fgets[$pipes[2]){
    echo ''.$ret;
}
}
注意事项:

1,读取管道时,有时候不能用stream_get_contents获得内容,必须用fgets, 这时候往往是因为命令执行文件有while死循环,不能结束进程,stream_get_contents是程序结束进程后才能

读取到。 而fgets 每次只读取一行, 要求命令执行文件输出内容需要带换行。 并且有时候需要用while语句循环来读取, 因为fgets每次只读一行 这样保证所有的都读取完了。

2,再读取管道时,往往会堵塞。但是不影响下面程序的执行。 如:

$ret=fgets($pipes[1]);//这里有堵塞。
echo $ret;
fwrite($pipes[0],'content');//但是这里可以执行。

这时候fgets 不能加while循环。


3,stream_set_blocking函数可以设置是否堵塞,但是不知道为什么对管道好像不起作用。

4,将读取内容加上空字符串后才进行echo输出的目的, 是将STDERR, STDOUT等特殊类型的字符串转换为标准字符串 ,不然有些程序将读取不了这些字符串,比如用  ob_get_clean() 有可能读取不了。  

public function __construct($cmd){        $this->pipes = null;        $this->descriptors = array(            0 => array('pipe', 'r'),            1 => array('pipe', 'w'),            2 => array('pipe', 'w')        );        $this->resource = proc_open($cmd, $this->descriptors, $this->pipes);        stream_set_blocking($this->pipes[0], FALSE);        stream_set_blocking($this->pipes[1], FALSE);    }

$status = proc_get_status($this->resource);
Array(    [command] => top    [pid] => 7985    [running] => 1    [signaled] =>    [stopped] =>    [exitcode] => -1    [termsig] => 0    [stopsig] => 0)

switch($status['exitcode']) {     case '255':     case '-1':     case '1':         return false;     case '0':         // normal end         return false;     default:         // unknown}

public function errors(){     $msg = 'Error:';     while (is_resource($this->pipes[self::PIPE_STDOUT]) && !feof($this->pipes[self::PIPE_STDOUT])) {        $msg .= fgets($this->pipes[self::PIPE_STDOUT]);     }     while (is_resource($this->pipes[self::PIPE_STDERR]) && !feof($this->pipes[self::PIPE_STDERR])) {        $msg .= fgets($this->pipes[self::PIPE_STDERR]);     }     fclose($this->pipes[self::PIPE_STDERR]);     proc_close($this->resource);     return $msg;}



原创粉丝点击