我需要从 PHP 脚本调用 python 脚本并将结果返回给 PHP。我玩proc_open
功能。但它不起作用。你知道为什么吗?这是 PHP 脚本:
$msg = "this is a new message \n bla ble !@#$%^&*%(*))(_+=-";
$descriptorspec = array(
0 => array("pipe","r"),
1 => array("pipe","w"),
2 => array("file","./error.log","a")
) ;
$cwd = './' ;
$command = 'python ./upper.py ';
$proc = proc_open($command, $descriptorspec, $pipes, $cwd) ;
if ( is_resource( $proc ) ) {
fwrite( $pipes[0], $msg );
fclose( $pipes[0] );
fclose( $pipes[1] );
proc_close($proc);
echo "proc is closed\n";
}
else {
echo 'proc is not a resource';
}
python `upper.py' 脚本
import sys
print 'in python script'
data = sys.stdin.readlines()
print data
输出是:
in php script
proc is closed
我在error.log中有错误:
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr