note 90342 deleted from function.proc-open by danbrown
| From: | danbrown@php.net | Date: | Fri, 17 Apr 2009 16:53:38 +0000 |
| Subject: | note 90342 deleted from function.proc-open by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153448@lists.php.net to get a copy of this message | ||
Note Submitter: mattalexxpub at gmail dot com
----
@missilesilo at gmail dot com
Your class is cool but I didn't get it working until I fixed one little typo:
This:
<?php
$descriptorspec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'r')
);
?>
Should be this:
<?php
$descriptorspec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
?>
Or the method hangs, waiting for input into stderr. So here's the corrected method:
<?php
class Process
{
public static function open($command)
{
$retval = '';
$error = '';
$descriptorspec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
$resource = proc_open($command, $descriptorspec, $pipes, null, $_ENV);
if (is_resource($resource))
{
$stdin = $pipes[0];
$stdout = $pipes[1];
$stderr = $pipes[2];
while (! feof($stdout))
{
$retval .= fgets($stdout);
}
while (! feof($stderr))
{
$error .= fgets($stderr);
}
fclose($stdin);
fclose($stdout);
fclose($stderr);
$exit_code = proc_close($resource);
}
if (! empty($error))
throw new Exception($error);
else
return $retval;
}
}
try
{
$output = Process::open('cat example.txt');
// do something with the output
}
catch (Exception $e)
{
echo $e->getMessage() . "\n";
// there was a problem executing the command
}
?>