memo

memo dayo.

コマンドを実行して結果を取得する



// ==============================================
function do_command ($str_cmd, $str_in=''){
// define
$descriptorspec = array(
0 => array('pipe', 'r')
,1 => array('pipe', 'w')
,2 => array('pipe', 'w')
);

// init
$bol_rtn = False;
$str_out = '';
$str_err = '';

// exec
$fp = proc_open($str_cmd, $descriptorspec, $pipes);
if (is_resource($fp) !== False) {
fputs($pipes[0], $str_in);
fclose($pipes[0]);

while ($error = fgets($pipes[2])){
$str_err .= $error;
}

while ($result = fgets($pipes[1])){
$str_out .= $result;
}

foreach ($pipes as $idx => $val){
if (is_resource($val) !== False){
fclose($val);
}
unset($idx);
unset($val);
}

$bol_rtn = proc_close($fp);
}
unset($fp);

// end
return array(
'return' => $bol_rtn
,'stdout' => $str_out
,'stderr' => $str_err
);
}
// ==============================================