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