我可以像这样简单地使用 PHP 执行 Python 程序吗? (在浏览器中)
exec("python myProgram /Applications/MAMP/htdocs/somefile.xml");
或者像这样:
exec("/path/to/python path/to/myProgram /Applications/MAMP/htdocs/somefile.xml");
这个方法正确吗?
如果没有,正确的方法应该是什么?
谢谢
最佳答案
我更喜欢使用 mvds 建议的 proc_open()
,因为您无法使用 exec()
/shell_exec( 写入 STDIN 或从 STDOUT 读取)
,以及提供您自己的一组环境变量 -$_ENV
。
从我的代码中提取的示例片段:
$process = proc_open(
"{$command}",
array(
array('pipe', 'r'),
array('pipe', 'w'),
array('pipe', 'w')
),
$pipes,
NULL,
$_ENV
);
if(is_resource($process)){
fwrite($pipes[0], $string);
fclose($pipes[0]);
$rt = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$rtErr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$exitCode = proc_close($process);
}
关于php - 使用 PHP 执行 Python 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3276491/